REFC: Updating web submodule to pydantic v2 #2857
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The original purpose of this PR was to update the web submodule as an isolated component to pydantic v2, since updating the core components (mainly Tidy3DBaseModel) would require a lot of effort.
However, gradual adaptation of pydantic V2 in the web submodule is more difficult than anticipated. The Tidy3DBaseModel (or subclasses thereof) are also used a lot in this submodule, making gradual adaptation almost impossible.
In this PR, I have adapted two isolated components to pydantic v2, since these components. This was possible since these components are not used within the tidy3D repo itself. I would need some help estimating if these changes could affect code outside of this repo.
It is also debatable if it even makes sense to only udpate few isolated components here, since they would need to be isolated until the rest of the repo is also updated to v2 (mixing of v1 and v2 immediately results in an exception). This could lead to problems in development later.
Greptile Overview
Updated On: 2025-10-01 12:11:50 UTC
Summary
This PR performs a partial migration of two isolated components in the web submodule from Pydantic v1 to v2. The changes update `tidy3d/web/core/s3utils.py` and `tidy3d/web/api/material_fitter.py` by switching imports from `pydantic.v1` to `pydantic` and updating deprecated method calls to their v2 equivalents.In
s3utils.py, theparse_obj()method is replaced withmodel_validate()for the_S3STSTokenclass. Inmaterial_fitter.py, the.json()method is updated to.model_dump_json()for JSON serialization. These components were selected because they use basic Pydantic functionality without depending on the coreTidy3DBaseModelclasses that are extensively used throughout the repository.The migration approach is intentionally limited in scope - the developer acknowledges that a full Pydantic v2 migration would be complex due to widespread use of
Tidy3DBaseModelthroughout the codebase. By updating only isolated components, this PR serves as a proof-of-concept for Pydantic v2 compatibility while avoiding the extensive refactoring that would be required for a complete migration.PR Description Notes:
Changed Files
tidy3d/web/core/s3utils.pytidy3d/web/api/material_fitter.pyConfidence score: 3/5
tidy3d/web/api/material_fitter.pywhich still uses the deprecated.dict()method on line 101Sequence Diagram
sequenceDiagram participant User participant MaterialFitterTask participant FitterOptions participant DispersionFitter participant HTTP_API participant S3_Service participant TempFile User->>MaterialFitterTask: "submit(fitter, options)" MaterialFitterTask->>DispersionFitter: "validate fitter type" MaterialFitterTask->>FitterOptions: "validate options type" MaterialFitterTask->>DispersionFitter: "extract wvl_um, n_data, k_data" MaterialFitterTask->>TempFile: "create temporary CSV file" MaterialFitterTask->>TempFile: "save data with np.savetxt" MaterialFitterTask->>HTTP_API: "get signed URL for upload" MaterialFitterTask->>HTTP_API: "PUT request to upload file" MaterialFitterTask->>FitterOptions: "model_dump_json(exclude_none=True)" MaterialFitterTask->>HTTP_API: "POST fitter/fit request" HTTP_API-->>MaterialFitterTask: "return task response" User->>MaterialFitterTask: "sync_status()" MaterialFitterTask->>HTTP_API: "GET fitter/{id} status" HTTP_API-->>MaterialFitterTask: "update status" User->>MaterialFitterTask: "save_to_library(name)" MaterialFitterTask->>HTTP_API: "POST fitter/save request" HTTP_API-->>MaterialFitterTask: "return success status" User->>S3_Service: "upload_file(resource_id, path, filename)" S3_Service->>HTTP_API: "get_s3_sts_token()" HTTP_API-->>S3_Service: "return S3 token" S3_Service->>S3_Service: "validate token expiration" S3_Service->>TempFile: "open file for reading" S3_Service->>S3_Service: "upload_fileobj to S3" User->>S3_Service: "download_file(resource_id, filename)" S3_Service->>HTTP_API: "get_s3_sts_token()" S3_Service->>S3_Service: "head_object for metadata" S3_Service->>TempFile: "create temporary download file" S3_Service->>S3_Service: "download_file from S3" S3_Service->>TempFile: "rename temp file to final path"