-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathasync104.py
More file actions
188 lines (163 loc) · 3.75 KB
/
async104.py
File metadata and controls
188 lines (163 loc) · 3.75 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# ARG --enable=ASYNC103,ASYNC104
import sys
try:
...
# raise different exception
except BaseException:
raise ValueError() # error: 4
try:
...
except BaseException as e:
raise ValueError() from e # error: 4
try:
...
except BaseException as e:
# see https://github.com/python-trio/flake8-async/pull/8#discussion_r932737341
raise BaseException() from e # error: 4
# fmt: off
# TODO: Black 23.1.0 moves the long comments around a bit.
# nested try
# in theory safe if the try, and all excepts raises - and there's a bare except.
# But is a very weird pattern that we don't handle.
try:
...
except BaseException as e: # ASYNC103_trio: 7, "BaseException"
try:
raise e
except ValueError:
raise e
except:
raise e # though sometimes okay, error: 8
try:
...
except BaseException: # safe
try:
...
finally:
raise
# check that nested non-critical exceptions are ignored
try:
...
except BaseException:
try:
...
except ValueError:
... # safe
raise
# check that name isn't lost
try:
...
except BaseException as e:
try:
...
except BaseException as f:
raise f
raise e
# don't bypass raise by raising from nested except
try:
...
except BaseException as e:
try:
...
except ValueError as g:
raise g # error: 8
except BaseException as h:
raise h # error? currently treated as safe
raise e
# avoid re-raise by raising parent exception
try:
...
except ValueError as e:
try:
...
except BaseException:
raise e # error: 8
def foo():
if True: # for code coverage
return
# check for avoiding re-raise by returning from function
def foo2():
try:
...
except BaseException: # ASYNC103_trio: 11, "BaseException"
return # error: 8
# check that we properly iterate over all nodes in try
try:
...
except BaseException: # ASYNC103_trio: 11, "BaseException"
try:
return # error: 12
except ValueError:
return # error: 12
else:
return # type: ignore[unreachable] # error: 12
finally:
if sys.version_info < (3, 14):
return # error: 12
# don't avoid re-raise with continue/break
def foo3():
while True:
try:
...
except BaseException:
if True:
continue # error: 16
raise
def foo4():
while True:
try:
...
except BaseException:
if True:
break # error: 16
raise
try:
...
except BaseException: # safe
while True:
break
raise
try:
...
except BaseException: # safe
while True:
continue
raise
# check for avoiding re-raise by yielding from function
def foo_yield():
if True: # for code coverage
yield 1
try:
...
except BaseException:
yield 1 # error: 8
raise
# check that we properly iterate over all nodes in try
try:
...
except BaseException:
try:
yield 1 # error: 12
except ValueError:
yield 1 # error: 12
else:
yield 1 # error: 12
finally:
yield 1 # error: 12
raise
# issue #106
# don't warn on bare / BaseException when cancelled have been handled in a previous except
def foo_cancelled_handled():
try:
...
except BaseException:
raise
except:
return # would otherwise error
def foo_cancelled_not_handled():
try:
...
except BaseException: # ASYNC103_trio: 11, "BaseException"
return # ASYNC104: 8
except:
return # would otherwise error