Skip to content

Permit either __aenter__ or __aexit__ without checkpoints#444

Merged
Zac-HD merged 3 commits intopython-trio:mainfrom
Zac-HD:claude/fix-inherited-checkpoints-NWk97
Apr 21, 2026
Merged

Permit either __aenter__ or __aexit__ without checkpoints#444
Zac-HD merged 3 commits intopython-trio:mainfrom
Zac-HD:claude/fix-inherited-checkpoints-NWk97

Conversation

@Zac-HD
Copy link
Copy Markdown
Member

@Zac-HD Zac-HD commented Apr 21, 2026

Fixes #441.

claude added 3 commits April 21, 2026 19:27
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 python-trio#441

https://claude.ai/code/session_014jAydKywq31Ew4fVYGJdiG
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
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
@Zac-HD Zac-HD merged commit 21af102 into python-trio:main Apr 21, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ASYNC910/911: require only one of __aenter__/__aexit__ to contain a checkpoint

2 participants