Skip to content

False positive for E1133(not-an-iterable) with pydantic model_fields class method #10144

@ecs-jnguyen

Description

@ecs-jnguyen

Bug description

When iterating over the pydantic BaseModel.model_fields class var pylint errors with E1133(not-an-iterable).

Example code:

"""hi.py file."""
from pydantic import BaseModel


class Person(BaseModel):
    """This is a person class."""

    name: str
    age: int


if __name__ == '__main__':
    p = Person(name='John Doe', age=30)
    print(p)

    print("model fields via class variable:")
    print("Pylint will error on this when it shouldn't")
    for field in Person.model_fields:
        print(field)

    print("model fields via instance variable:")
    print("Pylint does not error on this")
    for field in p.model_fields:
        print(field)

Here is where model_fields is defined in pydantic: https://github.com/pydantic/pydantic/blob/main/pydantic/main.py#L244

Configuration

I'm using the default configuration

Command used

pylint hi.py

Pylint output

pylint hi.py
************* Module hi
hi.py:18:17: E1133: Non-iterable value Person.model_fields is used in an iterating context (not-an-iterable)

------------------------------------------------------------------
Your code has been rated at 6.67/10 (previous run: 6.67/10, +0.00)

Expected behavior

I am expecting this to not report an error

Pylint version

pylint 3.3.2
astroid 3.3.6
Python 3.11.10 (main, Oct 15 2024, 09:49:14) [Clang 15.0.0 (clang-1500.3.9.4)]

OS / Environment

MacOS
iterm

However this also happens in GitHub actions

Additional dependencies

annotated-types==0.7.0
astroid==3.3.6
dill==0.3.9
isort==5.13.2
mccabe==0.7.0
platformdirs==4.3.6
pydantic==2.10.4
pydantic_core==2.27.2
pylint==3.3.2
tomlkit==0.13.2
typing_extensions==4.12.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    False Positive 🦟A message is emitted but nothing is wrong with the codeLib specific 💅This affect the code from a particular libraryNeeds PRThis issue is accepted, sufficiently specified and now needs an implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions