2Tests the evaluation of calls to constructors when there is a custom
3metaclass with a __call__ method.
6from typing import NoReturn, Self, TypeVar, assert_type
8# Specification: https://typing.readthedocs.io/en/latest/spec/constructors.html#constructor-calls
10# Metaclass __call__ method: https://typing.readthedocs.io/en/latest/spec/constructors.html#metaclass-call-method
14 def __call__(cls, *args, **kwargs) -> NoReturn:
15 raise TypeError("Cannot instantiate class")
18class Class1(metaclass=Meta1):
19 def __new__(cls, x: int) -> Self:
20 return super().__new__(cls)
23# This needs to be in a separate scope, because some type checkers might mark
24# the statements after it as unreachable.
26 assert_type(Class1(), NoReturn)
30 def __call__(cls, *args, **kwargs) -> "int | Meta2":
34class Class2(metaclass=Meta2):
35 def __new__(cls, x: int) -> Self:
36 return super().__new__(cls)
39assert_type(Class2(), int | Meta2)
Unexpected error
[type-assertion-failure] Type `int | Meta2` does not match asserted type `Class2`
[missing-argument] No argument provided for required parameter `x` of function `__new__`
45 def __call__(cls: type[T], *args, **kwargs) -> T:
46 return super().__call__(cls, *args, **kwargs)
Unexpected error
[invalid-super-argument] `type[T@__call__]` is not an instance or subclass of `<class 'Meta3'>` in `super(<class 'Meta3'>, type[T@__call__])` call
49class Class3(metaclass=Meta3):
50 def __new__(cls, x: int) -> Self:
51 return super().__new__(cls)
54Class3() # E: Missing argument for 'x' parameter in __new__
[missing-argument] No argument provided for required parameter `x` of function `__new__`
55assert_type(Class3(1), Class3)
59 def __call__(cls, *args, **kwargs):
60 return super().__call__(cls, *args, **kwargs)
63class Class4(metaclass=Meta4):
64 def __new__(cls, x: int) -> Self:
65 return super().__new__(cls)
68Class4() # E: Missing argument for 'x' parameter in __new__
[missing-argument] No argument provided for required parameter `x` of function `__new__`
69assert_type(Class4(1), Class4)