Repository navigation
fix(pydantic): propagate model_config to generated submodels - #2427
Open
buildwithpnj wants to merge 1 commit into
Open
buildwithpnj wants to merge 1 commit into
buildwithpnj wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
Description
Propagates the
model_configparameter passed topydantic_model_creator()down to generated nested submodels inPydanticModelCreator._get_submodel()andPydanticModelCreator._create_submodel().Root-level
titleis omitted when forwardingmodel_configso each nested submodel retains its own schema title (instead of inheriting the parent model's title).Motivation and Context
Fixes #1686.
Previously,
PydanticModelCreator._get_submodel()forwardedexclude,include,computed,allow_cycles, andsort_alphabetically, but omittedmodel_config. As a result, options supplied viapydantic_model_creator(..., model_config=...)(such asjson_encoders,str_to_upper, orextra) were ignored on nested relational submodels, and submodels could share cached classes with unconfigured models.How Has This Been Tested?
Added three regression tests in
tests/contrib/test_pydantic.py:test_pydantic_creator_propagates_model_config_to_submodels: verifies multi-level nested (Tournament->Event->Team), forward FK (Event->Tournament), and nullable FK (Event->Reporter) submodels inheritmodel_configoptions while preserving each submodel's owntitle.test_pydantic_creator_submodel_cache_respects_model_config: verifies submodels generated with custommodel_configdo not collide with default cached submodels, and atitle-onlymodel_configdoes not leak to submodels.test_pydantic_creator_submodel_inherits_json_encoders: verifiesjson_encoderspassed viamodel_configapply to nested submodel fields duringmodel_dump_json().Verified locally on Windows 11 / Python 3.12.14 (
TORTOISE_TEST_DB=sqlite://:memory:,PYTHONDEVMODE=1):python -m pytest tests/contrib/test_pydantic.py -v: 56 passedpython -m ruff check tortoise/contrib/pydantic/creator.py tests/contrib/test_pydantic.py: passedpython -m ruff format --check tortoise/contrib/pydantic/creator.py tests/contrib/test_pydantic.py: passedpython -m mypy tortoise/contrib/pydantic/creator.py tests/contrib/test_pydantic.py: 0 errorsChecklist: