From fa5202a701fb230617841e040aa3ba81bf3a226d Mon Sep 17 00:00:00 2001 From: jakkdl Date: Wed, 25 Sep 2024 16:05:20 +0200 Subject: [PATCH] fix assertionerror with infinite loop in context manager --- docs/changelog.rst | 4 ++++ flake8_async/__init__.py | 2 +- flake8_async/visitors/visitor91x.py | 4 ++-- tests/autofix_files/async100.py | 9 +++++++++ tests/eval_files/async100.py | 9 +++++++++ 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 7ad71327..9a4e78f9 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -4,6 +4,10 @@ Changelog `CalVer, YY.month.patch `_ +24.9.5 +====== +- Fix crash when analyzing code with infinite loop inside context manager. + 24.9.4 ====== - Add :ref:`ASYNC122 ` delayed-entry-of-relative-cancelscope. diff --git a/flake8_async/__init__.py b/flake8_async/__init__.py index af042280..82fdf434 100644 --- a/flake8_async/__init__.py +++ b/flake8_async/__init__.py @@ -38,7 +38,7 @@ # CalVer: YY.month.patch, e.g. first release of July 2022 == "22.7.1" -__version__ = "24.9.4" +__version__ = "24.9.5" # taken from https://github.com/Zac-HD/shed diff --git a/flake8_async/visitors/visitor91x.py b/flake8_async/visitors/visitor91x.py index 2c9a9f81..539fcee6 100644 --- a/flake8_async/visitors/visitor91x.py +++ b/flake8_async/visitors/visitor91x.py @@ -452,7 +452,7 @@ def error_91x( node: cst.Return | cst.FunctionDef | cst.Yield, statement: Statement, ) -> bool: - assert not isinstance(statement, ArtificialStatement) + assert not isinstance(statement, ArtificialStatement), statement if isinstance(node, cst.FunctionDef): msg = "exit" @@ -768,7 +768,7 @@ def leave_While_body(self, node: cst.For | cst.While): | self.uncheckpointed_statements | self.loop_state.uncheckpointed_before_continue ): - if stmt == ARTIFICIAL_STATEMENT: + if isinstance(stmt, ArtificialStatement): continue any_error |= self.error_91x(err_node, stmt) diff --git a/tests/autofix_files/async100.py b/tests/autofix_files/async100.py index bd3d2809..524e320f 100644 --- a/tests/autofix_files/async100.py +++ b/tests/autofix_files/async100.py @@ -121,3 +121,12 @@ async def more_nested_tests(): async def foo(): with trio.fail_after(1): yield + + +# This previously caused an AssertionError, see issue #295 +async def fn(timeout): + with trio.fail_after(timeout): + while True: + if condition(): + return + await trio.sleep(1) diff --git a/tests/eval_files/async100.py b/tests/eval_files/async100.py index 226e34d1..1460a545 100644 --- a/tests/eval_files/async100.py +++ b/tests/eval_files/async100.py @@ -121,3 +121,12 @@ async def more_nested_tests(): async def foo(): with trio.fail_after(1): yield + + +# This previously caused an AssertionError, see issue #295 +async def fn(timeout): + with trio.fail_after(timeout): + while True: + if condition(): + return + await trio.sleep(1)