Fix Pydantic Creator Model: Don't mark fields optional if default value is not null#5
Draft
fullonic wants to merge 2 commits intofeature/allow-pass-configuration-for-ToTsQueryfrom
Conversation
…x/pydantic-creator-dont-mark-fields-optional-if-default-value-is-not-null
There was a problem hiding this comment.
Pull Request Overview
This PR adjusts the logic for marking fields as Optional in the Pydantic creator model so that only fields with a None default (rather than any non-None default) become optional.
- Inverts the default check: now
Optionalis applied only whenfield.default is None. - Retains existing behavior for primary keys and nullable fields.
- No other files are modified.
Comments suppressed due to low confidence (3)
tortoise/contrib/pydantic/creator.py:528
- Add a unit test to verify that fields with a non-
Nonedefault no longer becomeOptional, and that fields withNonedefaults still do.
field_name in self._optional or field.default is None or field.null
tortoise/contrib/pydantic/creator.py:528
- [nitpick] Consider handling
default_factory(if supported by the field) similarly to explicit defaults, so fields with a factory-provided default aren’t incorrectly marked optional.
field_name in self._optional or field.default is None or field.null
tortoise/contrib/pydantic/creator.py:524
- [nitpick] Update or add a brief docstring explaining the combined logic for
default,nullable, and_optionalflags when determiningOptionaltyping for clarity.
ptype = python_type
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.
TODO: Add a test before open PR to the ORM repo
Description
Motivation and Context
How Has This Been Tested?
Checklist: