2Tests the handling of typing.Never and typing.NoReturn.
5# Specification: https://typing.readthedocs.io/en/latest/spec/special-types.html#noreturn
8from typing import Any, Generic, Never, NoReturn, TypeVar
11T_co = TypeVar("T_co", covariant=True)
15def stop() -> NoReturn:
16 raise RuntimeError("no way")
19def func1(x: int) -> NoReturn: # E: implicitly returns None
[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `Never`
24# > The checkers will also recognize that the code after calls to such functions
25# > is unreachable and will behave accordingly.
27# No error on implicit None return after stop()
28def func2(x: int) -> int:
34# The spec previously said that NoReturn is only valid in a function return type,
35# but this was removed and it should now be accepted in all of these contexts:
39 a: NoReturn, b: list[NoReturn]
50def func5() -> list[NoReturn]:
58 def __init__(self, x: NoReturn, y: list[NoReturn]) -> None:
63# Never is compatible with all types.
69 v3: list[str] = a # OK
72# Never is a synonym for NoReturn.
75def func7(x: int) -> Never:
79# Other types are not compatible with Never except for Never (and Any).
82def func8(a: Never, b: Any, c: list[Never]):
[invalid-assignment] Object of type `list[Never]` is not assignable to `list[int]`
86 v4: Never = stop() # OK
89class ClassB(Generic[T_co]):
93def func9(x: U) -> ClassB[U]:
94 # Never is a bottom type and therefore compatible with a covariant type variable.
95 return ClassB[Never]() # OK
98class ClassC(Generic[T]):
102def func10(x: U) -> ClassC[U]:
103 # Never is not compatible in an invariant context.
104 return ClassC[Never]() # E
[invalid-return-type] Return type does not match returned value: expected `ClassC[U@func10]`, found `ClassC[Never]`