Skip to content

Commit

Permalink
Pass is_classmethod to bind_self() also for superype (#7491)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ilevkivskyi committed Sep 10, 2019
1 parent 2bdbacf commit 41db9a0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion mypy/checker.py
Expand Up @@ -1454,14 +1454,17 @@ def check_method_override_for_base_with_name(
if isinstance(defn, (FuncDef, OverloadedFuncDef)):
typ = self.function_type(defn) # type: Type
override_class_or_static = defn.is_class or defn.is_static
override_class = defn.is_class
else:
assert defn.var.is_ready
assert defn.var.type is not None
typ = defn.var.type
override_class_or_static = defn.func.is_class or defn.func.is_static
override_class = defn.func.is_class
typ = get_proper_type(typ)
if isinstance(typ, FunctionLike) and not is_static(context):
typ = bind_self(typ, self.scope.active_self_type())
typ = bind_self(typ, self.scope.active_self_type(),
is_classmethod=override_class)
# Map the overridden method type to subtype context so that
# it can be checked for compatibility.
original_type = get_proper_type(base_attr.type)
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-classes.test
Expand Up @@ -6141,3 +6141,18 @@ class C(B[int, T]):
def __init__(self) -> None:
# TODO: error message could be better.
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]")

[case testOverrideGenericSelfClassMethod]
from typing import Generic, TypeVar, Type, List

T = TypeVar('T', bound=A)

class A:
@classmethod
def meth(cls: Type[T]) -> List[T]: ...

class B(A):
@classmethod
def meth(cls: Type[T]) -> List[T]: ...

[builtins fixtures/isinstancelist.pyi]
1 change: 1 addition & 0 deletions test-data/unit/fixtures/isinstancelist.pyi
Expand Up @@ -11,6 +11,7 @@ class type:

class function: pass
class ellipsis: pass
class classmethod: pass

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

0 comments on commit 41db9a0

Please sign in to comment.