Skip to content

[not-an-iterable] FP for attribute used in comprehension but guarded in if test #9729

@artsiomkaltovich

Description

@artsiomkaltovich

Bug description

Steps to reproduce:


class Model:
    field: list[int] | None = None

    def method(self):
        return [f + 1 for f in self.field] if self.field else None


if __name__ == "__main__":
    pass


NB: If replace `field: list[int] | None = None` with `field: list[int] | None` it works as expected

Configuration

No response

Command used

pylint filename.py

Pylint output

E1133: Non-iterable value self.field is used in an iterating context (not-an-iterable)

Expected behavior

No error

Pylint version

3.2.3

OS / Environment

MacOS 14.4.1
Python 3.12.0

Additional dependencies

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    AstroidRelated to astroidFalse Positive 🦟A message is emitted but nothing is wrong with the codeGood first issueFriendly and approachable by new contributorsHelp wanted 🙏Outside help would be appreciated, good for new contributorsNeeds PRThis issue is accepted, sufficiently specified and now needs an implementationNeeds astroid constraint

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions