Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

non-writeable property name in IO #2824

Closed
jolaf opened this issue Mar 5, 2019 · 1 comment
Closed

non-writeable property name in IO #2824

jolaf opened this issue Mar 5, 2019 · 1 comment

Comments

@jolaf
Copy link
Contributor

jolaf commented Mar 5, 2019

I'm trying to create my own class that has IO capability.
The class instances would have names similar to file names, but specified at the moment of instance creation.
However when I try setting name field, mypy is protesting.

Here's the minimal repro:

from typing import AnyStr, IO
class MyIO(IO[AnyStr]):
    def __init__(self, name: str) -> None:
        self.name = name

mypy output:

Test.py: note: In member "__init__" of class "MyIO":
Test.py:5: error: Property "name" defined in "IO" is read-only
@srittau
Copy link
Collaborator

srittau commented Mar 5, 2019

IO.name is defined as a property in both the typing implementation and the stubs. You need to override this property in your sub-class if you want to provide a setter:

from typing import AnyStr, IO

class MyIO(IO[AnyStr]):
    def __init__(self, name: str) -> None:
        self.name = name

    @property
    def name(self) -> str:
        ...

    @name.setter
    def name(self, name: str) -> None:
        ...

@srittau srittau closed this as completed Mar 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants