-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Access abstract class property from class method #8532
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
Comments
I would like to try and tackle this issue since it is labelled as a good first issue. @msullivan do you have an idea of where I should start looking ? |
Probably in |
Hi @msullivan @olirice . I am a newbie to open source. Please excuse if you perceive this as a dumb question. Saw this issue as it was marked as a good first issue. I ran the given code in python 3.6.9. It is not even working in normal python let alone mypy. Can you please clarify whether this was supposed to be a mypy specific bug or could be in python3 itself? |
@Sanchit-Trivedi thank you for catching that. I boiled the example down a little too far! Here is an updated minimal reproducable example: from abc import ABC, abstractmethod
class MyBase(ABC):
@property
@classmethod
@abstractmethod
def name(cls) -> str:
raise NotImplementedError()
def name_length_from_method(self) -> int:
return len(self.name) # succeeds
@classmethod
def name_length_from_class_method(cls) -> int:
return len(
cls.name
) # error: Argument 1 to "len" has incompatible type "Callable[[], str]" The valid usage of that abstract base class could be # Usage Example
class MyClass(MyBase):
name = "some_name"
print(MyClass().name_length_from_method()) # prints 9
print(MyClass.name_length_from_class_method()) # prints 9 |
@olirice are you sure about the usage of the abstract base class ? In the ABC you define After doing a bit of research, it actually seems that combining the |
@ErwanDL you're right. There are a bunch of examples floating around that stack abstractmethod, classmethod, and property decorators, but it doesn't appear to be officially supported. Thanks for your help and sorry this wasn't the good first issue you were looking for! |
@olirice no problem, and don't worry there are plenty of issues on this repo for me to dive into :) |
@olirice will have to look up other first issues to fix I guess. Can you suggest any? |
@Sanchit-Trivedi thank you as well! |
In 3.9 it's supported: https://docs.python.org/3.9/library/functions.html#classmethod |
I believe this is still an issue; as @rominf mentioned, this is officially supported as of 3.9 and I am still receiving spurious errors suggesting that the property is of type |
Can confirm this is still an issue in Python 3.9 and mypy 0.910 |
I have the same issue with mypy 0.910 and Python 3.9.7. It would be great if this could be reopened (and fixed of course). |
Feel free to follow #11619 , note that CPython might rollback some of the related changes in 3.9 |
When accessing a class property from a class method mypy does not respect the property decorator.
Steps to reproduce:
The text was updated successfully, but these errors were encountered: