Please consider the following Python 3.6 code:
fromioimportBytesIOclassNamedBytesIO(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:
fromioimportStringIOclassNamedStringIO(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'
Please consider the following Python 3.6 code:
mypy 0.560 complains:
This is due to the fact that in typeshed
BytesIOderives fromBinaryIO, which defines thenameproperty. The actualBytesIOimplementation does not have such an attribute:Interestingly,
StringIOdoes not have this problem:This checks fine with mypy, since the
StringIOstub includes anamefield, which also does not match the implementation: