Closed
Description
Please consider the following Python 3.6 code:
from io import BytesIO
class NamedBytesIO(BytesIO):
def __init__(self, content: bytes, name: str) -> None:
super().__init__(content)
self.name = name
mypy 0.560 complains:
test.py:8: error: Property "name" defined in "NamedBytesIO" is read-only
This is due to the fact that in typeshed BytesIO
derives from BinaryIO
, which defines the name
property. The actual BytesIO
implementation does not have such an attribute:
>>> from io import BytesIO
>>> BytesIO(b"").name
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: '_io.BytesIO' object has no attribute 'name'
Interestingly, StringIO
does not have this problem:
from io import StringIO
class NamedStringIO(StringIO):
def __init__(self, content: str, name: str) -> None:
super().__init__(content)
self.name = name
This checks fine with mypy, since the StringIO
stub includes a name
field, which also does not match the implementation:
>>> from io import StringIO
>>> StringIO("").name
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: '_io.StringIO' object has no attribute 'name'
Metadata
Metadata
Assignees
Labels
No labels