Repository navigation
Migrate to pydantic 2 - #96
Merged
Merged
Conversation
pydantic 1 is in maintenance mode, and newer packages in the lab's stack (e.g. gdsfactory >= 7.4, needed to unblock autogator) require pydantic 2. - Require pydantic>=2.8,<3 (2.8 for coerce_numbers_to_str). - Config models: ConfigDict instead of class Config; field_validator and an "after" model_validator for the cross-reference check; model_post_init (rather than a custom __init__) names nameservers on every construction/validation path; PrivateAttr from pydantic. - model_dump / model_validate / model_validate_json / model_copy replace dict / parse_obj / parse_file / parse_raw / copy throughout. - describe_config_error reports our validators' own messages rather than pydantic 2's "Value error, ..." wrapping. - Compatibility: coerce_numbers_to_str keeps configs that pydantic 1 accepted (an unquoted `version: 1.0`) loading; lockfiles and configs written by the previous release still parse. - docs/requirements.txt pinned pydantic==1.10.21, which would have made Read the Docs unable to install pyrolab at all. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Read the Docs installs docs/requirements.txt and then PyroLab itself, which brings its own dependencies. Repeating them here (pydantic, Pyro5, pyyaml, numpy, scipy) only risked pins that disagree with pyproject.toml, as the old pydantic==1.10.21 pin did. linkify-it-py was unused (MyST's linkify extension isn't enabled). Also stop mocking deprecation, typer, colorama, and tabulate for autodoc: they are real dependencies, always installed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Moves pyrolab from pydantic 1 to pydantic 2. This unblocks moving autogator to pydantic 2, which needs gdsfactory 7.4 or newer, or dropping gdsfactory for plain gdstk. That autogator work follows in a separate PR. Targets
master; it doesn't depend on anything else.What changed
Dependency:
pydantic>=2.8,<3. 2.8 is the first version withcoerce_numbers_to_str(see below).Config models (
configure.py):model_config = ConfigDict(extra="forbid", validate_default=True, coerce_numbers_to_str=True)replacesclass Config.@field_validatorreplaces@validator, and the cross-reference check becomes an "after"@model_validator.model_post_initinstead of a custom__init__, so this happens on every way a config is created: constructed, validated from YAML, or copied.PrivateAttris now imported frompydanticitself.API renames throughout:
.dict().model_dump().json().model_dump_json()parse_objmodel_validateparse_file/parse_rawmodel_validate_json.copy(deep=True).model_copy(deep=True)__fields_set__model_fields_setError messages:
describe_config_errorshows our validators' own messages. pydantic 2 would otherwise prefix them with "Value error, ...". Its own messages have new wording, for example "Extra inputs are not permitted", and the tests now expect that.Compatibility with existing files
version: 1.0or a numericdescription. Strict pydantic 2 rejects these, which would break existing configs, so thecoerce_numbers_to_strsetting keeps accepting them. A test covers it.model_dump().Read the Docs
docs/requirements.txtpinnedpydantic==1.10.21. With pyrolab requiring pydantic 2, Read the Docs would no longer be able to install the docs requirements together with pyrolab. Read the Docs runspip install .after installing that file, which already brings pyrolab's own dependencies, so the file now lists only the docs tools: jupyter-book and numpydoc. The pydantic, Pyro5, pyyaml, numpy and scipy pins are gone, and so is the unusedlinkify-it-py. autodoc also no longer mocksdeprecation,typer,coloramaandtabulate, which are real dependencies.A Read-the-Docs-style build (fresh Python 3.11 venv, then the requirements file, then
pip install ., then sphinx) succeeds from a clean checkout. Its warnings match master's, apart from ones from pydantic 2's own docstrings, which use mkdocs syntax and appear on the pages for the inheritedBaseModelmethods.Checks run
PydanticDeprecatedSince20) turned into errors.config updatewith an unquotedversion: 1.0, thenup,ps,status,info, andreloadwith re-registration at the nameserver, thendown. No processes were left behind.🤖 Generated with Claude Code