Skip to content

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
fix/pydantic-creator-dont-mark-fields-optional-if-default-value-is-not-null
Draft

Fix Pydantic Creator Model: Don't mark fields optional if default value is not null#5
fullonic wants to merge 2 commits intofeature/allow-pass-configuration-for-ToTsQueryfrom
fix/pydantic-creator-dont-mark-fields-optional-if-default-value-is-not-null

Conversation

@fullonic
Copy link
Collaborator

@fullonic fullonic commented Jul 2, 2025

TODO: Add a test before open PR to the ORM repo

Description

Motivation and Context

How Has This Been Tested?

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@fullonic fullonic requested a review from Copilot July 2, 2025 08:12
@fullonic fullonic self-assigned this Jul 2, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Optional is applied only when field.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-None default no longer become Optional, and that fields with None defaults 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 _optional flags when determining Optional typing for clarity.
        ptype = python_type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant