Skip to content

Commit 6ac1238

Browse files
authored
Merge pull request #19 from joshorr/josho/fix-dump-as-missing-when-we-have-real-value
Josho/fix dump as missing when we have real value
2 parents f4f4030 + 9f754f7 commit 6ac1238

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

pydantic_partials/meta.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ def __new__(
9595
raise ValueError(f'Invalid/Unknown `partial_auto` config value ({final_partial_auto}), use bool value.')
9696

9797
for k, v in cls.model_fields.items():
98+
if k in partial_fields:
99+
# The field is already a Partial
100+
continue
101+
98102
if v.default is PydanticUndefined and v.default_factory is None:
99103
v.annotation = v.annotation | MissingType
100104
partial_fields.add(k)

pydantic_partials/partial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class PartialModel(
3232
"""
3333
Class Args:
3434
35-
- auto_partial: For more details see `pydantic_partials.config.PartialConfigDict.auto_partials`.
35+
- auto_partials: For more details see `pydantic_partials.config.PartialConfigDict.auto_partials`.
3636
- If `Default`: Inherit behavior from parent/model_config; otherwise defaults to `True`.
3737
- If `True` (default): Will automatically make all fields on the model `Partial`.
3838
- If `False`: User needs to mark individual fields as `Partial` where they want.

pydantic_partials/sentinels.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,26 @@ def __get_pydantic_core_schema__(
2828

2929
@staticmethod
3030
def _validate(value: Any, info: core_schema.ValidationInfo) -> 'MissingType':
31-
# Tells Pydantic that our Sentinel value 'Missing' is our validated value.
32-
# return Missing
31+
if value is Missing:
32+
# Keeps the associated attribute 'deleted/omitted' from model.
33+
raise PydanticOmit()
3334

34-
# Keeps the associated attribute 'deleted/omitted' from model.
35-
#
36-
raise PydanticOmit()
35+
# `value` is not Missing, return it unchanged.
36+
return value
3737

3838
@staticmethod
3939
def _serialize(value: Any) -> 'MissingType':
4040
# Keeps the associated attribute 'deleted/omitted' from model.
4141
# raise PydanticOmit()
42-
# return 'a'
43-
return Missing
42+
43+
# Return same value we got, when requested to try and serialize real data it's not 'Missing',
44+
# and if it is, that means `Value` is `Missing` and we are fine to return that also.
45+
# I would love to rase a `raise PydanticOmit()` if `value` is `Missing`, but that does not currently work
46+
# for Pydantic, so I just return the `Missing` unchanged for now.
47+
# If there is a serialization error later on about how Pydantic can't turn `Missing` into a `Json`
48+
# then we can debug the situation
49+
# (right now PartialModel should delete any attributes that are set to `Missing` to work around this limitation)
50+
return value
4451

4552

4653
Missing = MissingType()

0 commit comments

Comments
 (0)