Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to keep the types as they were and just add a default value #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pydantic_partial/partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def create_partial_model(
base_cls: type[SelfT],
*fields: str,
recursive: bool = False,
optional: bool = True,
) -> type[SelfT]:
# Convert one type to being partial - if possible
def _partial_annotation_arg(field_name_: str, field_annotation: type) -> type:
Expand Down Expand Up @@ -104,8 +105,15 @@ def _partial_annotation_arg(field_name_: str, field_annotation: type) -> type:
# Construct new field definition
if field_name in fields_:
if model_compat.is_model_field_info_required(field_info):
# Allow to keep the types as they were and just add a
# default value
if optional:
annotation = Optional[field_annotation]
else:
annotation = field_annotation

optional_fields[field_name] = (
Optional[field_annotation],
annotation,
model_compat.copy_model_field_info(
field_info,
default=None, # Set default to None
Expand Down