Skip to content

BytesIO.name and StringIO.name should not be defined #1790

Closed
@srittau

Description

@srittau

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions