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

[alt] typing: accept buffers in IO.write #9861

Merged
merged 1 commit into from
Mar 15, 2023

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Mar 10, 2023

An alternative PR to #9084 (closes #9084)

Fixes #9082

Co-authored-by: JelleZijlstra jelle.zijlstra@gmail.com

Co-authored-by: JelleZijlstra <jelle.zijlstra@gmail.com>
Comment on lines +222 to 223
@overload
def write(self, s: AnyStr) -> int: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the third overload needed? This should be covered by the preceding two overloads. (Same for further overloads below.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately the test cases I've added don't pass for pyright unless we include this third overload. This is due to a design decision by pyright in the way it handles contrained TypeVars that leads to different behaviour from mypy: https://github.com/microsoft/pyright/blob/main/docs/mypy-comparison.md#constrained-type-variables

It's the same issue that led to microsoft/pyright#4534 being filed, which we then fixed over at typeshed in #9592.

@github-actions
Copy link
Contributor

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

rich (https://github.com/Textualize/rich)
+ rich/progress.py:173: error: Definition of "writelines" in base class "IOBase" is incompatible with definition in base class "IO"  [misc]

psycopg (https://github.com/psycopg/psycopg)
+ psycopg/psycopg/copy.py:464: error: Unused "type: ignore" comment

urllib3 (https://github.com/urllib3/urllib3)
+ test/test_ssltransport.py:443: error: Unused "type: ignore" comment
+ test/test_ssltransport.py:443: error: No overload variant of "write" of "IO" matches argument type "bytes"  [call-overload]
+ test/test_ssltransport.py:443: note: Error code "call-overload" not covered by "type: ignore" comment
+ test/test_ssltransport.py:443: note: Possible overload variants:
+ test/test_ssltransport.py:443: note:     def write(self, str, /) -> int
+ test/test_ssltransport.py:480: error: No overload variant of "write" of "IO" matches argument type "str"  [call-overload]
+ test/test_ssltransport.py:480: note: Error code "call-overload" not covered by "type: ignore" comment
+ test/test_ssltransport.py:480: note: Possible overload variants:
+ test/test_ssltransport.py:480: note:     def write(self, Union[bytes, Union[bytearray, memoryview, array[Any], mmap, _CData, PickleBuffer]], /) -> int
+ test/test_ssltransport.py:480: note:     def write(self, bytes, /) -> int

cwltool (https://github.com/common-workflow-language/cwltool)
+ cwltool/main.py:963: error: Unused "type: ignore[arg-type]" comment

@AlexWaygood AlexWaygood marked this pull request as ready for review March 10, 2023 13:54
@AlexWaygood
Copy link
Member Author

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

Two errors go away (psycopg, cwltool), one new error appears (rich), and two existing errors have their error code changed (urllib3). Looks promising to me. @JelleZijlstra, what do you think?

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.

Support buffers in IO[bytes].write
3 participants