@@ -379,7 +379,7 @@ class B(A):
379379[case testOverride__init_subclass__WithDifferentSignature]
380380class A:
381381 def __init_subclass__(cls, x: int) -> None: pass
382- class B(A):
382+ class B(A): # E: Too few arguments for "__init_subclass__" of "A"
383383 def __init_subclass__(cls) -> None: pass
384384
385385[case testOverrideWithDecorator]
@@ -6164,6 +6164,102 @@ class C(B[int, T]):
61646164 # TODO: error message could be better.
61656165 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]")
61666166
6167+ [case testInitSubclassWrongType]
6168+ class Base:
6169+ default_name: str
6170+
6171+ def __init_subclass__(cls, default_name: str):
6172+ super().__init_subclass__()
6173+ cls.default_name = default_name
6174+ return
6175+
6176+ class Child(Base, default_name=5): # E: Argument "default_name" to "__init_subclass__" of "Base" has incompatible type "int"; expected "str"
6177+ pass
6178+ [builtins fixtures/object_with_init_subclass.pyi]
6179+
6180+ [case testInitSubclassTooFewArgs]
6181+ class Base:
6182+ default_name: str
6183+
6184+ def __init_subclass__(cls, default_name: str, **kwargs):
6185+ super().__init_subclass__()
6186+ cls.default_name = default_name
6187+ return
6188+
6189+ class Child(Base): # E: Too few arguments for "__init_subclass__" of "Base"
6190+ pass
6191+ [builtins fixtures/object_with_init_subclass.pyi]
6192+
6193+ [case testInitSubclassTooFewArgs2]
6194+ class Base:
6195+ default_name: str
6196+
6197+ def __init_subclass__(cls, default_name: str, thing: int):
6198+ super().__init_subclass__()
6199+ cls.default_name = default_name
6200+ return
6201+ # TODO implement this, so that no error is raised?
6202+ d = {"default_name": "abc", "thing": 0}
6203+ class Child(Base, **d): # E: Too few arguments for "__init_subclass__" of "Base"
6204+ pass
6205+ [builtins fixtures/object_with_init_subclass.pyi]
6206+
6207+ [case testInitSubclassOK]
6208+ class Base:
6209+ default_name: str
6210+ thing: int
6211+
6212+ def __init_subclass__(cls, default_name: str, thing:int, **kwargs):
6213+ super().__init_subclass__()
6214+ cls.default_name = default_name
6215+ return
6216+
6217+ class Child(Base, thing=5, default_name=""):
6218+ pass
6219+ [builtins fixtures/object_with_init_subclass.pyi]
6220+
6221+ [case testInitSubclassWithMetaclassOK]
6222+ class Base(type):
6223+ thing: int
6224+
6225+ def __init_subclass__(cls, thing: int):
6226+ cls.thing = thing
6227+
6228+ class Child(Base, metaclass=Base, thing=0):
6229+ pass
6230+
6231+ [case testTooManyArgsForObject]
6232+ class A(thing=5):
6233+ pass
6234+ [out]
6235+ main:1: error: Unexpected keyword argument "thing" for "__init_subclass__" of "object"
6236+ tmp/builtins.pyi:5: note: "__init_subclass__" of "object" defined here
6237+ [builtins fixtures/object_with_init_subclass.pyi]
6238+
6239+ [case testInitSubclassWithImports]
6240+ from init_subclass.a import Base
6241+ class Child(Base, thing=5): # E: Missing positional arguments "default_name", "kwargs" in call to "__init_subclass__" of "Base"
6242+ pass
6243+ [file init_subclass/a.py]
6244+ class Base:
6245+ default_name: str
6246+ thing: int
6247+
6248+ def __init_subclass__(cls, default_name: str, thing:int, **kwargs):
6249+ pass
6250+ [file init_subclass/__init__.py]
6251+ [builtins fixtures/object_with_init_subclass.pyi]
6252+
6253+ [case testInitSubclassWithImportsOK]
6254+ from init_subclass.a import MidBase
6255+ class Main(MidBase, test=True): pass
6256+ [file init_subclass/a.py]
6257+ class Base:
6258+ def __init_subclass__(cls, **kwargs) -> None: pass
6259+ class MidBase(Base): pass
6260+ [file init_subclass/__init__.py]
6261+ [builtins fixtures/object_with_init_subclass.pyi]
6262+
61676263[case testOverrideGenericSelfClassMethod]
61686264from typing import Generic, TypeVar, Type, List
61696265
0 commit comments