I’m attempting to use typing protocols and pydantic together and am running into some issues. I expect probably my understanding of how to properly use the two of them together is lacking.
This pydantic code is producing an error:
from typing import TypeVar, Protocol, Generic, runtime_checkable
from pydantic import BaseModel
D = TypeVar('D')
@runtime_checkable
class SupportsDimensionality(Protocol[D]):
dimensionality: D
class ValueModel(BaseModel, Generic[D]):
value: SupportsDimensionality[D]
Error text:
pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for main.SupportsDimensionality[~D]. Set
arbitrary_types_allowed=True
in the model_config to ignore this error or implement__get_pydantic_core_schema__
on your type to fully support it.
I’ve been trying to figure out why this happens and how to fix it but need some help.
I figured it out. The docs say here you can use the arbitrary_types_allowed=True
configuration option in a case like this. The problem seems to be that pydantic doesn’t support validation of an arbitrary Protocol
.
What this does is circumvent the detailed/thorough model checking for fields in cases they are just arbitrary types that are not directly supported by pydantic.
Here is the fix I went with:
"""Implementation of the NBC 2020 building code, for flushmount solar."""
from typing import TypeVar, Protocol, Generic, runtime_checkable
from pydantic import BaseModel, ConfigDict
D = TypeVar('D')
@runtime_checkable
class SupportsDimensionality(Protocol[D]):
dimensionality: D
class Model(BaseModel):
"""A base model that allows protocols to be used for fields."""
model_config = ConfigDict(arbitrary_types_allowed=True)
class ValueModel(Model, Generic[D]):
value: SupportsDimensionality[D]