Skip to content
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

Merged
merged 5 commits into from
May 31, 2020
Merged

Conversation

JelleZijlstra
Copy link
Member

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.

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.
opener: Optional[_Opener] = ...,
) -> BufferedReader: ...

# Buffering argument is not given: return BufferedRandom, BufferedReader, or BufferedWriter
Copy link
Collaborator

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 ...?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, you're right

@srittau srittau added the topic: io I/O related issues label May 30, 2020
Copy link
Collaborator

@srittau srittau left a 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!

@srittau srittau merged commit adeda24 into python:master May 31, 2020
@JelleZijlstra JelleZijlstra deleted the openret branch June 1, 2020 00:14
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: io I/O related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BinaryIO does not have peek method
2 participants