Skip to content

Bump Pydantic to v2+ #1285

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

Open
wants to merge 43 commits into
base: main
Choose a base branch
from
Open

Bump Pydantic to v2+ #1285

wants to merge 43 commits into from

Conversation

BenjaminCharmes
Copy link
Contributor

@BenjaminCharmes BenjaminCharmes commented Jul 25, 2025

Update Pydantic from v1.10 to v2+

Also closes #1282

Copy link

cypress bot commented Jul 25, 2025

datalab    Run #3730

Run Properties:  status check passed Passed #3730  •  git commit 339ce2e6fd ℹ️: Merge 204e365d0c3ab3601965ef4046b8ca4aeecb7c04 into 0850aff9949ae7befcf7442d9c57...
Project datalab
Branch Review bc/bump-pydantic
Run status status check passed Passed #3730
Run duration 07m 04s
Commit git commit 339ce2e6fd ℹ️: Merge 204e365d0c3ab3601965ef4046b8ca4aeecb7c04 into 0850aff9949ae7befcf7442d9c57...
Committer Ben Charmes
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 0
Tests that did not run due to a developer annotating a test with .skip  Pending 0
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 336
View all changes introduced in this branch ↗︎

Copy link

codecov bot commented Jul 28, 2025

Codecov Report

❌ Patch coverage is 85.48185% with 116 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.95%. Comparing base (0850aff) to head (204e365).

Files with missing lines Patch % Lines
pydatalab/src/pydatalab/routes/v0_1/items.py 82.73% 24 Missing ⚠️
pydatalab/src/pydatalab/models/utils.py 81.18% 19 Missing ⚠️
pydatalab/src/pydatalab/routes/v0_1/graphs.py 72.72% 18 Missing ⚠️
pydatalab/src/pydatalab/models/traits/__init__.py 88.09% 10 Missing ⚠️
pydatalab/src/pydatalab/mongo.py 78.57% 9 Missing ⚠️
pydatalab/src/pydatalab/models/cells.py 87.03% 7 Missing ⚠️
pydatalab/src/pydatalab/models/people.py 80.00% 6 Missing ⚠️
...datalab/src/pydatalab/models/traits/collectable.py 83.33% 6 Missing ⚠️
pydatalab/src/pydatalab/routes/v0_1/auth.py 16.66% 5 Missing ⚠️
pydatalab/src/pydatalab/models/entries.py 90.47% 2 Missing ⚠️
... and 8 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1285      +/-   ##
==========================================
- Coverage   78.49%   77.95%   -0.55%     
==========================================
  Files          67       69       +2     
  Lines        4645     4885     +240     
==========================================
+ Hits         3646     3808     +162     
- Misses        999     1077      +78     
Files with missing lines Coverage Δ
pydatalab/src/pydatalab/apps/chat/blocks.py 58.06% <100.00%> (ø)
pydatalab/src/pydatalab/backups.py 62.96% <100.00%> (ø)
pydatalab/src/pydatalab/login.py 85.07% <100.00%> (+0.94%) ⬆️
pydatalab/src/pydatalab/main.py 66.00% <100.00%> (+1.41%) ⬆️
pydatalab/src/pydatalab/models/__init__.py 100.00% <100.00%> (ø)
pydatalab/src/pydatalab/models/collections.py 94.11% <100.00%> (-0.62%) ⬇️
pydatalab/src/pydatalab/models/equipment.py 100.00% <100.00%> (ø)
pydatalab/src/pydatalab/models/files.py 100.00% <100.00%> (ø)
pydatalab/src/pydatalab/models/items.py 96.00% <100.00%> (-0.67%) ⬇️
pydatalab/src/pydatalab/models/samples.py 100.00% <100.00%> (ø)
... and 21 more
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@BenjaminCharmes BenjaminCharmes force-pushed the bc/bump-pydantic branch 3 times, most recently from 6edc151 to 46cc0c8 Compare July 28, 2025 14:57
Copy link
Member

@ml-evs ml-evs left a comment

Choose a reason for hiding this comment

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

Hi @BenjaminCharmes, here's some partial initial comments/suggestions. I'll try to deploy a system based on this PR from scratch (and also investigate some of the new incantations that you've had to use for pydantic 2 compat).

Comment on lines 107 to 112
if "_id" in user:
user["immutable_id"] = str(user.pop("_id"))

if "managers" not in user:
user["managers"] = None

Copy link
Member

Choose a reason for hiding this comment

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

Note to self that I want to come back and verify this wrt. model aliases

Comment on lines 29 to 33
Sample.model_rebuild()
StartingMaterial.model_rebuild()
Cell.model_rebuild()
Equipment.model_rebuild()
Collection.model_rebuild()
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Sample.model_rebuild()
StartingMaterial.model_rebuild()
Cell.model_rebuild()
Equipment.model_rebuild()
Collection.model_rebuild()
{model.model_rebuild() for model in ITEM_MODELS.values()}

Might be neater, though not sure why we need this now and not before (will play around with it later)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doesn't work with pre-commit for me, need to find out why.

It seems that this is necessary because Pydantic v2 is stricter with forward reference and circular dependencies

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also need to keep Collection since it's not in ITEM_MODELS

@ml-evs ml-evs self-assigned this Aug 1, 2025
pydatalab now start, but not working well

pydatalab now start, but not working well

pydatalab now start, but not working well

pydatalab now start, but not working well

pydatalab now start, but not working well

Fix login with Pydantic v2+

Fix sample creation

Fix sample creation

Fix edit page
Fix pytest

Fix pytest

Fix pytest

Fix pytest

Fix pytest

Fix pytest

Fix pytest
Try fix .env PYDATALAB_TESTING

Try fix .env PYDATALAB_TESTING

Add temp. debug

Add temp. debug

Add temp. debug

Add temp. debug

Add temp. debug

Add temp. debug

Add temp. debug
@ml-evs ml-evs force-pushed the bc/bump-pydantic branch from 66c974c to e5ebeb1 Compare August 8, 2025 21:24
@ml-evs ml-evs force-pushed the bc/bump-pydantic branch from 947d1a6 to 1cb7e5c Compare August 9, 2025 11:29
@ml-evs ml-evs marked this pull request as ready for review August 9, 2025 12:40
@ml-evs ml-evs requested a review from jdbocarsly as a code owner August 9, 2025 12:40
Comment on lines +54 to +57
@model_validator(mode="before")
@classmethod
def add_missing_synthesis_relationships(cls, values):
"""Add any missing sample synthesis constituents to parent relationships"""
Copy link
Member

Choose a reason for hiding this comment

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

I've moved this whole module, but want to check this validator as it seems a bit excessive

Comment on lines +16 to +19
@model_validator(mode="before")
@classmethod
def add_missing_collection_relationships(cls, values):
if values.get("collections") is not None:
Copy link
Member

Choose a reason for hiding this comment

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

Likewise this validator

Comment on lines 1039 to 1041
except Exception as e:
LOGGER.exception(f"Error in get_item_data: {e}")
return jsonify({"status": "error", "message": str(e), "error_type": type(e).__name__}), 500
Copy link
Member

Choose a reason for hiding this comment

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

Better to avoid this -- there are app-level error handlers that will produce better error messages for all routes + it makes PRs hard to review


if contact_email or contact_email in (None, ""):
if contact_email in ("", None):
update["contact_email"] = None
else:
update["contact_email"] = EmailStr(contact_email)
if "@" not in contact_email or len(contact_email) > 1000:
Copy link
Member

Choose a reason for hiding this comment

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

Things like this really need to be handled at the validator level

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.

pydantic model descriptions don't seem to appear in API-reported schemas
2 participants