-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathunreachable1.py
More file actions
148 lines (102 loc) · 2.93 KB
/
Copy pathunreachable1.py
File metadata and controls
148 lines (102 loc) · 2.93 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# This sample tests the detection and reporting of unreachable code.
import os
import sys
from abc import abstractmethod
from typing import Any, Callable, NoReturn
def func1():
"""
Docstring
"""
raise NotImplementedError()
class Foo:
b: bool
@staticmethod
def method1():
"""
Docstring
"""
raise NotImplementedError("Not Implemented")
def method2(self, a: int):
"""
Docstring
"""
if a < 10 or self.b:
raise NotImplementedError()
@abstractmethod
def method3(self):
print(self.b)
raise RuntimeError()
def method4(self) -> None:
print(self.b)
raise RuntimeError()
def method5(self) -> NoReturn:
print(self.b)
raise RuntimeError()
def func2():
func1()
# This should not be marked unreachable because NotImplementedError
# is special-cased.
return 3
def func3(foo: Foo):
foo.method1()
return 3
def func4(foo: Foo):
foo.method2(2)
return 3
def func5(foo: Foo):
foo.method3()
return 3
def func6(foo: Foo):
foo.method4()
return 3
def func7(foo: Foo):
foo.method5()
# This should be marked as unreachable.
# If reportUnreachable is enabled, it should generate a diagnostic.
return 3
def func8() -> NoReturn:
raise NameError()
def func9():
func8()
# This should be marked unreachable.
# If reportUnreachable is enabled, it should generate a diagnostic.
return 3
def func9_1(noreturn_func: Callable[[], NoReturn], unknown_func, flag: bool):
callback = noreturn_func if flag else unknown_func
callback()
# This should not be marked unreachable because the call target includes Unknown.
return 3
def func9_2(noreturn_func: Callable[[], NoReturn], any_func: Any, flag: bool):
callback = noreturn_func if flag else any_func
callback()
# This should not be marked unreachable because the call target includes Any.
return 3
def func10():
e = OSError()
a1 = os.name == "nt" and None == e.errno
reveal_type(a1, expected_text="bool")
a2 = True and os.name == "nt"
reveal_type(a2, expected_text="bool")
if os.name == "nt":
# This should be marked unreachable.
b = e.errno
if sys.version_info >= (4, 0):
# This should be marked unreachable.
b = e.errno
return
# This should be marked unreachable.
# If reportUnreachable is enabled, it should generate a diagnostic.
b = e.errno
def func11(obj: str) -> list:
if isinstance(obj, str):
return []
else:
# This should be marked as unreachable.
# If reportUnreachable is enabled, it should generate a diagnostic.
return obj
def func12(obj: str) -> list:
if isinstance(obj, str):
return []
# This should be marked as unreachable.
# If reportUnreachable is enabled, it should generate a diagnostic.
return obj