Skip to content

FXC-3378 Pydantic v2#2433

Merged
daquinteroflex merged 2 commits into
developfrom
yaugenst-flex/pydantic-v2
Feb 5, 2026
Merged

FXC-3378 Pydantic v2#2433
daquinteroflex merged 2 commits into
developfrom
yaugenst-flex/pydantic-v2

Conversation

@yaugenst-flex
Copy link
Copy Markdown
Collaborator

@yaugenst-flex yaugenst-flex commented May 8, 2025

Summary

Migrates the tidy3d python client from Pydantic v1 → v2 across models, validators, serialization, and schema generation.

This PR also includes the CI/tooling updates needed to keep the v2 codebase clean (type-only import handling and mypy checks).

Key changes

Pydantic v2 migration

  • Standardize imports on pydantic / pydantic_settings; remove v1 compatibility imports.
  • Replace @validator / @root_validator with @field_validator / @model_validator (incl. mode='before'|'after'|'wrap' where appropriate), updating signatures (ValidationInfo) and using object.__setattr__ when mutating frozen models.
  • Replace .dict()/.json()/.parse_obj() with model_dump* / model_validate, and .schema() with model_json_schema().
  • Migrate model configuration to model_config = ConfigDict(...).
  • Migrate BaseSettings usage to pydantic_settings.BaseSettings.
  • Update custom types to the v2 hooks:
    • __get_pydantic_core_schema__ / __get_pydantic_json_schema__
    • refactors toward Annotated[...] validators/serializers (BeforeValidator, PlainSerializer, etc.)
  • Regenerate and commit updated schema artifacts under schemas/.

Pydantic-v2-specific fixes + regressions locked by tests

  • Fix ValueMap validation bug caused by Mapping.keys/values name collisions; add regression tests.
  • Add regression test for lazy proxy materialization that touches __pydantic_* internals (guards future Pydantic bumps).
  • Preserve YAML serialization behavior for Infinity / -Infinity / NaN (string-based, JSON-aligned); add regression test and document why to_yaml() intentionally uses a JSON round-trip.
  • Guard info.data[...] cross-field access in field validators to avoid KeyError when prerequisite fields fail validation; add regression tests.

Tooling / CI

  • Add/update pre-commit + CI steps around type-only import organization and mypy type-signature checking (needed to keep the v2 migration maintainable).

Compatibility notes

  • Goal: no intentional public API drift. The generated JSON schemas are treated as part of the public API and were regenerated/committed as part of the migration.
  • tidy3d continues to raise domain-specific exceptions (tidy3d.exceptions.ValidationError, SetupError, etc.) for many validation failures; Pydantic core parse errors may differ slightly from v1 for invalid inputs.

How to test

  • poetry run pre-commit run --all-files
  • poetry run pytest -q

Follow-ups (post-merge)

  • Refactor validator sprawl in Simulation/Medium into orchestrated helpers while preserving warning/error ordering.
  • Audit load-only aliases and consider validation_alias / serialization_alias where appropriate (may require schema review).
  • Evaluate removing JSON encode/decode round-trips only where behavior stays identical.

Note

Medium Risk
Primarily CI/tooling and dependency constraint changes; moderate risk due to expanding mypy coverage to all of tidy3d and introducing a new import-rewrite gate that could fail PRs or require code changes.

Overview
Adds a new CI and pre-commit verification (scripts/move_type_imports.py) that enforces type-only imports are moved under if TYPE_CHECKING, and wires this into the workflow’s required checks.

Updates verification tooling to standardize on Python 3.11 via a shared VERIFICATIONS_PY_VERSION, adjusts pytest config formatting, and broadens mypy to run across the full tidy3d package.

Tightens the Pydantic v2 dependency spec (pydantic >=2.9,<3) and adds pydantic-settings, plus includes libcst in dev extras to support the new import-guard tooling; also ignores simulation.json in git.

Written by Cursor Bugbot for commit bdf3a7a. This will update automatically on new commits. Configure here.

@yaugenst-flex yaugenst-flex self-assigned this May 8, 2025
@yaugenst-flex yaugenst-flex added the 2.9 will go into version 2.9.* label May 8, 2025
@yaugenst-flex yaugenst-flex linked an issue May 8, 2025 that may be closed by this pull request
@yaugenst-flex yaugenst-flex force-pushed the yaugenst-flex/pydantic-v2 branch from 0a529f4 to a7e7a49 Compare May 9, 2025 07:10
@yaugenst-flex yaugenst-flex mentioned this pull request May 9, 2025
@yaugenst-flex yaugenst-flex force-pushed the yaugenst-flex/pydantic-v2 branch 2 times, most recently from 348a931 to 5ecc9f9 Compare May 9, 2025 20:08
@momchil-flex
Copy link
Copy Markdown
Collaborator

It seems like fundamentally there's just a few things that change? Maybe I'm missing something important/tricky. Could you explain if there's anything like that, and what changes going forward?

Also very important - we will certainly need to update the backend python code too and might need to update denormalizer, metadata api, and who knows what else.

@yaugenst-flex
Copy link
Copy Markdown
Collaborator Author

Yes the biggest changes are related to custom types and serialization, everything else is relatively straightforward. I'll write something up.

@yaugenst-flex yaugenst-flex force-pushed the yaugenst-flex/pydantic-v2 branch from 93ce359 to dd10b26 Compare May 14, 2025 15:36
@yaugenst-flex yaugenst-flex force-pushed the yaugenst-flex/pydantic-v2 branch 3 times, most recently from dd0cd2b to 38d502a Compare May 16, 2025 10:50
@yaugenst-flex yaugenst-flex force-pushed the yaugenst-flex/pydantic-v2 branch from ea3b4cb to 29234a6 Compare May 19, 2025 12:06
@momchil-flex momchil-flex added the rc1 1st pre-release label May 19, 2025
@yaugenst-flex yaugenst-flex force-pushed the yaugenst-flex/pydantic-v2 branch 4 times, most recently from 6d3f88a to 2967f74 Compare May 22, 2025 08:58
@yaugenst-flex yaugenst-flex force-pushed the yaugenst-flex/pydantic-v2 branch 4 times, most recently from 0a72b9c to 715c758 Compare May 26, 2025 20:24
@yaugenst-flex yaugenst-flex force-pushed the yaugenst-flex/pydantic-v2 branch 2 times, most recently from 09d1e4a to 7e2c693 Compare July 4, 2025 10:58
@momchil-flex momchil-flex added 2.10 and removed rc2 2nd pre-release 2.9 will go into version 2.9.* labels Jul 7, 2025
@yaugenst-flex yaugenst-flex force-pushed the yaugenst-flex/pydantic-v2 branch 7 times, most recently from 5000801 to 5dd22db Compare July 10, 2025 17:03
@mahlau-flex
Copy link
Copy Markdown
Contributor

mahlau-flex commented Oct 2, 2025

I have added a commit where all of the conflicts are resolved that can be resolved automatically. This is a bit tricky, because I had to make this a commit that does not resolve the other merge conflicts with some git magic (create two clones of the repo, make changes in one and copy them to the other as a "normal" commit). With the same strategy we should be able to bring this branch back on track piece by piece. In the end, we can squash all of these commits into a single rebase.

@yaugenst-flex if this sounds good to you, I will continue to make more commits here that resolve more and more conflicts. For some of them I will need help though, because at first glance it does not always seem clear what the best strategy for merging is.

Comment thread tidy3d/web/core/task_core.py Outdated
Comment thread tidy3d/web/api/webapi.py Outdated
@marcorudolphflex
Copy link
Copy Markdown
Contributor

we should also update the readme here:

The `Simulation` can be exported as .json-like dictionary with `Simulation.json()`

with model_dump_json

Comment thread tidy3d/components/medium.py Outdated
Comment thread tidy3d/components/data/dataset.py Outdated
Comment thread .github/workflows/tidy3d-python-client-tests.yml Fixed
Comment thread .github/workflows/tidy3d-python-client-tests.yml Fixed
Comment thread .github/workflows/tidy3d-python-client-tests.yml Fixed
Comment thread .github/workflows/tidy3d-python-client-tests.yml Fixed
Comment thread pyproject.toml Outdated
Comment thread .github/workflows/tidy3d-python-client-tests.yml
Comment thread .github/workflows/tidy3d-python-client-tests.yml
Comment thread docs/api/plugins/index.rst Outdated
Copy link
Copy Markdown
Collaborator

@daquinteroflex daquinteroflex left a comment

Choose a reason for hiding this comment

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

Massive effort 🎉 💪

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

Comment thread pyproject.toml
yaugenst-flex and others added 2 commits February 5, 2026 09:37
fix: rebase changes for v2

chore(tidy3d): FXC-4559-add-type-checking-import-moving-and-checking-to-pre-commit-and-ci

fix: minor fixes for v2

fix: stabilize model hashing in tests

fix: restore copy(True) compatibility and accept doping lists

fix(tidy3d): FXC-4824-fix-corrupted-data-in-vtk-arrays

fix: more rebase fixes for v2

fix(tidy3d): FXC-5094-fix-deprecation-warnings-in-tests

fix: even more rebase changes

final rebase changes

fix: repair pydantic v2 validators

chore: regenerate schemas

chore(ci): fix move-type-imports and pin actions

chore(ci): fix misleading python step name

perf(pydantic): defer model build

Fix missing Field reference in TCAD grid

chore: remove stale pd references

test: restore default pytest coverage

docs: remove stale adjoint plugin warning

Co-authored-by: Yannick Mahlau <yannik.mahlau@flexcompute.com>
Co-authored-by: Marco Rudolph <marco.rudolph@flexcompute.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ignore_diff_coverage Ignores CI comments on diff coverage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Full Pydantic v2 upgrade ⏫

8 participants