Skip to content

FURB156 should not apply to docstrings #16351

@dscorbett

Description

@dscorbett

Description

hardcoded-string-charset (FURB156) applies to docstrings in Ruff 0.9.7, but it should not.

When the string is a module docstring, the fix inserts the import after its use, causing a NameError.

$ cat >furb156_1.py <<'# EOF'
"01234567"
print(__doc__)
# EOF

$ python furb156_1.py
01234567

$ ruff --isolated check --preview --select FURB156 furb156_1.py --fix
Found 1 error (1 fixed, 0 remaining).

$ cat furb156_1.py
string.octdigits
import string
print(__doc__)

$ python furb156_1.py 
Traceback (most recent call last):
  File "furb156_1.py", line 1, in <module>
    string.octdigits
    ^^^^^^
NameError: name 'string' is not defined. Did you forget to import 'string'?

It can also introduce a syntax error.

$ printf '"01234567"'  | ruff --isolated check --preview --select FURB156 - --diff 2>&1 | grep error:
error: Fix introduced a syntax error. Reverting all changes.

Even when the import is inserted in the intended place, FURB156 should not apply to docstrings, because replacing them with identifiers makes them not be docstrings.

$ cat >furb156_2.py <<'# EOF'
class C:
    "01234567"
print(C.__doc__)
# EOF

$ python furb156_2.py
01234567

$ ruff --isolated check --preview --select FURB156 furb156_2.py --fix
Found 1 error (1 fixed, 0 remaining).

$ cat furb156_2.py
import string
class C:
    string.octdigits
print(C.__doc__)

$ python furb156_2.py
None

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinghelp wantedContributions especially welcome

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions