diff --git a/mypy/nodes.py b/mypy/nodes.py index 9fdf871ec87b..236cb92c9a0f 100644 --- a/mypy/nodes.py +++ b/mypy/nodes.py @@ -439,7 +439,7 @@ class FuncBase(Node): 'unanalyzed_type', 'info', 'is_property', - 'is_class', # Uses "@classmethod" + 'is_class', # Uses "@classmethod" (explicit or implicit) 'is_static', # Uses "@staticmethod" 'is_final', # Uses "@final" '_fullname', diff --git a/mypy/semanal.py b/mypy/semanal.py index 48fa6c21d2f0..2664d40ddb2f 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -611,10 +611,12 @@ def prepare_method_signature(self, func: FuncDef, info: TypeInfo) -> None: if not func.arguments: self.fail('Method must have at least one argument', func) elif isinstance(functype, CallableType): + if func.name() == '__init_subclass__': + func.is_class = True self_type = functype.arg_types[0] if isinstance(self_type, AnyType): leading_type = fill_typevars(info) # type: Type - if func.is_class or func.name() in ('__new__', '__init_subclass__'): + if func.is_class or func.name() == '__new__': leading_type = self.class_type(leading_type) func.type = replace_implicit_first_type(functype, leading_type) diff --git a/test-data/unit/check-super.test b/test-data/unit/check-super.test index 54ee5841fe10..223bfbba98c9 100644 --- a/test-data/unit/check-super.test +++ b/test-data/unit/check-super.test @@ -252,6 +252,12 @@ def g(b: B) -> None: super(B, b).f('42') super(B, b).f(42) # E: Argument 1 to "f" of "A" has incompatible type "int"; expected "str" +[case testSuperInInitSubclass] +class A: + def __init_subclass__(cls) -> None: + super().__init_subclass__() +[builtins fixtures/__init_subclass__.pyi] + -- Invalid uses of super() -- ----------------------- diff --git a/test-data/unit/fixtures/__init_subclass__.pyi b/test-data/unit/fixtures/__init_subclass__.pyi new file mode 100644 index 000000000000..79fd04fd964e --- /dev/null +++ b/test-data/unit/fixtures/__init_subclass__.pyi @@ -0,0 +1,11 @@ +# builtins stub with object.__init_subclass__ + +class object: + def __init_subclass__(cls) -> None: pass + +class type: pass + +class int: pass +class bool: pass +class str: pass +class function: pass