arta icon indicating copy to clipboard operation
arta copied to clipboard

Use Pydantic V2 models

Open develop-cs opened this issue 1 year ago • 3 comments

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

develop-cs avatar Jun 05 '24 14:06 develop-cs

Hello, what is the goal here? should final code match requirements for both pydantic v1 and v2 or switch to v2 completely (impacting dependencies)?

roman2git avatar Sep 28 '24 08:09 roman2git

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")

develop-cs avatar Oct 01 '24 16:10 develop-cs

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

roman2git avatar Oct 04 '24 16:10 roman2git

Hi @roman2git, Have you started anything on this issue? If not, I will add it to my next implementations. If yes, I won't.

develop-cs avatar Nov 06 '24 15:11 develop-cs

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?

roman2git avatar Nov 07 '24 07:11 roman2git

@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?

roman2git avatar Nov 07 '24 11:11 roman2git

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.

roman2git avatar Nov 08 '24 09:11 roman2git