-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Fix assert rewriting with assignment expressions #11414
Merged
nicoddemus
merged 5 commits into
pytest-dev:main
from
cdce8p:fix-assert-rewriting-assignexpr
Sep 9, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fixed ``:=`` in asserts impacting unrelated test cases. |
This file contains 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
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't quite understand the role of
_SCOPE_END_MARKER
... could you explain it? Or perhaps if you follow my suggestion of using an AST subclass, add that explanation to the docstring of the subclass.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although the
AssertionRewriter
inherits fromNodeVisitor
, not all nodes are in fact visited - onlyassert
and their child nodes. That makes it a bit difficult to detect when a scope change happens. ATM pytest only iterates over all nodes recursively and copies them if they don't need to be rewritten. To do that, child nodes are added tonodes
and then popped in the while loop. I added_SCOPE_END_MARKER
to know when all child nodes e.g. for aFunctionDef
have been visited.An example
nodes
list contains maybe a few imports and say twoFunctionDef
nodes fortest_1
andtest_2
.FunctionDef
node fortest_2
is popped fromnodes
and all child nodes are added tonodes
recursively.nodes
._SCOPE_END_MARKER
we would reach a point at which theFunctionDef
node fortest_1
would be popped fromnodes
, not knowing that we already left thetest_2
function scope -> That's why the marker is added right after thetest_2
node is popped, i.e. when we reach it all child nodes have been dealt with and we should leave the current scope.There are a few caveats, mainly that not all child nodes are actually in the child scope (e.g. default arguments which are evaluated in the parent scope), but it doesn't really matter here as those don't usually contain
assert
statements. So we don't need to handle them specifically.Hope that at least somewhat makes sense.
pytest/src/_pytest/assertion/rewrite.py
Lines 722 to 743 in e5c81fa
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!