-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
open: introduce concrete return types #4146
Conversation
This makes these classes usable if type annotations are given as "IO" or "TextIO". In the future, we'll then be able to move open() to return a concrete class instead (python#3951).
Fixes python#3951. We use the values of the "mode" and "buffering" arguments to figure out the concrete type open() will return at runtime. (Compare the CPython code in https://github.com/python/cpython/blob/master/Modules/_io/_iomodule.c#L231.) I tested by writing a script that generated every mode/buffering combination, then running mypy with the open plugin disabled and comparing the results. This PR depends on python#4145.
stdlib/2/__builtin__.pyi
Outdated
opener: Optional[_Opener] = ..., | ||
) -> BufferedReader: ... | ||
|
||
# Buffering argument is not given: return BufferedRandom, BufferedReader, or BufferedWriter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't these three overloads not be merged into the overloads above using ...
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you're right
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is quite an exciting change and quite a large step towards better IO types!
* make io classes inherit from typing IO classes This makes these classes usable if type annotations are given as "IO" or "TextIO". In the future, we'll then be able to move open() to return a concrete class instead (python#3951). * open: introduce concrete return types Fixes python#3951. We use the values of the "mode" and "buffering" arguments to figure out the concrete type open() will return at runtime. (Compare the CPython code in https://github.com/python/cpython/blob/master/Modules/_io/_iomodule.c#L231.)
We have upgraded to mypy 0.790 in #26066, which brings us python/typeshed#4146. Unfortunately, we still have to use io.open instead of the built-in open to make mypy happy in Python 2, because open() is still typed as BinaryIO unconditionally in Python 2.
We have upgraded to mypy 0.790 in #26066, which brings us python/typeshed#4146. Unfortunately, we still have to use io.open instead of the built-in open to make mypy happy in Python 2, because open() is still typed as BinaryIO unconditionally in Python 2. Also made some drive-by stylistic changes.
We have upgraded to mypy 0.790 in #26066, which brings us python/typeshed#4146. Unfortunately, we still have to use io.open instead of the built-in open because the "text mode" of open() in Python 2 doesn't really take text (unicode). Also made some drive-by stylistic changes.
We have upgraded to mypy 0.790 in #26066, which brings us python/typeshed#4146. Unfortunately, we still have to use io.open instead of the built-in open because the "text mode" of open() in Python 2 doesn't really take text (unicode). Also made some drive-by stylistic changes.
We have upgraded to mypy 0.790 in #26066, which brings us python/typeshed#4146. Unfortunately, we still have to use io.open instead of the built-in open because the "text mode" of open() in Python 2 doesn't really take text (unicode). Also made some drive-by stylistic changes.
Automatic update from web-platform-tests Remove a hack for mypy<0.790 (#26102) We have upgraded to mypy 0.790 in #26066, which brings us python/typeshed#4146. Unfortunately, we still have to use io.open instead of the built-in open because the "text mode" of open() in Python 2 doesn't really take text (unicode). Also made some drive-by stylistic changes. -- wpt-commits: fdc3a462396df8af5d2d8f2e5277e98817ffbb9d wpt-pr: 26102
Automatic update from web-platform-tests Remove a hack for mypy<0.790 (#26102) We have upgraded to mypy 0.790 in #26066, which brings us python/typeshed#4146. Unfortunately, we still have to use io.open instead of the built-in open because the "text mode" of open() in Python 2 doesn't really take text (unicode). Also made some drive-by stylistic changes. -- wpt-commits: fdc3a462396df8af5d2d8f2e5277e98817ffbb9d wpt-pr: 26102
Fixes #3951.
We use the values of the "mode" and "buffering" arguments to figure out
the concrete type open() will return at runtime. (Compare the CPython
code in https://github.com/python/cpython/blob/master/Modules/_io/_iomodule.c#L231.)
I tested by writing a script that generated every mode/buffering combination, then
running mypy with the open plugin disabled and comparing the results.
This PR depends on #4145.