Skip to content

Commit 41db9a0

Browse files
authored
Pass is_classmethod to bind_self() also for superype (#7491)
Fix a regression caused by #7474 The original PR correctly added `is_classmethod=...` in `bind_self()` for supertype, but I didn't notice the same problem (flag not passed) was also present for _subtype_. The fix is trivial.
1 parent 2bdbacf commit 41db9a0

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

mypy/checker.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1454,14 +1454,17 @@ def check_method_override_for_base_with_name(
14541454
if isinstance(defn, (FuncDef, OverloadedFuncDef)):
14551455
typ = self.function_type(defn) # type: Type
14561456
override_class_or_static = defn.is_class or defn.is_static
1457+
override_class = defn.is_class
14571458
else:
14581459
assert defn.var.is_ready
14591460
assert defn.var.type is not None
14601461
typ = defn.var.type
14611462
override_class_or_static = defn.func.is_class or defn.func.is_static
1463+
override_class = defn.func.is_class
14621464
typ = get_proper_type(typ)
14631465
if isinstance(typ, FunctionLike) and not is_static(context):
1464-
typ = bind_self(typ, self.scope.active_self_type())
1466+
typ = bind_self(typ, self.scope.active_self_type(),
1467+
is_classmethod=override_class)
14651468
# Map the overridden method type to subtype context so that
14661469
# it can be checked for compatibility.
14671470
original_type = get_proper_type(base_attr.type)

test-data/unit/check-classes.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6141,3 +6141,18 @@ class C(B[int, T]):
61416141
def __init__(self) -> None:
61426142
# TODO: error message could be better.
61436143
self.x: Tuple[str, T] # E: Incompatible types in assignment (expression has type "Tuple[str, T]", base class "A" defined the type as "Tuple[int, T]")
6144+
6145+
[case testOverrideGenericSelfClassMethod]
6146+
from typing import Generic, TypeVar, Type, List
6147+
6148+
T = TypeVar('T', bound=A)
6149+
6150+
class A:
6151+
@classmethod
6152+
def meth(cls: Type[T]) -> List[T]: ...
6153+
6154+
class B(A):
6155+
@classmethod
6156+
def meth(cls: Type[T]) -> List[T]: ...
6157+
6158+
[builtins fixtures/isinstancelist.pyi]

test-data/unit/fixtures/isinstancelist.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class type:
1111

1212
class function: pass
1313
class ellipsis: pass
14+
class classmethod: pass
1415

1516
def isinstance(x: object, t: Union[type, Tuple]) -> bool: pass
1617
def issubclass(x: object, t: Union[type, Tuple]) -> bool: pass

0 commit comments

Comments
 (0)