[ruff
] Add assert-with-print-message
rule (#11974)
#11981
Merged
+810
−5
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.
Summary
Addresses #11974 to add a
RUF
rule to replaceprint
expressions inassert
statements with the inner message.An autofix is available, but is considered unsafe as it changes behaviour of the execution, notably:
stdout
, andAssertionError
instance containing a different message.While the detection of the condition is a straightforward matter, deciding how to resolve the print arguments into a string literal can be a relatively subjective matter. The implementation of this PR chooses to be as tolerant as possible, and will attempt to reformat any number of
print
arguments containing single or concatenated strings or variables into either a string literal, or a f-string if any variables or placeholders are detected.Test Plan
cargo test
.Examples
For ease of discussion, this is the diff for the tests:
Known Issues
The current implementation resolves all arguments and separators of the
print
expression into a single string, be itStringLiteralValue::single
or aFStringValue::single
. This:"...\n...\n..."
format.These are purely formatting issues only occurring in unusual scenarios.
Additionally, the autofix will tolerate
print
calls that were previously invalid:This will be transformed into
which some could argue is an alteration of behaviour.