Skip to content

102 now also checks __aexit__ #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Changelog

`CalVer, YY.month.patch <https://calver.org/>`_

24.10.2
=======
- :ref:`ASYNC102 <async102>` now also warns about ``await()`` inside ``__aexit__``.

24.10.1
=======
- Add :ref:`ASYNC123 <async123>` bad-exception-group-flattening
Expand Down
4 changes: 2 additions & 2 deletions docs/rules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ ASYNC101 : yield-in-cancel-scope
This has substantial overlap with :ref:`ASYNC119 <ASYNC119>`, which will warn on almost all instances of ASYNC101, but ASYNC101 is about a conceptually different problem that will not get resolved by `PEP 533 <https://peps.python.org/pep-0533/>`_.

_`ASYNC102` : await-in-finally-or-cancelled
``await`` inside ``finally`` or :ref:`cancelled-catching <cancelled>` ``except:`` must have shielded :ref:`cancel scope <cancel_scope>` with timeout.
If not, the async call will immediately raise a new cancellation, suppressing the cancellation that was caught.
``await`` inside ``finally``, :ref:`cancelled-catching <cancelled>` ``except:``, or ``__aexit__`` must have shielded :ref:`cancel scope <cancel_scope>` with timeout.
If not, the async call will immediately raise a new cancellation, suppressing any cancellation that was caught.
See :ref:`ASYNC120 <async120>` for the general case where other exceptions might get suppressed.
This is currently not able to detect asyncio shields.

Expand Down
2 changes: 1 addition & 1 deletion flake8_async/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@


# CalVer: YY.month.patch, e.g. first release of July 2022 == "22.7.1"
__version__ = "24.10.1"
__version__ = "24.10.2"


# taken from https://github.com/Zac-HD/shed
Expand Down
2 changes: 1 addition & 1 deletion flake8_async/visitors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
visitor2xx,
visitor91x,
visitor101,
visitor102,
visitor102_120,
visitor103_104,
visitor105,
visitor111,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
"""Visitor102, which warns on unprotected `await` inside `finally`.
"""Contains Visitor102 with ASYNC102 and ASYNC120.

ASYNC102: await-in-finally-or-cancelled
ASYNC120: await-in-except


To properly protect they must be inside a shielded cancel scope with a timeout.
"""
Expand Down Expand Up @@ -222,6 +226,10 @@ def visit_FunctionDef(

self._potential_120 = []

# lambda doesn't have `name` attribute
if getattr(node, "name", None) == "__aexit__":
self._critical_scope = Statement("__aexit__", node.lineno, node.col_offset)

visit_AsyncFunctionDef = visit_FunctionDef
# lambda can't contain await, try, except, raise, with, or assignments.
# You also can't do assignment expressions with attributes. So we don't need to
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ ignore = [
"D105",
"D106",
"D107",
"D400", # ends-in-period, stricter version of ends-in-punctuation
"S101",
"D203", # one-blank-line-before-class
"D213", # multi-line-summary-second-line
Expand Down
5 changes: 5 additions & 0 deletions tests/eval_files/async102.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,8 @@ async def foo_nested_cs():
await foo() # error: 12, Statement("bare except", lineno-12)
cs1.shield = True
await foo()


# treat __aexit__ as a critical scope
async def __aexit__():
await foo() # error: 4, Statement("__aexit__", lineno-1)
Loading