Skip to content

Commit e1d1d32

Browse files
authored
fix internal error in visitor91x (#232)
* fix internal error in visitor91x caused by different try/except sharing the same reference to self.try_state * remove duplicated test case from eval_files/async900
1 parent 43cb713 commit e1d1d32

File tree

7 files changed

+43
-3
lines changed

7 files changed

+43
-3
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22
*[CalVer, YY.month.patch](https://calver.org/)*
33

4+
5+
## 24.4.1
6+
- ASYNC91X fix internal error caused by multiple `try/except` incorrectly sharing state.
7+
48
## 24.3.6
59
- ASYNC100 no longer triggers if a context manager contains a `yield`.
610

flake8_async/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838

3939
# CalVer: YY.month.patch, e.g. first release of July 2022 == "22.7.1"
40-
__version__ = "24.3.6"
40+
__version__ = "24.4.1"
4141

4242

4343
# taken from https://github.com/Zac-HD/shed

flake8_async/visitors/visitor91x.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ def leave_Yield(
454454
def visit_Try(self, node: cst.Try):
455455
if not self.async_function:
456456
return
457-
self.save_state(node, "try_state")
457+
self.save_state(node, "try_state", copy=True)
458458
# except & finally guaranteed to enter with checkpoint if checkpointed
459459
# before try and no yield in try body.
460460
self.try_state.body_uncheckpointed_statements = (
@@ -596,6 +596,8 @@ def visit_While_body(self, node: cst.For | cst.While):
596596
self.save_state(
597597
node,
598598
"uncheckpointed_statements",
599+
# reference is overwritten below so don't need to copy
600+
copy=False,
599601
)
600602

601603
# inject an artificial uncheckpointed statement that won't raise an error,

tests/autofix_files/async910.py

+13
Original file line numberDiff line numberDiff line change
@@ -601,3 +601,16 @@ async def await_in_gen_target():
601601
async def await_everywhere_except_gen_target(): # error: 0, "exit", Statement("function definition", lineno)
602602
(await x async for x in bar())
603603
await trio.lowlevel.checkpoint()
604+
605+
606+
# Issue #226
607+
async def fn_226(): # error: 0, "exit", Statement("function definition", lineno)
608+
try:
609+
while foo():
610+
try:
611+
await trio.sleep(1)
612+
except Exception:
613+
pass
614+
except Exception:
615+
pass
616+
await trio.lowlevel.checkpoint()

tests/autofix_files/async910.py.diff

+9-1
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,16 @@
205205

206206

207207
async def foo_comprehension_3():
208-
@@ x,3 x,4 @@
208+
@@ x,6 x,7 @@
209209

210210
async def await_everywhere_except_gen_target(): # error: 0, "exit", Statement("function definition", lineno)
211211
(await x async for x in bar())
212212
+ await trio.lowlevel.checkpoint()
213+
214+
215+
# Issue #226
216+
@@ x,3 x,4 @@
217+
pass
218+
except Exception:
219+
pass
220+
+ await trio.lowlevel.checkpoint()

tests/eval_files/async910.py

+12
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,15 @@ async def await_in_gen_target():
572572

573573
async def await_everywhere_except_gen_target(): # error: 0, "exit", Statement("function definition", lineno)
574574
(await x async for x in bar())
575+
576+
577+
# Issue #226
578+
async def fn_226(): # error: 0, "exit", Statement("function definition", lineno)
579+
try:
580+
while foo():
581+
try:
582+
await trio.sleep(1)
583+
except Exception:
584+
pass
585+
except Exception:
586+
pass

tests/test_flake8_async.py

+1
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ def _parse_eval_file(
416416
msg = f"Line {lineno}: Failed to format\n {message!r}\nwith\n{args}"
417417
raise ParseError(msg) from e
418418

419+
assert enabled_codes, "no codes enabled. Fix file name or add `# ARGS --enable=...`"
419420
enabled_codes_list = enabled_codes.split(",")
420421
for code in enabled_codes_list:
421422
assert re.fullmatch(

0 commit comments

Comments
 (0)