open: introduce concrete return types #4146
Merged
srittau merged 5 commits intopython:masterfrom May 31, 2020
Merged
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.
srittau
reviewed
May 30, 2020
stdlib/2/__builtin__.pyi
Outdated
| opener: Optional[_Opener] = ..., | ||
| ) -> BufferedReader: ... | ||
|
|
||
| # Buffering argument is not given: return BufferedRandom, BufferedReader, or BufferedWriter |
Collaborator
There was a problem hiding this comment.
Can't these three overloads not be merged into the overloads above using ...?
srittau
approved these changes
May 31, 2020
Collaborator
srittau
left a comment
There was a problem hiding this comment.
This is quite an exciting change and quite a large step towards better IO types!
vishalkuo
pushed a commit
to vishalkuo/typeshed
that referenced
this pull request
Jun 26, 2020
* 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.)
Hexcles
added a commit
to web-platform-tests/wpt
that referenced
this pull request
Oct 13, 2020
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.
Hexcles
added a commit
to web-platform-tests/wpt
that referenced
this pull request
Oct 13, 2020
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.
Hexcles
added a commit
to web-platform-tests/wpt
that referenced
this pull request
Oct 14, 2020
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.
Hexcles
added a commit
to web-platform-tests/wpt
that referenced
this pull request
Oct 14, 2020
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.
stephenmcgruer
pushed a commit
to web-platform-tests/wpt
that referenced
this pull request
Oct 14, 2020
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.
moz-v2v-gh
pushed a commit
to mozilla/gecko-dev
that referenced
this pull request
Oct 21, 2020
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
jamienicol
pushed a commit
to jamienicol/gecko
that referenced
this pull request
Oct 24, 2020
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
bhearsum
pushed a commit
to mozilla-releng/staging-firefox
that referenced
this pull request
May 1, 2025
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.