Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/refurb/FURB156.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,30 @@
# with comment
).capitalize()

# example with augmented assignment
_ += "0123456789"

# OK

_ = "1234567890"
_ = "1234"
_ = "12" in "12345670"


# No errors as the string is considered as a docstring
class C:
"01234567"


class C:
def method(self):
"01234567"


def function():
"""01234567"""


"""01234567"""

"01234567"
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ use crate::checkers::ast::Checker;
use crate::importer::ImportRequest;
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, ViolationMetadata};
use ruff_python_ast::ExprStringLiteral;
use ruff_python_ast::{ExprStringLiteral, Stmt};
use ruff_text_size::TextRange;

/// ## What it does
/// Checks for uses of hardcoded charsets, which are defined in Python string module.
///
/// Note that the rule doesn't apply to docstring.
///
/// ## Why is this bad?
/// Usage of named charsets from the standard library is more readable and less error-prone.
///
Expand Down Expand Up @@ -46,6 +48,13 @@ impl AlwaysFixableViolation for HardcodedStringCharset {

/// FURB156
pub(crate) fn hardcoded_string_charset_literal(checker: &Checker, expr: &ExprStringLiteral) {
// if the string literal is a docstring, the rule is not applied
if !matches!(checker.semantic().current_statement(), &Stmt::Assign(_))
&& !matches!(checker.semantic().current_statement(), &Stmt::AugAssign(_))
{
return;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can use checker.semantic().in_pep_257_docstring() here instead. This should give us consistent behavior to other rules ignoring docstrings.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was that method. The problems with this method are that

"""01234567"""

"01234567"

are not considered as docstrings and marked as violations.

To fix all the issues reported in the issue, I had to use the current condition present in the PR.

But I can change it to if checker.semantic().in_pep_257_docstring() {...} if you want to be consistent between docstrings ;-)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I double checked and just the second string in my above example will be marked as an error as it is not at the top of the module. So it makes sense to use: in_pep_257_docstring().

I will change


if let Some(charset) = check_charset_exact(expr.value.to_str().as_bytes()) {
push_diagnostic(checker, expr.range, charset);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,30 @@ FURB156.py:26:5: FURB156 [*] Use of hardcoded string charset
27 |+ string.digits
27 28 | # with comment
28 29 | ).capitalize()
29 30 |
29 30 |

FURB156.py:31:6: FURB156 [*] Use of hardcoded string charset
|
30 | # example with augmented assignment
31 | _ += "0123456789"
| ^^^^^^^^^^^^ FURB156
32 |
33 | # OK
|
= help: Replace hardcoded charset with `string.digits`

ℹ Safe fix
1 1 | # Errors
2 |+import string
2 3 |
3 4 | _ = "0123456789"
4 5 | _ = "01234567"
--------------------------------------------------------------------------------
28 29 | ).capitalize()
29 30 |
30 31 | # example with augmented assignment
31 |-_ += "0123456789"
32 |+_ += string.digits
32 33 |
33 34 | # OK
34 35 |