rberga06.utils.types.SupportsPydanticV2#
- class rberga06.utils.types.SupportsPydanticV2(*args, **kwargs)#
Bases:
Protocol
[_T
]A
typing.Protocol
that makes it easy to implementpydantic
v2 support.- Example:
>>> class Foo(SupportsPydanticV2[Any]): ... def __init__(self, value: str, /) -> None: ... self.value = value ... ... @override ... def __repr__(self) -> str: ... return f"<Foo: {self.value!r}>" ... ... @classmethod ... @override ... def validate(cls, /, value: Any) -> Self: ... if isinstance(value, str): ... return cls(value) ... return cls(repr(value)) ... >>> class Model(pydantic.BaseModel): ... answer: Foo # you can use Foo as a pydantic field type ... >>> Foo("42") <Foo: '42'> >>> # useful when outside a pydantic model >>> Foo.validate(42) <Foo: '42'> >>> # Foo.validate() is called when validating the pydantic model >>> Model(answer=42) Model(answer=<Foo: '42'>) >>> Model(answer="42") Model(answer=<Foo: '42'>) >>> Model.model_validate({"answer": 42}) Model(answer=<Foo: '42'>)
- __init__(*args, **kwargs)#
Methods