|
17 | 17 | analyze_class_attribute_access, |
18 | 18 | analyze_instance_member_access, |
19 | 19 | analyze_member_access, |
| 20 | + is_instance_var, |
20 | 21 | ) |
21 | 22 | from mypy.checkpattern import PatternChecker |
22 | 23 | from mypy.constraints import SUPERTYPE_OF |
@@ -3408,10 +3409,17 @@ def get_variable_type_context(self, inferred: Var, rvalue: Expression) -> Type | |
3408 | 3409 | type_contexts = [] |
3409 | 3410 | if inferred.info: |
3410 | 3411 | for base in inferred.info.mro[1:]: |
| 3412 | + if inferred.name not in base.names: |
| 3413 | + continue |
3411 | 3414 | # For inference within class body, get supertype attribute as it would look on |
3412 | | - # a class object for lambdas overriding methods. |
3413 | | - base_type, base_node = self.lvalue_type_from_base( |
3414 | | - inferred, base, is_class=isinstance(rvalue, LambdaExpr) |
| 3415 | + # a class object for lambdas overriding methods, etc. |
| 3416 | + base_node = base.names[inferred.name].node |
| 3417 | + base_type, _ = self.lvalue_type_from_base( |
| 3418 | + inferred, |
| 3419 | + base, |
| 3420 | + is_class=is_method(base_node) |
| 3421 | + or isinstance(base_node, Var) |
| 3422 | + and not is_instance_var(base_node), |
3415 | 3423 | ) |
3416 | 3424 | if ( |
3417 | 3425 | base_type |
@@ -9167,3 +9175,11 @@ def is_typeddict_type_context(lvalue_type: Type | None) -> bool: |
9167 | 9175 | return False |
9168 | 9176 | lvalue_proper = get_proper_type(lvalue_type) |
9169 | 9177 | return isinstance(lvalue_proper, TypedDictType) |
| 9178 | + |
| 9179 | + |
| 9180 | +def is_method(node: SymbolNode | None) -> bool: |
| 9181 | + if isinstance(node, OverloadedFuncDef): |
| 9182 | + return not node.is_property |
| 9183 | + if isinstance(node, Decorator): |
| 9184 | + return not node.var.is_property |
| 9185 | + return isinstance(node, FuncDef) |
0 commit comments