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

Abstract settable properties are not fully supported #13649

Open
sobolevn opened this issue Sep 12, 2022 · 2 comments
Open

Abstract settable properties are not fully supported #13649

sobolevn opened this issue Sep 12, 2022 · 2 comments
Labels
bug mypy got something wrong

Comments

@sobolevn
Copy link
Member

sobolevn commented Sep 12, 2022

Python docs say that properties can have individual abstract parts: https://docs.python.org/3/library/abc.html#abc.abstractproperty

So, let's see what mypy thinks about it.

abstract getter

import abc

class Base(abc.ABC):
    @property
    @abc.abstractmethod
    def x(self) -> int: ...

class Child(Base):
    ...

c = Child()  # error: Cannot instantiate abstract class "Child" with abstract attribute "x"

It is supported ✅

abstract setter

import abc

class Base(abc.ABC):
    @property
    def x(self) -> int: ...

    @x.setter
    @abc.abstractmethod
    def x(self, arg: int) -> None: ...

class Child(Base):
    ...

c = Child()
c.x = 2
reveal_type(c)  # Revealed type is "ex.Child"
# Runtime:
# TypeError: Can't instantiate abstract class Child with abstract method x

Not supported 🚫

Should instead say: # error: Cannot instantiate abstract class "Child" with abstract attribute "x"
Or even better with abstract property setter "x"

abstract getter and setter

import abc

class Base(abc.ABC):
    @property
    @abc.abstractmethod
    def x(self) -> int: ...

    @x.setter
    @abc.abstractmethod
    def x(self, arg: int) -> None: ...

class Child(Base):
    ...

c = Child()  # E: Cannot instantiate abstract class "Child" with abstract attribute "x"

However, this does not fully cover this case:

import abc

class Base(abc.ABC):
    @property
    @abc.abstractmethod
    def x(self) -> int: ...

    @x.setter
    @abc.abstractmethod
    def x(self, arg: int) -> None: ...

class Child(Base):
    @property  # E: Read-only property cannot override read-write property
    def x(self) -> int: ...

Child()  # ok

We can add additional note about x.setter being abstract. Or we can keep this as is.

Related:

@sobolevn sobolevn added the bug mypy got something wrong label Sep 12, 2022
@warsaw
Copy link
Member

warsaw commented Jun 8, 2023

It's not clear to me that issue #4165 is actually resolved. I'm still running into a problem with abstract properties. Was #4165 closed because this issue intends to fix that?

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Jun 8, 2023

I just opened a PR #15395 that will fix #4165 for deleters. However, as you can see in the test case I added it doesn't fix this false negative issue, either for setters or deleters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants