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 · 7 comments · May be fixed by #37
Open

Use Pydantic V2 models #16

develop-cs opened this issue Jun 5, 2024 · 7 comments · May be fixed by #37
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

@develop-cs
Copy link
Collaborator Author

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

@roman2git
Copy link

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
Copy link

roman2git commented Nov 7, 2024

@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 roman2git linked a pull request Nov 8, 2024 that will close this issue
3 tasks
@roman2git
Copy link

roman2git commented Nov 8, 2024

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.

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

Successfully merging a pull request may close this issue.

2 participants