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=Truein 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. Instead pydantic just does a simple instance check. Since it is doing an instance check of a Protocol at runtime, you do have to use the runtime_checkable decorator.
Here is the fix I went with:
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]
Also see this issue at GH with a short discussion of the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With