-
Notifications
You must be signed in to change notification settings - Fork 242
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
Add typing.Reader and typing.Writer #564
Comments
I like this idea. Using the current |
Of course many duck type providers for files don't even support close() or readlines(), so even the proposed I think that Also you've left out Finally this doesn't help much for return types -- we often also want to describe that something returns a readable stream (or a writable one) but the interface promised by many APIs is much wider than the minimal protocols proposed here. This is one of the reasons I punted on this when originally designing the IO API -- the other reason was of course that we didn't have protocols then. (There are other things one might want to express in the type system too, esp. whether |
Maybe a good way to go about this is to start replacing argument types in typeshed with custom protocols in the respective modules (marked private so they do not get reused outside the module) to get a feeling what is required in practice. |
I like this idea. If we don't do this, we risk implementing something that won't be that useful and will mostly confuse users. |
See python/typing#564 for background.
As seen above I submitted a first pull request implementing my last suggestion. Maybe it would be a good idea to mark those somehow, for example with a link to this issue, to be able to grep them easily later? |
I think just adding a link to this issue would be enough. |
@ilevkivskyi You mean in the commit message like above? |
I think adding the link in PR description may be better. GitHub only uses the commit message for PR description if there is a single commit in your PR, and as you can see the links to PRs are bigger than to commits. |
See python/typing#564 for background.
* loads() only accepts str as first argument for Python < 3.6 * Use a protocol for the first argument to load() (cf python/typing#564)
* loads() only accepts str as first argument for Python < 3.6 * Use a protocol for the first argument to load() (cf python/typing#564)
See python/typing#564 for background.
* loads() only accepts str as first argument for Python < 3.6 * Use a protocol for the first argument to load() (cf python/typing#564)
This seems like an area where Python could look at Golang's interfaces. In the case of IO, they have (concerning this)
This allows the end-user to operate with the specificity needed for the problem space at the cost of a more complex interface system. Personally, I've stayed away from Python's IO as it's never been clear to me what the intended use is from the docs. References |
We have a good starting point for useful protocols already in the |
I recently stumbled at a TODO in typeshed that proposes to split
IO[AnyStr]
into smaller pieces. I think it is a good idea and propose to add two protocols:Then
IO[AnyStr]
can subclass both, plus add some less often used methods (alsoIO
should stay nominal). What do you think?The text was updated successfully, but these errors were encountered: