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

Make BufferedReader generic over a protocol #13533

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

srittau
Copy link
Collaborator

@srittau srittau commented Feb 24, 2025

Works towards #13495

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

Copy link
Contributor

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

streamlit (https://github.com/streamlit/streamlit)
- lib/tests/streamlit/elements/audio_test.py:78:38: error: Argument 1 to "_calculate_file_id" has incompatible type "Union[str, Path, bytes, BytesIO, RawIOBase, BufferedReader, Any, None]"; expected "bytes"  [arg-type]
+ lib/tests/streamlit/elements/audio_test.py:78:38: error: Argument 1 to "_calculate_file_id" has incompatible type "Union[str, Path, bytes, BytesIO, RawIOBase, BufferedReader[_BufferedReaderStream], Any, None]"; expected "bytes"  [arg-type]

werkzeug (https://github.com/pallets/werkzeug)
- tests/test_wsgi.py:158: error: Incompatible types in assignment (expression has type "TextIOWrapper[BufferedReader]", variable has type "LimitedStream")  [assignment]
+ tests/test_wsgi.py:158: error: Incompatible types in assignment (expression has type "TextIOWrapper[BufferedReader[LimitedStream]]", variable has type "LimitedStream")  [assignment]
+ tests/test_wsgi.py:158: error: Value of type variable "_BufferedReaderStreamT" of "BufferedReader" cannot be "LimitedStream"  [type-var]

urllib3 (https://github.com/urllib3/urllib3)
+ test/test_response.py:770: error: Unused "type: ignore" comment  [unused-ignore]
+ test/test_response.py:770: error: Value of type variable "_BufferedReaderStreamT" of "BufferedReader" cannot be "HTTPResponse"  [type-var]
+ test/test_response.py:770: note: Error code "type-var" not covered by "type: ignore" comment
+ test/test_response.py:782: error: Unused "type: ignore" comment  [unused-ignore]
+ test/test_response.py:782: error: Value of type variable "_BufferedReaderStreamT" of "BufferedReader" cannot be "HTTPResponse"  [type-var]
+ test/test_response.py:782: note: Error code "type-var" not covered by "type: ignore" comment
+ test/test_response.py:787: error: Unused "type: ignore" comment  [unused-ignore]
+ test/test_response.py:787: error: Value of type variable "_BufferedReaderStreamT" of "BufferedReader" cannot be "HTTPResponse"  [type-var]
+ test/test_response.py:787: note: Error code "type-var" not covered by "type: ignore" comment
+ test/test_response.py:800: error: Unused "type: ignore" comment  [unused-ignore]
+ test/test_response.py:800: error: Value of type variable "_BufferedReaderStreamT" of "BufferedReader" cannot be "HTTPResponse"  [type-var]
+ test/test_response.py:800: note: Error code "type-var" not covered by "type: ignore" comment

materialize (https://github.com/MaterializeInc/materialize)
- misc/python/materialize/mzbuild.py:828: error: Incompatible types in assignment (expression has type "BufferedRandom", variable has type "BufferedReader")  [assignment]
+ misc/python/materialize/mzbuild.py:828: error: Incompatible types in assignment (expression has type "BufferedRandom", variable has type "BufferedReader[_BufferedReaderStream]")  [assignment]

@srittau
Copy link
Collaborator Author

srittau commented Feb 24, 2025

Looking at the primer hits: Both LimitedStream (from werkzeug) and HTTPResponse (from urllib3) have wrong annotations for their readinto() methods: BufferedReader (both, the C and Python implementation) sends a memoryview to self.raw.readinto(), while the libraries's readinto() methods are annotated as accepting only bytearray. But their implementation actually accept memoryviews:

@srittau srittau marked this pull request as ready for review February 24, 2025 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant