Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Pydantic V2 models #16

Open
develop-cs opened this issue Jun 5, 2024 · 3 comments
Open

Use Pydantic V2 models #16

develop-cs opened this issue Jun 5, 2024 · 3 comments
Labels
good first issue Good for newcomers technical debt Additional rework caused by choosing an easy or limited solution

Comments

@develop-cs
Copy link
Collaborator

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 develop-cs added technical debt Additional rework caused by choosing an easy or limited solution good first issue Good for newcomers labels Jun 5, 2024
@roman2git
Copy link

roman2git commented Sep 28, 2024

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

@develop-cs
Copy link
Collaborator Author

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:

arta/src/arta/models.py

Lines 8 to 11 in b7b8938

try:
from pydantic import v1 as pydantic
except ImportError:
import pydantic # type: ignore

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

@roman2git
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers technical debt Additional rework caused by choosing an easy or limited solution
Projects
None yet
Development

No branches or pull requests

2 participants