How to correctly override BinaryIO.write
?
#5275
-
I have written a # pyright: strict
from typing import BinaryIO
class Decorator(BinaryIO):
def __init__(self, file: BinaryIO) -> None:
self._file = file
def write(self, s: bytes) -> int:
return self._file.write(s) This passed Pyright 1.1.311, but fails 1.1.313 with the following error:
Looking at typeshed, I think Pyright is complaining about the method not being compatible with the following overload: @overload
def write(self: IO[str], __s: str) -> int: ... But I also tried copying the annotations from typeshed as closely as I could. I checked this with Pyright built from d20a39c so it wouldn't complain about importing # pyright: strict
from typing import Any, AnyStr, BinaryIO, overload
from typing_extensions import Buffer
class Decorator(BinaryIO):
def __init__(self, file: BinaryIO) -> None:
self._file = file
@overload
def write(self, s: str) -> int: ...
@overload
def write(self, s: Buffer) -> int: ...
@overload
def write(self, s: AnyStr) -> int: ...
def write(self, s: Any) -> int:
return self._file.write(s) For this, Pyright generates the following error:
The overloads are in the same order as the base method, so I don't understand what Pyright is trying to tell me here, unless the overloads do not match the base method closely enough. If this is not a bug, could someone explain why Pyright is generating this error? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
There is a bug in the logic for overloaded overrides, which I added recently. I'll fix this in the next release. That said, to avoid the class Decorator(BinaryIO):
def __init__(self, file: BinaryIO) -> None:
self._file = file
@overload
def write(self: IO[str], s: str, /) -> int:
...
@overload
def write(self: IO[bytes], s: ReadableBuffer, /) -> int:
...
@overload
def write(self, s: AnyStr, /) -> int:
...
def write(self, s: Any, /) -> int:
return self._file.write(s) |
Beta Was this translation helpful? Give feedback.
There is a bug in the logic for overloaded overrides, which I added recently. I'll fix this in the next release.
That said, to avoid the
reportIncompatibleMethodOverride
error, you'll need to copy the overload signatures from typeshed exactly. That includes the use of a positional-only parameter__s
rather than a named parameters
. It also includes type annotations for theself
parameter on the first two overloads.