fix(rewrite): skip assertion rewriting in class bodies - #14961
Closed
mturac wants to merge 2 commits into
Closed
Conversation
added 2 commits
September 2, 2026 02:53
The assertion rewriter injects temporary variables (@py_assert0, etc.) into class bodies and then cleans them up by assigning None to the same name. Class namespaces that reject duplicate keys — most notably Enum's metaclass — raise TypeError on the second assignment. Skip rewriting assert statements that are direct children of a ClassDef body. Methods inside the class are still rewritten because they are FunctionDef nodes whose own bodies are visited in separate iterations. Fixes pytest-dev#9582
Member
|
At first glance this is a unattributed simple prompt result without deeper human work I won't review as such |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #9582
assertion rewriting crashes with
TypeErrorinsideEnumclass bodies (and any class whose metaclass rejects duplicate keys in namespace).root cause: the rewriter injects temp variables like
@py_assert0then sets them toNonefor cleanup._EnumDict.__setitem__raises on that second assignment becuase the name is already defined.fix: skip rewriting
assertthat sits directly inClassDef.body. methods inside the class still get rewritten normally — they're seperateFunctionDefnodes visited on their own.i hit this bug while working on a project that uses asserts in Enum bodies for validation. took a while to figure out it was the rewriter, not the enum itself.
ran the full
testing/test_assertrewrite.pysuite: 122 passed, 1 skipped. also added two tests — one for the Enum crash case, one to make sure methods still get assertion introspection.