Skip to content

0.740 doesn't understand abstract properties #7760

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

Closed
hukkin opened this issue Oct 21, 2019 · 2 comments
Closed

0.740 doesn't understand abstract properties #7760

hukkin opened this issue Oct 21, 2019 · 2 comments

Comments

@hukkin
Copy link
Contributor

hukkin commented Oct 21, 2019

Running mypy 0.740 with check_untyped_defs = True gives a false positive
mypy_bug.py:12: error: "Callable[[AbstractBase], Dict[Any, Any]]" has no attribute "values"
when running against the code below:

from abc import ABC, abstractmethod


class AbstractBase(ABC):
    @property
    @abstractmethod
    def SOME_DICT(self) -> dict:
        pass

    @classmethod
    def get_dict_values(cls):
        return cls.SOME_DICT.values()


class Implementation(AbstractBase):
    SOME_DICT = {"some": "data"}

Mypy thinks that the value of cls.SOME_DICT on line 12 is Callable[[AbstractBase], Dict[Any, Any]], even though it is a property, typehinted to be dict.

@ilevkivskyi
Copy link
Member

Mypy is correct and your code is unsafe (although the error message is a bit cryptic), properties should not be accessed on class objects. Consider:

class Implementation(AbstractBase):
     @property
     def SOME_DICT(self) -> dict:
         return {"some": "data"}

Implementation.det_dict_values()

This code fails with a traceback (similar to what mypy says):

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 8, in get_dict_values
AttributeError: 'property' object has no attribute 'values'

@hukkin
Copy link
Contributor Author

hukkin commented Oct 21, 2019

Thanks, understood. I guess my real issue then is Python language's lack of notation for abstract class attributes...

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