-
-
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
Use a Protocol as the argument for io.TextIOWrapper #10229
Conversation
I ran into a false positive where gzip.GzipFile couldn't be assigned to io.TextIOWrapper.
The Protocol is based on reading https://github.com/python/cpython/blob/main/Modules/_io/textio.c, specifically how it uses |
This comment has been minimized.
This comment has been minimized.
Considering the (valid) primer hits: The documentation says: "A buffered text stream providing higher-level access to a BufferedIOBase buffered binary stream." Would it make sense to use that type? I didn't investigate yet. |
Looks like |
This comment has been minimized.
This comment has been minimized.
OK, that's worse for mypy-primer. |
Looking at the primer hits:
Overall I would say that we should fix the other two problems in typeshed first and then do this change. It's a bit disruptive, but I think overall worth it for finding more potential problems and further cleanup of the I/O types. |
This matches the documentation, which says: > A buffered text stream providing higher-level access to a BufferedIOBase > buffered binary stream. Cf python#10229
Incorporates changes from python#10266, so we don't have two PRs open.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Actually, the |
This comment has been minimized.
This comment has been minimized.
To recap the primer output: Category 1: Passes in incompatible typeThese seem to work at runtime, but still I think that a type checker pointing out that this could be problematic is actually an advantage. Explicitly casting away the problem would acknowledge this in the implementation.
Category 2: Passes in `sys.stdout.bufferThese have the usual "std" types problems. Using an explicit assert on
Category 3: Acknowledged as problematic in the source codeI see adding a cast here as acceptable.
Category 4: Needs to update its explicit type annotation/cast
As written before, despite the primer output I think this change is worthwhile as is, for the reasons outlined above. |
I think I'd prefer to go back to a protocol, even if the documentation explicitly calls for a |
This comment has been minimized.
This comment has been minimized.
I consider this solution worse. For example, the primer hit at src/_pytest/capture.py:546 is a valid use that's now rejected. While this particular instance can be fixed by adjusting the protocol, we can't fix all valid uses without the protocol actually mimicking The best solution would be to make |
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:179: error: Returning Any from function declared to return "str" [no-any-return]
+ src/_pytest/capture.py:179: error: "_TextIOWrapperBuffer" has no attribute "mode" [attr-defined]
+ src/_pytest/capture.py:471: error: Argument 1 to "EncodedFile" has incompatible type "FileIO"; expected "_TextIOWrapperBuffer" [arg-type]
+ src/_pytest/capture.py:471: note: Following member(s) of "FileIO" have conflicts:
+ src/_pytest/capture.py:471: note: name: expected "str", got "int | str | bytes | PathLike[str] | PathLike[bytes]"
+ src/_pytest/capture.py:551: error: Too few arguments for "read" of "_TextIOWrapperBuffer" [call-arg]
+ src/_pytest/capture.py:554: error: Incompatible return value type (got "Buffer", expected "bytes") [return-value]
streamlit (https://github.com/streamlit/streamlit)
- lib/tests/streamlit/web/server/component_request_handler_test.py:176: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:176:38: error: Argument 1 to "TextIOWrapper" has incompatible type "str"; expected "_TextIOWrapperBuffer" [arg-type]
|
Just wanted to confirm the primer output was still the same, closing in favor of #11420. |
I ran into a false positive where gzip.GzipFile couldn't be assigned
to io.TextIOWrapper.