Open awgymer opened 3 weeks ago
I think the API to specify validation for extra fields is a bit flawed. While specifying it as an annotation is beneficial as you can then easily type check attribute accesses to __pydantic_extra__
, there's a couple issues arising from this:
__pydantic_extra__
as a field, so using = Field(init=False)
is necessary as an ugly workaroundextra
to 'allow'
without redefining __pydantic_extra__
(because you don't want any specific validation to be applied), __pydantic_extra__
is still annotated as dict[str, Any] | None
, although at runtime it is guaranteed to not be None
__pydantic_extra__
is considered mutable (see an explanation with a similar example here). In practice, I don't expect anyone to mutate the __pydantic_extra__
dictionary directly. In fact, we provide a model_extra
property to access the extra values. It is also flawed because the type of this property does not reflect the possibly overridden __pydantic_extra__
annotation.I can't come up with a good API immediately, however one thing that is certain is that we will have to break the existing API if we ever want to make changes here. As such, marking this issue as https://github.com/pydantic/pydantic/labels/v3.
In the meanwhile, you can add a pyright: ignore[reportIncompatibleVariableOverride]
comment or disable the error code at the project level (enabling this error code is debatable, see https://github.com/microsoft/pyright/issues/5933).
Initial Checks
Description
Overriding
__pyright_extra__
causes apyright
typing error because the type is not the same as the base declaration.Example Code
Python, Pydantic & OS Version