Migrate to pydantic v2 - #769
Merged
Merged
Conversation
The report/model_card system used pydantic v1-only APIs throughout: Config-class inheritance, root_validator/validator, .dict()/.json()/ .parse_obj()/.parse_file(), __fields__/ModelField introspection, and Field(unique_items=...). None of this works under pydantic v2. - base.py: replace Config classes with ConfigDict; replace the root_validator-based composition check with model_validator(mode= "before"); capture the composable_with/list_factory class arguments via __init_subclass__ instead of pydantic v1's implicit Config-kwarg merging. Simplify add_field/update_field: pydantic v2's extra="allow" natively tracks and (de)serializes extra attributes, so the manual ModelField/FieldInfo construction is no longer needed. Preserve one v1 behavior update_field relied on: pydantic v1 silently coerced an arbitrary BaseModel instance into a differently-typed nested model via dict(); v2 requires an explicit dict, so mismatched instances are dumped to dicts before being appended. - fields.py/model_card.py: root_validator/validator -> model_validator/ field_validator; drop the no-longer-supported unique_items Field kwarg (never enforced past initial construction anyway); License's spdx validator now mutates self directly (model_validator(mode= "after")) instead of a values dict, bypassing the validated __setattr__ to avoid infinite recursion since validate_assignment would otherwise re-run the same "after" validator. - report.py/utils.py: .dict()/.json()/.parse_obj()/.parse_file()/ ModelCard.validate() -> model_dump()/model_dump_json()/ model_validate()/model_validate_json(); __fields__/.type_ -> model_fields + a small Optional/List-unwrapping helper, since FieldInfo has no v1-style .type_. - deploy/report/api/main.py: same validator/Field(min_items=...) updates for its own (separate) pydantic model. - tests: AnyUrl is no longer a str subclass in pydantic v2, so compare str(url) instead of url directly. Full pre-commit suite (ruff, mypy, doctest, nbstripout, pytest) passes across the whole repo. Co-Authored-By: Claude Fable 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.
Problem
The
report/model_cardsystem used pydantic v1-only APIs throughout:Config-class inheritance,root_validator/validator,.dict()/.json()/.parse_obj()/.parse_file(),__fields__/ModelFieldintrospection, andField(unique_items=...). This blocked upgrading pydantic past<2, which in turn was one of the pinned dependencies with known vulnerabilities. An earlier attempt at this (#642) was an incomplete, auto-generated stub — closed as superseded by this PR.Changes
Configclasses withConfigDict; replace theroot_validator-based composition check withmodel_validator(mode="before"); capture thecomposable_with/list_factoryclass arguments via__init_subclass__instead of pydantic v1's implicit Config-kwarg merging. Simplifyadd_field/update_field: pydantic v2'sextra="allow"natively tracks and (de)serializes extra attributes, so manualModelField/FieldInfoconstruction is no longer needed. One v1 behavior is explicitly preserved: v1 silently coerced an arbitraryBaseModelinstance into a differently-typed nested model viadict(); v2 requires an explicit dict, so mismatched instances are dumped to dicts before being appended (a test relies on this).root_validator/validator→model_validator/field_validator; drop the no-longer-supportedunique_itemsField kwarg (it was never enforced past initial construction anyway, per the existing code comments).License's SPDX validator now mutatesselfdirectly (model_validator(mode="after")) instead of avaluesdict, bypassing the validated__setattr__to avoid infinite recursion (sincevalidate_assignmentwould otherwise re-run the same "after" validator)..dict()/.json()/.parse_obj()/.parse_file()/ModelCard.validate()→model_dump()/model_dump_json()/model_validate()/model_validate_json();__fields__/.type_→model_fields+ a small Optional/List-unwrapping helper, sinceFieldInfohas no v1-style.type_.validator/Field(min_items=...)updates for its own (separate) pydantic model.AnyUrlis no longer astrsubclass in pydantic v2, so comparestr(url)instead ofurldirectly.Validation
Full local
pre-commit run --all-files(ruff, ruff-format, mypy, doctest, nbstripout, pytest) passes across the whole repo, including the fulltests/cyclops/report/suite (50 tests).🤖 Generated with Claude Code