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

Proper way to override attribute in superclass with property in subclass? #9190

Closed
fabioz opened this issue Jul 22, 2020 · 1 comment
Closed

Comments

@fabioz
Copy link

fabioz commented Jul 22, 2020

In the code:

class A:
    def __init__(self):
        self.foo: str = "foo"


class B(A):
    @property
    def foo(self) -> str:
        return self._foo

    @foo.setter
    def foo(self, foo: str) -> None:
        self._foo = foo

    @foo.deleter
    def foo(self) -> None:
        del self._foo


print(B().foo)

I get the errors below in the @property and def foo(self) -> str:

snippet.py:7:6: error: Signature of "foo" incompatible with supertype "A"
snippet.py:8:5: error: Signature of "foo" incompatible with supertype "A"

Note: something similar was reported (#8185) and it was closed saying that you can't override a writeable attribute with a read-only one, but in the case, it does have the setter (I even added a deleter to check if it made a difference, but it didn't).

So, is there any way to convert from an attribute in a superclass to a property keeping type-safety?

@fabioz
Copy link
Author

fabioz commented Jul 22, 2020

Actually, this seems to be a duplicate of: #4125, so, closing this one.

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

1 participant