[alt] typing: accept buffers in IO.write#9861
Conversation
Co-authored-by: JelleZijlstra <jelle.zijlstra@gmail.com>
| @overload | ||
| def write(self, s: AnyStr) -> int: ... |
There was a problem hiding this comment.
Is the third overload needed? This should be covered by the preceding two overloads. (Same for further overloads below.)
There was a problem hiding this comment.
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.
|
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
|
Two errors go away ( |
An alternative PR to #9084 (closes #9084)
Fixes #9082
Co-authored-by: JelleZijlstra jelle.zijlstra@gmail.com