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 properties report inconsistent typing #4364

Closed
blink1073 opened this issue Dec 20, 2022 · 2 comments
Closed

Abstract properties report inconsistent typing #4364

blink1073 opened this issue Dec 20, 2022 · 2 comments
Labels
as designed Not a bug, working as intended

Comments

@blink1073
Copy link

Note: if you are reporting a wrong signature of a function or a class in the standard library, then the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues.

Describe the bug
This is related to #2601 but applies to a @property in the abstract base class.

To Reproduce

Create a base class that has an abstract property:

import abs
from typing import Any

class Foo(abc.ABC):
    @property
    @abc.abstractmethod
    def sub_type(self) -> Any:
        pass

class Bar(Foo):
    sub_type = None
test.py:12:16 - error: Expression of type "None" cannot be assigned to declared type "property"
    Type "None" cannot be assigned to type "property" (reportGeneralTypeIssues)
1 error, 0 warnings, 0 informations

Expected behavior
The property is allowed to be overridden.

Additional context
The code does not error when run against mypy==0.990.

@erictraut
Copy link
Collaborator

This is intended behavior. The abstract class indicates that the symbol sub_type must be a read-only property. In the subclass, you are attempting to implement it using a read/write attribute. The semantics for properties and attributes are similar, but they differ in important ways. If your abstract base class indicates that the type of a symbol is a property, a subclass must implement this symbol as a property for it to satisfy the base class requirements.

The following will eliminate the type violation in your code:

class Bar(Foo):
    @property
    def sub_type(self):
      return None

@erictraut erictraut added the as designed Not a bug, working as intended label Dec 20, 2022
@blink1073
Copy link
Author

Makes sense, thanks @erictraut!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as designed Not a bug, working as intended
Projects
None yet
Development

No branches or pull requests

2 participants