Skip to content

PLC1802 false negative when a sequence is an attribute #22780

@leandrobbraga

Description

@leandrobbraga

PLC1802 won’t trigger if the sequence is an attribute.

https://play.ruff.rs/74477bb6-7796-4884-927a-2d0a8f93e4e2

Example

By running

$ echo 'class Fruits:
    fruits: list[str]

    def __init__(self):
        self.fruits = ["apple", "orange"]

        if len(self.fruits):
            ...

fruits = ["apple", "orange"]
if len(fruits):
    ...' | cargo run -p ruff -- check --select PLC1802 -

We get this output, which is missing the self.fruits inside the Fruits class:

PLC1802 [*] `len(fruits)` used as condition without comparison
  --> -:11:4
   |
10 | fruits = ["apple", "orange"]
11 | if len(fruits):
   |    ^^^^^^^^^^^
12 |     ...
   |
help: Remove `len`

Found 1 error.
[*] 1 fixable with the `--fix` option.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingruleImplementing or modifying a lint ruletype-inferenceRequires more advanced type inference.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions