Skip to content

Commit 64d3a0b

Browse files
committed
Access aliases correctly
1 parent 152b257 commit 64d3a0b

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

mypy/checkmember.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,13 +1262,17 @@ def analyze_class_attribute_access(
12621262
erase_vars.add(itype.type.self_type)
12631263
t = erase_typevars(t, {tv.id for tv in erase_vars})
12641264

1265-
if (
1266-
isinstance(node.node, Decorator)
1267-
and node.node.func.is_property
1268-
or isinstance(node.node, OverloadedFuncDef)
1269-
and node.node.is_property
1270-
):
1271-
property_type = mx.chk.get_property_instance(node.node)
1265+
if isinstance(node.node, Var):
1266+
if isinstance(node.node.type, Overloaded):
1267+
prop_candidate = node.node.type.items[0].definition
1268+
elif isinstance(node.node.type, CallableType):
1269+
prop_candidate = node.node.type.definition
1270+
else:
1271+
prop_candidate = node.node
1272+
else:
1273+
prop_candidate = node.node
1274+
if isinstance(prop_candidate, (Decorator, OverloadedFuncDef)):
1275+
property_type = mx.chk.get_property_instance(prop_candidate)
12721276
if property_type is not None:
12731277
return property_type
12741278

test-data/unit/check-classes.test

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9287,14 +9287,38 @@ class B:
92879287

92889288
class C(B):
92899289
prop = A.prop
9290-
prop_t = AT.prop # E: Incompatible types in assignment (expression has type "C", base class "B" defined the type as "str")
9290+
prop_t = AT.prop
92919291

92929292
reveal_type(C().prop) # N: Revealed type is "builtins.str"
92939293
C().prop = "no" # E: Invalid self argument "C" to attribute function "prop" with type "Callable[[A, str], None]"
92949294
reveal_type(C().prop_t) # N: Revealed type is "__main__.C"
92959295
C().prop_t = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "list[C]")
92969296
[builtins fixtures/property.pyi]
92979297

9298+
[case testPropertyAliasClassAccess]
9299+
class A:
9300+
@property
9301+
def ro(self) -> int: ...
9302+
9303+
@property
9304+
def rw(self) -> int: ...
9305+
@rw.setter
9306+
def rw(self, val: int) -> None: ...
9307+
9308+
class B:
9309+
ro = A.ro
9310+
rw = A.rw
9311+
9312+
reveal_type(A.ro) # N: Revealed type is "builtins.property"
9313+
reveal_type(A.rw) # N: Revealed type is "builtins.property"
9314+
reveal_type(B.ro) # N: Revealed type is "builtins.property"
9315+
reveal_type(B.rw) # N: Revealed type is "builtins.property"
9316+
reveal_type(B().ro) # E: Invalid self argument "B" to attribute function "ro" with type "Callable[[A], int]" \
9317+
# N: Revealed type is "builtins.int"
9318+
reveal_type(B().rw) # E: Invalid self argument "B" to attribute function "rw" with type "Callable[[A], int]" \
9319+
# N: Revealed type is "builtins.int"
9320+
[builtins fixtures/property.pyi]
9321+
92989322
[case testClassEqDecoratedAbstractNote]
92999323
from abc import abstractmethod
93009324

0 commit comments

Comments
 (0)