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

feat: Upgrade to Pydantic V2 #3134

Conversation

michael-genson
Copy link
Collaborator

@michael-genson michael-genson commented Feb 9, 2024

What type of PR is this?

(REQUIRED)

  • bug
  • feature

What this PR does / why we need it:

(REQUIRED)

This PR is a monster. Pydantic provides a tool to make some model changes (bump-pydantic), but it doesn't cover most things. Thankfully, their migration guide covers just about everything, save for some of the grosser stuff: https://docs.pydantic.dev/latest/migration/

Most changes are pretty straightforward, but there are a few... "special" changes that are a bit hacky:

  • We use a custom in-house controller for our FastAPI routes. Part of the magic-controller-stuff includes walking params and handling them in various ways, including certain handling for classvars. We relied on Pydantic's is_classvar for this, but that doesn't exist anymore. Rather than attempt to figure out how that part of the codebase works, I just copied it over from Pydantic V1
  • Similarly, when parsing datetime strings, we relied on Pydantic's datetime_parse module/method to handle this. I tried using a recommended substitution, but got different results than we were expecting (timezones were getting mishmashed somewhere and tests were failing) so I, again, just copied Pydantic V1's implementation
  • The GetterDict isn't a thing anymore, so all of our custom getter-dicts had to be replaced. I basically replaced them with field validators (Pydantic V2's replacement for @validator), and fixed some other edgecases (particularly with Group/User.group being the group name)

Also, Somehow the format for PostgresDsn changed. I had to tweak it to get postgres to work right, but if you could take a look and make sure I didn't screw that up, that'd be great.

Which issue(s) this PR fixes:

(REQUIRED)

N/A, will close some renovate bot PRs

@michael-genson michael-genson changed the title feat: Upgrade to Pyndatic V2 feat: Upgrade to Pydantic V2 Feb 9, 2024
@michael-genson michael-genson marked this pull request as ready for review February 10, 2024 22:41
@property
def db_url(self) -> str:
host = f"{self.POSTGRES_SERVER}:{self.POSTGRES_PORT}"
return PostgresDsn.build(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PostgresDsn changes, pretty sure this is right

@@ -47,6 +46,25 @@ def _cbv(router: APIRouter, cls: type[T], *urls: str, instance: Any | None = Non
return cls


# copied from Pydantic V1 Source: https://github.com/pydantic/pydantic/blob/1c91c8627b541b22354b9ed56b9ef1bb21ac6fbd/pydantic/v1/typing.py
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gross copy from Pydantic V1

@@ -0,0 +1,252 @@
"""
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied from Pydantic V1. I wasn't sure where to put it so I put it in schema/_mealie

@michael-genson
Copy link
Collaborator Author

I tried to highlight some of the bigger changes in the comments, but this PR is pretty massive

hay-kot
hay-kot previously approved these changes Feb 11, 2024
Copy link
Collaborator

@hay-kot hay-kot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. This was such a huge lift. Thanks for taking this on.

I'll give you $50 if you didn't break anything 😉

@michael-genson
Copy link
Collaborator Author

if you didn't break anything

Optimistic

@michael-genson michael-genson enabled auto-merge (squash) February 11, 2024 16:44
@michael-genson michael-genson merged commit 7a10758 into mealie-recipes:mealie-next Feb 11, 2024
9 checks passed
@michael-genson michael-genson deleted the feat/upgrade-pydantic-v2 branch February 11, 2024 16:47
anoadragon453 added a commit to anoadragon453/nixpkgs that referenced this pull request May 6, 2024
In mealie v1.3.0, the dependency on pydantic was bumped from v1 to v2:
mealie-recipes/mealie#3134

Thus the workaround for using pydantic v1 can be removed.
anoadragon453 added a commit to anoadragon453/nixpkgs that referenced this pull request May 7, 2024
* authlib was required in v1.4.0
  (mealie-recipes/mealie#3280)
* pillow-heif was required in v1.5.0
  (mealie-recipes/mealie#3409)
* pydantic-settings was required in v1.3.0
  (mealie-recipes/mealie#3134 - same PR as
  pydantic v2 bump)
* pyjwt was required in v1.6.0
  (mealie-recipes/mealie#3521)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants