-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Pyright/Pylance typing error when overriding __pydantic_extra__
type
#9187
Comments
Thanks for reporting this! Yep, looks like a bug. Great first issue, for anyone interested! |
This is unfortunate, as A workaround is to define your model as: from pydantic import ConfigDict, Field
class MyModel(BaseModel):
__pydantic_extra__: dict[str, int] = Field(init=False)
a: int
model_config = ConfigDict(extra="allow") Ideally, this feature could have been implemented as a config value, e.g. |
@sydney-runkle I can pick this up. |
What do you think about this approach here (we might need some docs changes): from pydantic import BaseModel, ConfigDict
from pydantic.fields import PrivateAttr as _PrivateAttr
class Model(BaseModel):
__pydantic_extra__: dict[str, int] = _PrivateAttr()
a: int
model_config = ConfigDict(extra="allow")
m = Model(a=1) Thinking about this approach on the heels of #9293 |
Also, here's the relevant docs for reference: https://docs.pydantic.dev/latest/concepts/models/#extra-fields |
Looks good as a workaround, so the docs can probably be updated with the added |
#9187 (comment) (pydantic-env) ➜ tmp mypy 9187.py
9187.py:12: error: Missing named argument "__pydantic_extra__" for "Model" [call-arg]
Found 1 error in 1 file (checked 1 source file) |
When #9659 gets merged, I think the solution to this will be to do: from pydantic import BaseModel, Field
class MyModel(BaseModel, extra='allow'):
__pydantic_extra__: dict[str, int] = Field(init=False) (or similar, with whatever type hint you want on the pydantic extra) |
Initial Checks
Description
Similar to #9147 and #7094, but those seem to be focused on
TypeAdapter
and I'm unsure how closely those are related. Sorry if this is a duplicate.See code below. Pylance warns that
__pydantic_extra__
should also be specified when instantiating the class. I believe this warning should not appear.Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: