Repository navigation
[ruff] Add assert-with-print-message rule (#11974) - #11981
Merged
charliermarsh merged 5 commits intoJun 23, 2024
Merged
Conversation
ruff] Add assert-with-print-expression flag (#11974)ruff] Add assert-with-print-expression rule (#11974)
charliermarsh
self-requested a review
June 23, 2024 15:01
Member
|
Thanks! I will review. |
charliermarsh
approved these changes
Jun 23, 2024
charliermarsh
left a comment
Member
There was a problem hiding this comment.
This is excellent work, nicely done.
Member
|
I think I would rename the rule to |
Member
|
Makes sense to me. |
charliermarsh
enabled auto-merge (squash)
June 23, 2024 16:51
ruff] Add assert-with-print-expression rule (#11974)ruff] Add assert-with-print-message rule (#11974)
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.
Summary
Addresses #11974 to add a
RUFrule to replaceprintexpressions inassertstatements with the inner message.An autofix is available, but is considered unsafe as it changes behaviour of the execution, notably:
stdout, andAssertionErrorinstance 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
printarguments 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
printexpression into a single string, be itStringLiteralValue::singleor aFStringValue::single. This:"...\n...\n..."format.These are purely formatting issues only occurring in unusual scenarios.
Additionally, the autofix will tolerate
printcalls that were previously invalid:This will be transformed into
which some could argue is an alteration of behaviour.