Skip to content

Commit 06f92b1

Browse files
authored
Merge pull request #176 from jakkdl/false_alarms
2 parents 47cc2de + 5bcb429 commit 06f92b1

File tree

5 files changed

+142
-3
lines changed

5 files changed

+142
-3
lines changed

flake8_trio/visitors/visitor100.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ def __init__(self, *args: Any, **kwargs: Any):
3636
self.node_dict: dict[cst.With, list[AttributeCall]] = {}
3737

3838
def checkpoint(self) -> None:
39-
if self.has_checkpoint_stack:
40-
self.has_checkpoint_stack[-1] = True
39+
# Set the whole stack to True.
40+
self.has_checkpoint_stack = [True] * len(self.has_checkpoint_stack)
4141

4242
def visit_With(self, node: cst.With) -> None:
4343
if m.matches(node, m.With(asynchronous=m.Asynchronous())):

tests/autofix_files/trio100.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,48 @@ async def foo():
6868

6969
async with random_ignored_library.fail_after(10):
7070
...
71+
72+
73+
# Seems like the inner context manager 'hides' the checkpoint.
74+
async def does_contain_checkpoints():
75+
with trio.fail_after(1): # false-alarm TRIO100
76+
with trio.CancelScope(): # or any other context manager
77+
await trio.sleep_forever()
78+
79+
80+
async def more_nested_tests():
81+
with trio.fail_after(1):
82+
with trio.CancelScope():
83+
await trio.sleep_forever()
84+
# error: 13, "trio", "CancelScope"
85+
...
86+
with trio.CancelScope():
87+
await trio.sleep_forever()
88+
# error: 13, "trio", "CancelScope"
89+
...
90+
91+
# error: 9, "trio", "fail_after"
92+
# error: 13, "trio", "CancelScope"
93+
...
94+
# error: 13, "trio", "CancelScope"
95+
...
96+
97+
with trio.fail_after(1):
98+
with trio.CancelScope():
99+
with trio.CancelScope():
100+
with trio.CancelScope():
101+
await trio.sleep_forever()
102+
103+
# don't remove other scopes
104+
with contextlib.suppress(Exception):
105+
print("foo")
106+
# error: 9, "trio", "fail_after"
107+
with contextlib.suppress(Exception):
108+
print("foo")
109+
with contextlib.suppress(Exception):
110+
# error: 13, "trio", "fail_after"
111+
print("foo")
112+
113+
with contextlib.suppress(Exception):
114+
with open("blah") as file:
115+
print("foo")

tests/autofix_files/trio100.py.diff

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,49 @@
8484

8585
async with random_ignored_library.fail_after(10):
8686
...
87+
@@ x,18 x,18 @@
88+
with trio.fail_after(1):
89+
with trio.CancelScope():
90+
await trio.sleep_forever()
91+
- with trio.CancelScope(): # error: 13, "trio", "CancelScope"
92+
- ...
93+
+ # error: 13, "trio", "CancelScope"
94+
+ ...
95+
with trio.CancelScope():
96+
await trio.sleep_forever()
97+
- with trio.CancelScope(): # error: 13, "trio", "CancelScope"
98+
- ...
99+
+ # error: 13, "trio", "CancelScope"
100+
+ ...
101+
102+
- with trio.fail_after(1): # error: 9, "trio", "fail_after"
103+
- with trio.CancelScope(): # error: 13, "trio", "CancelScope"
104+
- ...
105+
- with trio.CancelScope(): # error: 13, "trio", "CancelScope"
106+
- ...
107+
+ # error: 9, "trio", "fail_after"
108+
+ # error: 13, "trio", "CancelScope"
109+
+ ...
110+
+ # error: 13, "trio", "CancelScope"
111+
+ ...
112+
113+
with trio.fail_after(1):
114+
with trio.CancelScope():
115+
@@ x,12 x,12 @@
116+
# don't remove other scopes
117+
with contextlib.suppress(Exception):
118+
print("foo")
119+
- with trio.fail_after(1): # error: 9, "trio", "fail_after"
120+
- with contextlib.suppress(Exception):
121+
- print("foo")
122+
+ # error: 9, "trio", "fail_after"
123+
with contextlib.suppress(Exception):
124+
- with trio.fail_after(1): # error: 13, "trio", "fail_after"
125+
- print("foo")
126+
+ print("foo")
127+
+ with contextlib.suppress(Exception):
128+
+ # error: 13, "trio", "fail_after"
129+
+ print("foo")
130+
131+
with contextlib.suppress(Exception):
132+
with open("blah") as file:

tests/eval_files/trio100.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,48 @@ async def foo():
6868

6969
async with random_ignored_library.fail_after(10):
7070
...
71+
72+
73+
# Seems like the inner context manager 'hides' the checkpoint.
74+
async def does_contain_checkpoints():
75+
with trio.fail_after(1): # false-alarm TRIO100
76+
with trio.CancelScope(): # or any other context manager
77+
await trio.sleep_forever()
78+
79+
80+
async def more_nested_tests():
81+
with trio.fail_after(1):
82+
with trio.CancelScope():
83+
await trio.sleep_forever()
84+
with trio.CancelScope(): # error: 13, "trio", "CancelScope"
85+
...
86+
with trio.CancelScope():
87+
await trio.sleep_forever()
88+
with trio.CancelScope(): # error: 13, "trio", "CancelScope"
89+
...
90+
91+
with trio.fail_after(1): # error: 9, "trio", "fail_after"
92+
with trio.CancelScope(): # error: 13, "trio", "CancelScope"
93+
...
94+
with trio.CancelScope(): # error: 13, "trio", "CancelScope"
95+
...
96+
97+
with trio.fail_after(1):
98+
with trio.CancelScope():
99+
with trio.CancelScope():
100+
with trio.CancelScope():
101+
await trio.sleep_forever()
102+
103+
# don't remove other scopes
104+
with contextlib.suppress(Exception):
105+
print("foo")
106+
with trio.fail_after(1): # error: 9, "trio", "fail_after"
107+
with contextlib.suppress(Exception):
108+
print("foo")
109+
with contextlib.suppress(Exception):
110+
with trio.fail_after(1): # error: 13, "trio", "fail_after"
111+
print("foo")
112+
113+
with contextlib.suppress(Exception):
114+
with open("blah") as file:
115+
print("foo")

tests/test_flake8_trio.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ def check_autofix(
153153
return
154154

155155
# assert that there's no difference in the autofixed code from before
156-
assert visited_code == previous_autofixed
156+
assert visited_code == previous_autofixed, (
157+
"autofix diff, run with --generate-autofix if the test file has changed to"
158+
" update the autofix files."
159+
)
157160
# and assert that the diff is the same, which it should be if the above passes
158161
assert added_autofix_diff == autofix_diff_content
159162

0 commit comments

Comments
 (0)