Skip to content

FURB161 fix should be marked unsafe in some contexts #16457

@dscorbett

Description

@dscorbett

Summary

The fix for bit-count (FURB161) has a few problems.

When the argument to bin is a starred expression, the fix introduces a syntax error.

$ cat >furb161_1.py <<'# EOF'
print(bin(*[123]).count("1"))
# EOF

$ python furb161_1.py
6

$ ruff --isolated check --target-version py310 --preview --select FURB161 furb161_1.py --diff 2>&1 | grep error:
error: Fix introduced a syntax error. Reverting all changes.

The fix is marked safe, but it is only safe when the argument to bin is an instance of certain types, including int and bool, that have __index__ and bit_count methods. If Ruff can’t determine that the argument is of one of those types, it should mark the fix unsafe. Otherwise, the fix can introduce a runtime error.

$ cat >furb161_2.py <<'# EOF'
from plistlib import UID
print(bin(UID(123)).count("1"))
# EOF

$ python furb161_2.py
6

$ ruff --isolated check --target-version py310 --preview --select FURB161 furb161_2.py --fix
Found 1 error (1 fixed, 0 remaining).

$ cat furb161_2.py
from plistlib import UID
print(UID(123).bit_count())

$ python furb161_2.py
Traceback (most recent call last):
  File "furb161_2.py", line 2, in <module>
    print(UID(123).bit_count())
          ^^^^^^^^^^^^^^^^^^
AttributeError: 'UID' object has no attribute 'bit_count'

Another way the fix can be unsafe is by changing which exception is raised.

$ cat >furb161_3.py <<'# EOF'
try:
    bin("123").count("1")
except Exception as e:
    print(repr(e))
# EOF

$ python furb161_3.py
TypeError("'str' object cannot be interpreted as an integer")

$ ruff --isolated check --target-version py310 --preview --select FURB161 furb161_3.py --fix
Found 1 error (1 fixed, 0 remaining).

$ cat furb161_3.py
try:
    "123".bit_count()
except Exception as e:
    print(repr(e))

$ python furb161_3.py
AttributeError("'str' object has no attribute 'bit_count'")

Version

ruff 0.9.9 (091d0af 2025-02-28)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated to suggested fixes for violationshelp wantedContributions especially welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions