Skip to content

Mypy disallows overriding a generic type attribute with a property #18189

Closed
@mmdanggg2

Description

@mmdanggg2

Bug Report

If I have a generic class with an attribute of a generic type, when creating a concrete subclass, overriding that attribute with a correctly typed property raises an error. Similar to #4125

To Reproduce

from typing import TypeVar, Generic

T = TypeVar("T")

class A(Generic[T]):
    def __init__(self, val: T):
        self.member: T = val

class B(A[str]):
    @property
    def member(self) -> str:
        return "bar"
    
    @member.setter
    def member(self, val: str):
        pass

Actual Behavior

property_issue.py:10: error: Signature of "member" incompatible with supertype "A"  [override]
property_issue.py:10: note:      Superclass:
property_issue.py:10: note:          T
property_issue.py:10: note:      Subclass:
property_issue.py:10: note:          str

Your Environment

  • Mypy version used: 1.9.0
  • Mypy command-line flags: -
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.9.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions