Migrate front from pydantic v1 to pydantic v2 - #31
Merged
Merged
Conversation
Hard cut — no compatibility shim. See issue #30 for the migration guide. Changes ------- - front/py2pydantic.py - Doctest examples switched from schema_json() to model_json_schema(). - Detect BaseModel-shadowing field names proactively (pydantic v2 only warns instead of raising NameError, which left conflicting fields in place silently). The auto-uppercase rename is now triggered upfront. - In func_to_pyd_model_specs, infer the field type from a default's type when no annotation is given. Pydantic v2 requires (type, default) tuples and no longer infers the type from a bare default value. - Module docstring expanded — concrete enough to drive doc generation. - front/tests/test_py2pydantic.py - model.__fields__ -> model.model_fields - field.type_ -> field.annotation - default sentinel: handled via field.is_required() and the pydantic_core.PydanticUndefined sentinel rather than comparing to None. - front/tests/test_py2pydantic_integration.py (new) - End-to-end coverage of the function -> pydantic input model -> wrapped-call -> result pipeline that streamlitfront and extrude depend on. Previously only the doctest exercised this path; the unit test only inspected model class structure. - pyproject.toml - dependencies: pydantic==1.10.12 -> pydantic>=2
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.
Closes #30.
Why
frontpinnedpydantic==1.10.12, propagating that pin intostreamlitfront,extrude, and anything else composing front elements. Meanwhile, neweri2mint packages (e.g.
juviahttp2py) call pydantic v2 APIs at importtime, so the v1 pin was breaking the wider ecosystem. This PR cuts the
anchor:
frontnow requirespydantic>=2.This is a hard cut — no v1 compatibility shim. The migration surface
inside
frontis tiny (one module, ~140 lines), and dual-supporting v1and v2 would cost more than the migration itself.
What changed
front/py2pydantic.pyschema_json()tomodel_json_schema()(v1's
schema_json()is removed in v2).func_to_pyd_model_specsnow infers the field type from a default'stype when no annotation is provided. Pydantic v2 requires explicit
(type, default)tuples; v1's "bare default infers type" path nolonger works.
copy,schema) are detectedproactively now. In v1 a conflict raised
NameError, which the codecaught and used as a trigger for the auto-uppercase rename. In v2,
pydantic merely warns and leaves the conflict in place, so the
detection has to be upfront via
set(Sig(func).names) & set(dir(BaseModel)).front/tests/test_py2pydantic.pymodel.__fields__→model.model_fieldsfield.type_→field.annotationpydantic_core.PydanticUndefinedfor required fieldsrather than
None. Test now usesfield.is_required()to check.front/tests/test_py2pydantic_integration.py(new)End-to-end coverage of the path that downstream packages actually
depend on:
ValidationErrorsurfaces on bad inputThis was previously only exercised by the doctest. The pre-existing
unit test only checked model-class structure, not dispatch behavior.
pyproject.tomlpydantic==1.10.12→pydantic>=2Migration guide for downstream callers
If your code uses
front.py2pydantic.func_to_pyd_input_model_cls(...):model.__fields__model.model_fieldsfield.type_field.annotationfield.default(None if required)field.is_required()+field.defaultmodel.schema_json()json.dumps(model.model_json_schema())instance.dict()instance.model_dump()instance.json()instance.model_dump_json()create_model, instantiating models, and basic validation are unchanged.Verified
Including the new integration tests, all 37 doctests + unit tests pass
under pydantic 2.12.2.
Follow-ups
After this lands and a new front version is on PyPI:
streamlitfront— drop its v1-erastreamlit_pydantic/ pydanticusage, bump to a v2-compatible release. Separate PR.
extrude— revert the temporary CI workarounds (Python 3.12 +Windows skip) added during the wider CI sweep. Should be one-line
reverts once the ecosystem is consistent on v2.