-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathspecialtypes_never.py
More file actions
104 lines (63 loc) · 1.97 KB
/
specialtypes_never.py
File metadata and controls
104 lines (63 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""
Tests the handling of typing.Never and typing.NoReturn.
"""
# Specification: https://typing.readthedocs.io/en/latest/spec/special-types.html#noreturn
import sys
from typing import Any, Generic, Never, NoReturn, TypeVar
T = TypeVar("T")
T_co = TypeVar("T_co", covariant=True)
U = TypeVar("U")
def stop() -> NoReturn:
raise RuntimeError("no way")
def func1(x: int) -> NoReturn: # E: implicitly returns None
if x != 0:
sys.exit(1)
# > The checkers will also recognize that the code after calls to such functions
# > is unreachable and will behave accordingly.
# No error on implicit None return after stop()
def func2(x: int) -> int:
if x > 0:
return x
stop()
# The spec previously said that NoReturn is only valid in a function return type,
# but this was removed and it should now be accepted in all of these contexts:
def func3(
a: NoReturn, b: list[NoReturn]
) -> None:
c: NoReturn = a
def func4(
a: list[NoReturn],
) -> None:
c: list[NoReturn] = a
def func5() -> list[NoReturn]:
return []
class ClassA:
x: NoReturn
y: list[NoReturn]
def __init__(self, x: NoReturn, y: list[NoReturn]) -> None:
self.x = x
self.y = y
# Never is compatible with all types.
def func6(a: Never):
v1: int = a # OK
v2: str = a # OK
v3: list[str] = a # OK
# Never is a synonym for NoReturn.
def func7(x: int) -> Never:
sys.exit(1)
# Other types are not compatible with Never except for Never (and Any).
def func8(a: Never, b: Any, c: list[Never]):
v1: Never = a # OK
v2: Never = b # OK
v3: list[int] = c # E
v4: Never = stop() # OK
class ClassB(Generic[T_co]):
pass
def func9(x: U) -> ClassB[U]:
# Never is a bottom type and therefore compatible with a covariant type variable.
return ClassB[Never]() # OK
class ClassC(Generic[T]):
pass
def func10(x: U) -> ClassC[U]:
# Never is not compatible in an invariant context.
return ClassC[Never]() # E