Use Pydantic V2 models
Is your feature request related to a problem? Please describe
Use Pydantic V2 BaseModel class rather than V1 (maintenance purpose only).
Describe the solution you'd like
Migrate referring the Pydantic V2 Migration Guide.
Describe alternatives you've considered
Use Pydantic V2/V1 backward compatibility:
try:
from pydantic import v1 as pydantic
except ImportError:
import pydantic # type: ignore
Hello, what is the goal here? should final code match requirements for both pydantic v1 and v2 or switch to v2 completely (impacting dependencies)?
Hi, Good question. Answer is the first:
"final code match requirements for both pydantic v1 and v2"
Currently, we are using Pydantic v2's backward compatibility of v1:
https://github.com/MAIF/arta/blob/b7b89382a957d841c2b675c1849994a0499f4b19/src/arta/models.py#L8-L11
The goal here is to implement the Pydantic models using Pydantic v2 but still compatible with v1.
A first idea (inspired by FastAPI) but not tested could be something like (code is an example):
from pydantic import BaseModel
from pydantic.version import VERSION
PYDANTIC_V1: bool = VERSION.startswith("1.")
if PYDANTIC_V1:
class MyModel(BaseModel):
attr_1: str
class Config:
extra = "allow"
else:
from pydantic import ConfigDict
class MyModel(BaseModel):
attr_1: str
model_config = ConfigDict(extra="allow")
Hello, thanks for explaining I'll try to implement it. And yes, probably, there will be a lot of ugly if..else here and there
Hi @roman2git, Have you started anything on this issue? If not, I will add it to my next implementations. If yes, I won't.
Hi @develop-cs I was not able to spend much time on that, currently I have a few days window and would be able to play with it. I will let you know the result in 3-4 days and if that is in a good shape - I will provide PR, if not - then you would pick it. Does it work for you?
@develop-cs also, as we are going to do about the same, would it be possible to use existing helper package like this: https://github.com/pyapp-kit/pydantic-compat or you'd prefer 'own' solution/implementation?
Hi @develop-cs , please, have a look at #37
Just a note: keeping compatibility would be extremely hard, once the pydantic package is used more extensively.