Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ecs-jnguyen opened this issue Dec 19, 2024 · 0 comments
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Lib specific 💅 This affect the code from a particular library Needs PR This issue is accepted, sufficiently specified and now needs an implementation

Comments

@ecs-jnguyen
Copy link

ecs-jnguyen commented Dec 19, 2024

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
@ecs-jnguyen ecs-jnguyen added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Dec 19, 2024
@zenlyj zenlyj added False Positive 🦟 A message is emitted but nothing is wrong with the code Lib specific 💅 This affect the code from a particular library Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Dec 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Lib specific 💅 This affect the code from a particular library Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

No branches or pull requests

2 participants