typeshed
typeshed copied to clipboard
add _io module
In addition to shuffling defintions between files, these things got tweaked:
- I moved BufferedIOBase.raw to the child classes which do define that attribute on themselves
- Removed several redundant definitions of __enter__. These seem to be an artifact of using TypeVar instead of Self originally
- seek and truncate have inconsistent variable names. They're also positional only, so they were in the stubtest allowlist before, but I went ahead and made them explicit in the stubs
- BytesIO.readlines shouldn't have been on the allowlist in the first place. It differs not only in variable name, but also default value.
- A big block of functions in TextIOWrapper were commented that mypy needed them, but I don't think it does anymore, unless there's a problem with subclassing TextIOWrapper that doesn't show up in typeshed itself. No indication in the history about that.
- In the implementation, the concrete classes inherit from the private implementation _*IOBase classes, not the classes in io which are actually metaclasses, but they are registered to those metaclasses at runtime. It wasn't technically required for any reason, but I split the difference on this by keeping the _*IOBase classes in their base classes directly. I think it's a bit of a reminder of the actual implementation, and means that a stubtest check for inheritance will show that typeshed is adding to the base classes, rather than replacing the base class, and I think that's a little cleaner.
Partially related to https://github.com/python/typeshed/issues/3968
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]
+ rich/progress.py:173: error: Definition of "writelines" in base class "_IOBase" is incompatible with definition in base class "IO" [misc]
pytest (https://github.com/pytest-dev/pytest)
+ src/_pytest/capture.py:168: error: Cannot override writeable attribute with read-only property [override]
+ testing/io/test_terminalwriter.py:81: error: Incompatible types in assignment (expression has type "StringIO", variable has type "TextIOWrapper") [assignment]
operator (https://github.com/canonical/operator)
- ops/pebble.py:1631: error: Argument 1 of "write" is incompatible with supertype "BufferedIOBase"; supertype defines the argument type as "Buffer" [override]
+ ops/pebble.py:1631: error: Argument 1 of "write" is incompatible with supertype "_BufferedIOBase"; supertype defines the argument type as "Buffer" [override]
- ops/pebble.py:1655: error: Return type "str | bytes" of "read" incompatible with return type "bytes" in supertype "BufferedIOBase" [override]
+ ops/pebble.py:1655: error: Return type "str | bytes" of "read" incompatible with return type "bytes" in supertype "_BufferedIOBase" [override]
- ops/pebble.py:1655: error: Argument 1 of "read" is incompatible with supertype "BufferedIOBase"; supertype defines the argument type as "int | None" [override]
+ ops/pebble.py:1655: error: Argument 1 of "read" is incompatible with supertype "_BufferedIOBase"; supertype defines the argument type as "int | None" [override]
- ops/pebble.py:1688: error: Return type "str | bytes" of "read1" incompatible with return type "bytes" in supertype "BufferedIOBase" [override]
+ ops/pebble.py:1688: error: Return type "str | bytes" of "read1" incompatible with return type "bytes" in supertype "_BufferedIOBase" [override]
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]
+ rich/progress.py:173: error: Definition of "writelines" in base class "_IOBase" is incompatible with definition in base class "IO" [misc]
pytest (https://github.com/pytest-dev/pytest)
+ src/_pytest/capture.py:168: error: Cannot override writeable attribute with read-only property [override]
operator (https://github.com/canonical/operator)
- ops/pebble.py:1631: error: Argument 1 of "write" is incompatible with supertype "BufferedIOBase"; supertype defines the argument type as "Buffer" [override]
+ ops/pebble.py:1631: error: Argument 1 of "write" is incompatible with supertype "_BufferedIOBase"; supertype defines the argument type as "Buffer" [override]
- ops/pebble.py:1655: error: Return type "str | bytes" of "read" incompatible with return type "bytes" in supertype "BufferedIOBase" [override]
+ ops/pebble.py:1655: error: Return type "str | bytes" of "read" incompatible with return type "bytes" in supertype "_BufferedIOBase" [override]
- ops/pebble.py:1655: error: Argument 1 of "read" is incompatible with supertype "BufferedIOBase"; supertype defines the argument type as "int | None" [override]
+ ops/pebble.py:1655: error: Argument 1 of "read" is incompatible with supertype "_BufferedIOBase"; supertype defines the argument type as "int | None" [override]
- ops/pebble.py:1688: error: Return type "str | bytes" of "read1" incompatible with return type "bytes" in supertype "BufferedIOBase" [override]
+ ops/pebble.py:1688: error: Return type "str | bytes" of "read1" incompatible with return type "bytes" in supertype "_BufferedIOBase" [override]
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]
+ rich/progress.py:173: error: Definition of "writelines" in base class "_IOBase" is incompatible with definition in base class "IO" [misc]
operator (https://github.com/canonical/operator)
- ops/pebble.py:1631: error: Argument 1 of "write" is incompatible with supertype "BufferedIOBase"; supertype defines the argument type as "Buffer" [override]
+ ops/pebble.py:1631: error: Argument 1 of "write" is incompatible with supertype "_BufferedIOBase"; supertype defines the argument type as "Buffer" [override]
- ops/pebble.py:1655: error: Return type "str | bytes" of "read" incompatible with return type "bytes" in supertype "BufferedIOBase" [override]
+ ops/pebble.py:1655: error: Return type "str | bytes" of "read" incompatible with return type "bytes" in supertype "_BufferedIOBase" [override]
- ops/pebble.py:1655: error: Argument 1 of "read" is incompatible with supertype "BufferedIOBase"; supertype defines the argument type as "int | None" [override]
+ ops/pebble.py:1655: error: Argument 1 of "read" is incompatible with supertype "_BufferedIOBase"; supertype defines the argument type as "int | None" [override]
- ops/pebble.py:1688: error: Return type "str | bytes" of "read1" incompatible with return type "bytes" in supertype "BufferedIOBase" [override]
+ ops/pebble.py:1688: error: Return type "str | bytes" of "read1" incompatible with return type "bytes" in supertype "_BufferedIOBase" [override]
Unfortunately this has severe merge conflicts now. If you fix them, I will review the PR.