You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Permit either __aenter__ or __aexit__ without checkpoints (#444)
* Exempt async CM methods from ASYNC910/911 when partner checkpoints
ASYNC910 and ASYNC911 no longer require every `__aenter__`/`__aexit__` to
contain a checkpoint. Per Trio's documentation, an async context manager
only needs one of entry/exit to act as a checkpoint.
When a class defines both methods, the one without an `await` is exempt
if its partner contains one. When a class defines only one of the two,
the partner is charitably assumed to be inherited from a base class and
to contain a checkpoint, so the defined method is also exempt.
Closes#441https://claude.ai/code/session_014jAydKywq31Ew4fVYGJdiG
* Tighten async CM exemption rules
Two refinements in response to review feedback:
- If an `__aenter__`/`__aexit__` method contains any checkpoint-like
construct (`await`, `async with`, or `async for`), it must always
checkpoint. We no longer exempt such methods even when the partner
provides a checkpoint -- conditional checkpoints are still flagged.
- Only charitably assume a missing partner is inherited (with a
checkpoint) when the class actually inherits from something. Classes
with no base classes are treated as flat, and methods that don't
checkpoint are flagged. `metaclass=` and other keyword arguments do
not count as inheriting, since they live in `ClassDef.keywords` rather
than `ClassDef.bases`.
https://claude.ai/code/session_014jAydKywq31Ew4fVYGJdiG
* Only flag __aenter__ when neither CM method checkpoints
When both `__aenter__` and `__aexit__` are defined and neither contains
a checkpoint, we used to flag (and autofix) both methods, which produced
redundant `lowlevel.checkpoint()` calls -- only one is needed for the
async context manager to checkpoint.
Prefer to report and fix `__aenter__` in this case; `__aexit__` is
exempted since adding a checkpoint to either satisfies the rule.
https://claude.ai/code/session_014jAydKywq31Ew4fVYGJdiG
---------
Co-authored-by: Claude <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: docs/changelog.rst
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,7 @@ Changelog
7
7
Unreleased
8
8
==========
9
9
- Autofix for :ref:`ASYNC910 <async910>` / :ref:`ASYNC911 <async911>` no longer inserts checkpoints inside ``except`` clauses (which would trigger :ref:`ASYNC120 <async120>`); instead the checkpoint is added at the top of the function or of the enclosing loop. `(issue #403) <https://github.com/python-trio/flake8-async/issues/403>`_
10
+
- :ref:`ASYNC910 <async910>` and :ref:`ASYNC911 <async911>` now accept ``__aenter__`` / ``__aexit__`` methods when the partner method provides the checkpoint, or when only one of the two is defined on a class that inherits from another class (charitably assuming the partner is inherited and contains a checkpoint). `(issue #441) <https://github.com/python-trio/flake8-async/issues/441>`_
0 commit comments