ruff icon indicating copy to clipboard operation
ruff copied to clipboard

UP035: doesn't detect errors when it should

Open morotti opened this issue 1 year ago • 1 comments

Hello,

I am trying to use ruff to automatically fix issues with collections import that have moved to collections.abc in python 3.10+

I've found that ruff can only detect and fix the import about half the time.

from collections import Iterable

def myfunction(arg):
    if isinstance(arg, Iterable):
        return True
    return False

myfunction("string")
import collections

def myfunction(arg):
    if isinstance(arg, collections.Iterable):
        return True
    return False

myfunction("string")

Try with ruff --select ALL example.py

In the top example, ruff finds the error and can fix it automatically.

example.py:1:1: UP035 [*] Import from `collections.abc` instead: `Iterable`

In the bottom example, ruff doesn't find any error when it should have. The code fails at runtime.

Traceback (most recent call last):
  File "/home/username/example.py", line 8, in <module>
    myfunction("string")
  File "/home/username/example.py", line 4, in myfunction
    if isinstance(arg, collections.Iterable):
                       ^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'collections' has no attribute 'Iterable'

Running on latest ruff release, 0.3.7, on python 3.11

morotti avatar Apr 15 '24 14:04 morotti

That rule flags imports, not usages, and so it doesn't flag attribute accesses on the module, just from collections import ... cases.

charliermarsh avatar Apr 15 '24 14:04 charliermarsh

It looks like the same as #3936 and #7526

jerr0328 avatar Jul 11 '24 14:07 jerr0328