Skip to content

TYP: tighten IO Protocols #326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 24, 2022
Merged

TYP: tighten IO Protocols #326

merged 1 commit into from
Sep 24, 2022

Conversation

twoertwein
Copy link
Member

and to ensure that changes of pandas-dev/pandas#48144 also work.

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Sep 24, 2022

It's not clear to me why we have to declare the methods of these various Buffer types for the public stubs, as users wouldn't instantiate those classes.

Is the goal to make it so that what we have in public pandas for these types in _typing.py is the same as in _typing.pyi in pandas-stubs ?

@twoertwein
Copy link
Member Author

It's not clear to me why we have to declare the methods of these various Buffer types for the public stubs

Not having some of those mehods/properties can lead to errors such as pandas-dev/pandas#45488 and more obvious errors (not having a read method for buffers used for reading - surprise). pandas._typing has comments for which feature each method is needed. We cannot use typing.IO as it requires way too many methods/properties (see python/typeshed#4212) so I tried to add the minimal needed methods/properties in pandas-dev/pandas#43951 (with an update in pandas-dev/pandas#48144).

as users wouldn't instantiate those classes.

These are protocols, they don't need to be instantiated (like typing.IO). The current tests of pandas-stubs show that all the current IO tests still work fine :)

Is the goal to make it so that what we have in public pandas for these types in _typing.py is the same as in _typing.pyi in pandas-stubs ?

That's a secondary goal.


class ReadPickleBuffer(ReadBuffer[bytes], Protocol):
def readline(self, size: int | None = ...) -> bytes: ...
def readline(self) -> bytes: ...
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the buffers are used as input arguments (not as return values), so we can remove not needed optional arguments to be compatible with a wider class of IO functions that might or might not implement optional keywords.

@Dr-Irv Dr-Irv merged commit 0bd7a57 into pandas-dev:main Sep 24, 2022
@twoertwein twoertwein deleted the io branch February 10, 2024 20:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants