-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[ruff
] Add assert-with-print-message
rule (#11974)
#11981
Merged
charliermarsh
merged 5 commits into
astral-sh:main
from
denwong47:assert-with-print-expression
Jun 23, 2024
Merged
[ruff
] Add assert-with-print-message
rule (#11974)
#11981
charliermarsh
merged 5 commits into
astral-sh:main
from
denwong47:assert-with-print-expression
Jun 23, 2024
Conversation
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
denwong47
changed the title
[
[Jun 22, 2024
ruff
] Add assert-with-print-expression
flag (#11974)ruff
] Add assert-with-print-expression
rule (#11974)
Thanks! I will review. |
charliermarsh
approved these changes
Jun 23, 2024
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.
This is excellent work, nicely done.
I think I would rename the rule to |
Makes sense to me. |
MichaReiser
changed the title
[
[Jun 23, 2024
ruff
] Add assert-with-print-expression
rule (#11974)ruff
] Add assert-with-print-message
rule (#11974)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
great writeup
A wonderful example of a quality contribution
preview
Related to preview mode features
rule
Implementing or modifying a lint rule
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.