Skip to content

Commit e218d7d

Browse files
committed
Refactor
1 parent 64d3a0b commit e218d7d

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

mypy/checker.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2448,10 +2448,22 @@ def check_method_override_for_base_with_name(
24482448
)
24492449
return False
24502450

2451-
def get_property_instance(self, method: Decorator | OverloadedFuncDef) -> Instance | None:
2451+
def get_property_instance(
2452+
self, method: Var | Decorator | OverloadedFuncDef
2453+
) -> Instance | None:
24522454
if method.type is None:
24532455
return None
2454-
deco = method if isinstance(method, Decorator) else method.items[0]
2456+
2457+
func: SymbolNode | None = method
2458+
if isinstance(method, Var):
2459+
mt = get_proper_type(method.type)
2460+
if isinstance(mt, Overloaded):
2461+
func = mt.items[0].definition
2462+
elif isinstance(mt, CallableType):
2463+
func = mt.definition
2464+
if not isinstance(func, (Decorator, OverloadedFuncDef)):
2465+
return None
2466+
deco = func if isinstance(func, Decorator) else func.items[0]
24552467
if not isinstance(deco, Decorator):
24562468
return None
24572469
property_deco_name = next(

mypy/checker_shared.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,9 @@ def get_precise_awaitable_type(self, typ: Type, local_errors: ErrorWatcher) -> T
275275
raise NotImplementedError
276276

277277
@abstractmethod
278-
def get_property_instance(self, method: Decorator | OverloadedFuncDef) -> Instance | None:
278+
def get_property_instance(
279+
self, method: Var | Decorator | OverloadedFuncDef
280+
) -> Instance | None:
279281
raise NotImplementedError
280282

281283
@abstractmethod

mypy/checkexpr.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,13 @@ def analyze_ref_expr(self, e: RefExpr, lvalue: bool = False) -> Type:
388388

389389
if isinstance(node, Var):
390390
# Variable reference.
391-
result = self.analyze_var_ref(node, e)
392-
if isinstance(result, PartialType):
393-
result = self.chk.handle_partial_var_type(result, lvalue, node, e)
391+
property_type = self.chk.get_property_instance(node)
392+
if property_type is not None:
393+
result = property_type
394+
else:
395+
result = self.analyze_var_ref(node, e)
396+
if isinstance(result, PartialType):
397+
result = self.chk.handle_partial_var_type(result, lvalue, node, e)
394398
elif isinstance(node, Decorator):
395399
property_type = self.chk.get_property_instance(node)
396400
if property_type is not None:

mypy/checkmember.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,17 +1262,8 @@ 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 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)
1265+
if isinstance(node.node, (Var, Decorator, OverloadedFuncDef)):
1266+
property_type = mx.chk.get_property_instance(node.node)
12761267
if property_type is not None:
12771268
return property_type
12781269

0 commit comments

Comments
 (0)