Skip to content

Commit 97684e2

Browse files
committed
Add missing map_type_from_supertype when checking attr override with property
1 parent 106f714 commit 97684e2

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

mypy/checker.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,6 +2095,13 @@ def check_method_override_for_base_with_name(
20952095
if original_node and is_property(original_node):
20962096
original_type = get_property_type(original_type)
20972097

2098+
if isinstance(original_node, Var):
2099+
expanded_type = map_type_from_supertype(original_type, defn.info, base)
2100+
expanded_type = expand_self_type(
2101+
original_node, expanded_type, fill_typevars(defn.info)
2102+
)
2103+
original_type = get_proper_type(expanded_type)
2104+
20982105
if is_property(defn):
20992106
inner: FunctionLike | None
21002107
if isinstance(typ, FunctionLike):

test-data/unit/check-generic-subtyping.test

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,3 +1065,66 @@ class F(E[T_co], Generic[T_co]): ... # E: Variance of TypeVar "T_co" incompatib
10651065

10661066
class G(Generic[T]): ...
10671067
class H(G[T_contra], Generic[T_contra]): ... # E: Variance of TypeVar "T_contra" incompatible with variance in parent type
1068+
1069+
[case testParameterizedGenericOverrideWithProperty]
1070+
from typing import TypeVar, Generic
1071+
1072+
T = TypeVar("T")
1073+
1074+
class A(Generic[T]):
1075+
def __init__(self, val: T):
1076+
self.member: T = val
1077+
1078+
class B(A[str]):
1079+
member: str
1080+
1081+
class GoodPropertyOverride(A[str]):
1082+
@property
1083+
def member(self) -> str: ...
1084+
1085+
@member.setter
1086+
def member(self, val: str): ...
1087+
1088+
class BadPropertyOverride(A[str]):
1089+
@property # E: Signature of "member" incompatible with supertype "A" \
1090+
# N: Superclass: \
1091+
# N: str \
1092+
# N: Subclass: \
1093+
# N: int
1094+
def member(self) -> int: ...
1095+
1096+
@member.setter
1097+
def member(self, val: int): ...
1098+
1099+
class BadGenericPropertyOverride(A[str], Generic[T]):
1100+
@property # E: Signature of "member" incompatible with supertype "A" \
1101+
# N: Superclass: \
1102+
# N: str \
1103+
# N: Subclass: \
1104+
# N: T
1105+
def member(self) -> T: ...
1106+
1107+
@member.setter
1108+
def member(self, val: T): ...
1109+
[builtins fixtures/property.pyi]
1110+
1111+
[case testParameterizedGenericOverrideWithSelfProperty]
1112+
from typing import TypeVar, Generic
1113+
from typing_extensions import Self
1114+
1115+
T = TypeVar("T")
1116+
1117+
class A(Generic[T]):
1118+
def __init__(self, val: T):
1119+
self.member: T = val
1120+
1121+
class B(A["B"]):
1122+
member: Self
1123+
1124+
class GoodPropertyOverride(A["GoodPropertyOverride"]):
1125+
@property
1126+
def member(self) -> Self: ...
1127+
1128+
@member.setter
1129+
def member(self, val: Self): ...
1130+
[builtins fixtures/property.pyi]

0 commit comments

Comments
 (0)