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

mypy doesn't complain about non-existent attributes when setting #9017

Closed
1 task done
samskiter opened this issue Mar 14, 2024 · 3 comments
Closed
1 task done

mypy doesn't complain about non-existent attributes when setting #9017

samskiter opened this issue Mar 14, 2024 · 3 comments
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation

Comments

@samskiter
Copy link

samskiter commented Mar 14, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Related: #245

The above issue is fixed for getting attributes when using the pydantic mypy plugin, but not for setting attributes

Example Code

class blah(pydantic.BaseModel):

    temperature: Optional[int] = pydantic.Field(
        ..., description="The temperature of the zone"
    )
    zone: int = pydantic.Field(..., description="The zone number")


myblah = blah(temperature=1, zone=2)
myblah.sdlkjdshljkdsj = "wekjhe" <<< # does not generate an error

print(myblah.aaa) # <<< Generates an error

Python, Pydantic & OS Version

python -c "import pydantic.version; print(pydantic.version.version_info())"
             pydantic version: 2.6.3
        pydantic-core version: 2.16.3
          pydantic-core build: profile=release pgo=true
                 install path: /Users/samduke/Development/tado-playground/hub_rpi/.venv/lib/python3.11/site-packages/pydantic
               python version: 3.11.8 (main, Feb  6 2024, 21:21:21) [Clang 15.0.0 (clang-1500.1.0.2.5)]
                     platform: macOS-14.1-arm64-arm-64bit
             related packages: typing_extensions-4.10.0 mypy-1.8.0
                       commit: unknown
@samskiter samskiter added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Mar 14, 2024
@samskiter
Copy link
Author

Workaround:

Adding an @no_type_check decorator to setattr:

class blah(pydantic.BaseModel):
    @no_type_check
    def __setattr__(self, name: str, value: json.Any) -> None:
        return super().__setattr__(name, value)

    temperature: Optional[int] = pydantic.Field(
        ..., description="The temperature of the zone"
    )
    zone: int = pydantic.Field(..., description="The zone number")

@Viicos
Copy link
Contributor

Viicos commented Mar 15, 2024

Hum, are you certain this is an issue on latest? I believe this was fixed in #8651.

Those dunder methods are currently defined in a if not TYPE_CHECKING block, however it might be interesting to use no_type_check, would avoid the extra indentation block

@sydney-runkle
Copy link
Member

Closing for now, as I believe this is fixed on main and will be in 2.7.0b1. Feel free to ping me if you run a test that proves otherwise!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation
Projects
None yet
Development

No branches or pull requests

3 participants