Migrate to pydantic v2; drop unmaintained streamlit_pydantic#10
Merged
Conversation
front has moved to pydantic v2 (i2mint/front#30). streamlitfront consumes front.py2pydantic and was the next package pinning the ecosystem to pydantic v1. The blocker was the streamlit_pydantic dependency: its last release (0.6.0, 2022) imports pydantic.BaseSettings, which was removed in pydantic v2, and there is no v2-compatible release. Rather than keep the ecosystem on pydantic v1, the small subset of streamlit_pydantic that streamlitfront used is reimplemented natively. Changes ------- - streamlitfront/pydantic_widgets.py (new) Native-Streamlit rendering of pydantic v2 models — a drop-in for the old `import streamlit_pydantic as sp`: * pydantic_input(key, model) -> validated instance (or None) * pydantic_form(key, model) -> instance on submit, else None * pydantic_output(instance) -> render as JSON Widgets are chosen per field type via model.model_fields. - streamlitfront/page_funcs.py SimplePageFuncPydanticWrite / SimplePageFuncPydanticWithOutput now use streamlitfront.pydantic_widgets. Result handling uses instance.model_dump() instead of dict(instance). - streamlitfront/tests/dummy_app.py Same import swap; added the missing module docstring. - streamlitfront/tests/test_pydantic_widgets.py (new) Covers the new module: pure-helper unit tests plus AppTest-based integration tests that render the widgets in a headless Streamlit run and confirm a validated model instance is built. - setup.cfg Drop streamlit_pydantic; add an explicit pydantic>=2 requirement (it was used at import time but never declared).
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 #9.
Why
fronthas moved to pydantic v2 (i2mint/front#30, merged).streamlitfrontconsumesfront.py2pydanticand was the next package keeping the ecosystem (extrude, anything composing front elements) pinned to pydantic v1.The blocker:
streamlit_pydanticstreamlit_pydantic's last release — 0.6.0 (2022) — importspydantic.BaseSettings, removed in pydantic v2. There is no v2-compatible release on PyPI; the package is effectively abandoned.Rather than pin the whole ecosystem to pydantic v1 for the sake of one
unmaintained dependency, the small subset of
streamlit_pydanticthatstreamlitfront actually used is reimplemented natively against pydantic v2
and
streamlit's own widgets.What changed
streamlitfront/pydantic_widgets.py(new, ~115 lines)A drop-in replacement for
import streamlit_pydantic as sp:pydantic_input(key, model)Noneon validation error)pydantic_form(key, model)st.form+ submit button; returns the instance on submit, elseNonepydantic_output(instance)Widget type is chosen per field from
model.model_fields(bool → checkbox,int/float → number_input, everything else → text_input;
Optional[X]isunwrapped to
X).streamlitfront/page_funcs.pySimplePageFuncPydanticWrite/SimplePageFuncPydanticWithOutputnow usestreamlitfront.pydantic_widgets. Result handling usesinstance.model_dump()rather thandict(instance).streamlitfront/tests/test_pydantic_widgets.py(new)_unwrap_optional).streamlit.testing.v1.AppTest: render thewidgets in a headless Streamlit run, edit them, and confirm a validated
model instance is produced; confirm
pydantic_formreturnsNoneuntilsubmitted.
streamlitfront/tests/dummy_app.pySame import swap; added the missing module docstring.
setup.cfgstreamlit_pydantic.pydantic>=2explicitly (it was imported at module-load time butnever declared).
Migration guide for downstream code
If you imported
streamlit_pydanticvia streamlitfront, switch tofrom streamlitfront import pydantic_widgets as sp— the three functionsabove keep the same names. Note
pydantic_input/pydantic_formnowreturn a pydantic model instance (not a dict); use
.model_dump()ifyou need a plain dict.
General pydantic v1→v2 changes (see front#30) also apply:
model.__fields__model.model_fieldsinstance.dict()instance.model_dump()model.schema_json()json.dumps(model.model_json_schema())Verified
All imports resolve with no
streamlit_pydanticinstalled, underpydantic 2.12.
Follow-up
extrudecan now drop its temporary CI workarounds (Python 3.12 + Windowsskip) once a new streamlitfront is on PyPI.