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

Tighten TextIOWrapper buffer types #10266

Closed
wants to merge 1 commit into from
Closed

Conversation

srittau
Copy link
Collaborator

@srittau srittau commented Jun 6, 2023

This matches the documentation, which says:

A buffered text stream providing higher-level access to a BufferedIOBase
buffered binary stream.

Cf #10229

This matches the documentation, which says:

> A buffered text stream providing higher-level access to a BufferedIOBase
> buffered binary stream.

Cf python#10229
@srittau srittau added the topic: io I/O related issues label Jun 6, 2023
@srittau
Copy link
Collaborator Author

srittau commented Jun 6, 2023

Also, the current annotations are unsound. You can pass in any IO[bytes], but are guaranteed to get back a BinaryIO from .buffer and .detach(), although in reality you just get back what you passed in.

@github-actions
Copy link
Contributor

github-actions bot commented Jun 6, 2023

Diff from mypy_primer, showing the effect of this PR on open source code:

pytest (https://github.com/pytest-dev/pytest)
+ src/_pytest/capture.py:174: error: Returning Any from function declared to return "str"  [no-any-return]
+ src/_pytest/capture.py:174: error: "BufferedIOBase" has no attribute "mode"  [attr-defined]
+ src/_pytest/capture.py:466: error: Argument 1 to "EncodedFile" has incompatible type "IO[bytes]"; expected "BufferedIOBase"  [arg-type]
+ testing/test_capture.py:1635: error: Argument 1 to "EncodedFile" has incompatible type "BinaryIO"; expected "BufferedIOBase"  [arg-type]

rich (https://github.com/Textualize/rich)
+ rich/progress.py:1363: error: Argument 1 to "TextIOWrapper" has incompatible type "_Reader"; expected "BufferedIOBase"  [arg-type]

urllib3 (https://github.com/urllib3/urllib3)
+ src/urllib3/util/ssltransport.py:165: error: Argument 1 to "TextIOWrapper" has incompatible type "BinaryIO"; expected "BufferedIOBase"  [arg-type]

streamlit (https://github.com/streamlit/streamlit)
- lib/tests/streamlit/web/server/component_request_handler_test.py:161:38: error: Argument 1 to "TextIOWrapper" has incompatible type "str"; expected "IO[bytes]"  [arg-type]
+ lib/tests/streamlit/web/server/component_request_handler_test.py:161:38: error: Argument 1 to "TextIOWrapper" has incompatible type "str"; expected "BufferedIOBase"  [arg-type]

black (https://github.com/psf/black)
+ src/black/__init__.py:844:17: error: Argument 1 to "TextIOWrapper" has incompatible type "BinaryIO"; expected "BufferedIOBase"  [arg-type]
+ src/black/__init__.py:888:13: error: Argument 1 to "TextIOWrapper" has incompatible type "BinaryIO"; expected "BufferedIOBase"  [arg-type]

cwltool (https://github.com/common-workflow-language/cwltool)
+ cwltool/main.py:55: note: In module imported here:
+ cwltool/cwlprov/writablebagfile.py: note: In function "write_bag_file":
+ cwltool/cwlprov/writablebagfile.py:119:30: error: Argument 1 to "TextIOWrapper" has incompatible type "BinaryIO"; expected "BufferedIOBase"  [arg-type]
+ cwltool/main.py: note: In function "main":
+ cwltool/main.py:965:43: error: Argument 1 to "TextIOWrapper" has incompatible type "BinaryIO"; expected "BufferedIOBase"  [arg-type]

@AlexWaygood
Copy link
Member

I'd be interested to see the mypy_primer results if we tried making it generic. It might be very disruptive, but it also might not.

@srittau
Copy link
Collaborator Author

srittau commented Jun 6, 2023

Ok, not sure why I replicated @JelleZijlstra's issue #10229. I tried to actually address one of the suggestions I made in there, but accidentally replicated it mostly. Although this PR is a little but more extensive, as it also adds .detach(), so I'll leave it open.

@srittau
Copy link
Collaborator Author

srittau commented Jun 6, 2023

Anyway, the merge order is #10233 -> #10232 -> potentially this or #10229

@srittau
Copy link
Collaborator Author

srittau commented Jun 6, 2023

I suggest we merge the two other PRs first, rerun the tests here and then I'll also do a test with a generic TextIOWrapper.

srittau added a commit to JelleZijlstra/typeshed that referenced this pull request Jun 6, 2023
Incorporates changes from python#10266, so we don't have two PRs open.
@srittau
Copy link
Collaborator Author

srittau commented Jun 6, 2023

I merged the additional changes into #10229, closing this here.

@srittau srittau closed this Jun 6, 2023
@srittau srittau deleted the io-detach branch June 6, 2023 11:37
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.

2 participants