-
Notifications
You must be signed in to change notification settings - Fork 292
Add option to exclude a field using a callable #1535
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
base: main
Are you sure you want to change the base?
Changes from all commits
66da277
68955e6
b225197
a7d272b
2ac82ca
a836948
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2817,7 +2817,8 @@ class TypedDictField(TypedDict, total=False): | |||||
validation_alias: Union[str, List[Union[str, int]], List[List[Union[str, int]]]] | ||||||
serialization_alias: str | ||||||
serialization_exclude: bool # default: False | ||||||
metadata: Dict[str, Any] | ||||||
exclude_if: Callable[[Any], bool] # default None | ||||||
metadata: Any | ||||||
|
||||||
|
||||||
def typed_dict_field( | ||||||
|
@@ -2827,7 +2828,8 @@ def typed_dict_field( | |||||
validation_alias: str | list[str | int] | list[list[str | int]] | None = None, | ||||||
serialization_alias: str | None = None, | ||||||
serialization_exclude: bool | None = None, | ||||||
metadata: Dict[str, Any] | None = None, | ||||||
exclude_if: Callable[[Any], bool] | None = None, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
metadata: Any = None, | ||||||
) -> TypedDictField: | ||||||
""" | ||||||
Returns a schema that matches a typed dict field, e.g.: | ||||||
|
@@ -2844,6 +2846,7 @@ def typed_dict_field( | |||||
validation_alias: The alias(es) to use to find the field in the validation data | ||||||
serialization_alias: The alias to use as a key when serializing | ||||||
serialization_exclude: Whether to exclude the field when serializing | ||||||
exclude_if: Callable that determines whether to exclude a field during serialization based on its value. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
metadata: Any other information you want to include with the schema, not used by pydantic-core | ||||||
""" | ||||||
return _dict_not_none( | ||||||
|
@@ -2853,6 +2856,7 @@ def typed_dict_field( | |||||
validation_alias=validation_alias, | ||||||
serialization_alias=serialization_alias, | ||||||
serialization_exclude=serialization_exclude, | ||||||
exclude_if=exclude_if, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
metadata=metadata, | ||||||
) | ||||||
|
||||||
|
@@ -2943,6 +2947,7 @@ class ModelField(TypedDict, total=False): | |||||
validation_alias: Union[str, List[Union[str, int]], List[List[Union[str, int]]]] | ||||||
serialization_alias: str | ||||||
serialization_exclude: bool # default: False | ||||||
exclude_if: Callable[[Any], bool] # default: None | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
frozen: bool | ||||||
metadata: Dict[str, Any] | ||||||
|
||||||
|
@@ -2953,6 +2958,7 @@ def model_field( | |||||
validation_alias: str | list[str | int] | list[list[str | int]] | None = None, | ||||||
serialization_alias: str | None = None, | ||||||
serialization_exclude: bool | None = None, | ||||||
exclude_if: Callable[[Any], bool] | None = None, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
frozen: bool | None = None, | ||||||
metadata: Dict[str, Any] | None = None, | ||||||
) -> ModelField: | ||||||
|
@@ -2970,6 +2976,7 @@ def model_field( | |||||
validation_alias: The alias(es) to use to find the field in the validation data | ||||||
serialization_alias: The alias to use as a key when serializing | ||||||
serialization_exclude: Whether to exclude the field when serializing | ||||||
exclude_if: Callable that determines whether to exclude a field during serialization based on its value. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
frozen: Whether the field is frozen | ||||||
metadata: Any other information you want to include with the schema, not used by pydantic-core | ||||||
""" | ||||||
|
@@ -2979,6 +2986,7 @@ def model_field( | |||||
validation_alias=validation_alias, | ||||||
serialization_alias=serialization_alias, | ||||||
serialization_exclude=serialization_exclude, | ||||||
exclude_if=exclude_if, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
frozen=frozen, | ||||||
metadata=metadata, | ||||||
) | ||||||
|
@@ -3171,7 +3179,8 @@ class DataclassField(TypedDict, total=False): | |||||
validation_alias: Union[str, List[Union[str, int]], List[List[Union[str, int]]]] | ||||||
serialization_alias: str | ||||||
serialization_exclude: bool # default: False | ||||||
metadata: Dict[str, Any] | ||||||
exclude_if: Callable[[Any], bool] # default: None | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
metadata: Any | ||||||
|
||||||
|
||||||
def dataclass_field( | ||||||
|
@@ -3184,7 +3193,8 @@ def dataclass_field( | |||||
validation_alias: str | list[str | int] | list[list[str | int]] | None = None, | ||||||
serialization_alias: str | None = None, | ||||||
serialization_exclude: bool | None = None, | ||||||
metadata: Dict[str, Any] | None = None, | ||||||
exclude_if: Callable[[Any], bool] | None = None, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
metadata: Any = None, | ||||||
frozen: bool | None = None, | ||||||
) -> DataclassField: | ||||||
""" | ||||||
|
@@ -3210,6 +3220,7 @@ def dataclass_field( | |||||
validation_alias: The alias(es) to use to find the field in the validation data | ||||||
serialization_alias: The alias to use as a key when serializing | ||||||
serialization_exclude: Whether to exclude the field when serializing | ||||||
exclude_if: Callable that determines whether to exclude a field during serialization based on its value. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
metadata: Any other information you want to include with the schema, not used by pydantic-core | ||||||
frozen: Whether the field is frozen | ||||||
""" | ||||||
|
@@ -3223,6 +3234,7 @@ def dataclass_field( | |||||
validation_alias=validation_alias, | ||||||
serialization_alias=serialization_alias, | ||||||
serialization_exclude=serialization_exclude, | ||||||
exclude_if=exclude_if, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
metadata=metadata, | ||||||
frozen=frozen, | ||||||
) | ||||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -29,6 +29,7 @@ pub(super) struct SerField { | |||
// None serializer means exclude | ||||
pub serializer: Option<CombinedSerializer>, | ||||
pub required: bool, | ||||
pub exclude_if: Option<Py<PyAny>>, | ||||
} | ||||
|
||||
impl_py_gc_traverse!(SerField { serializer }); | ||||
|
@@ -40,6 +41,7 @@ impl SerField { | |||
alias: Option<String>, | ||||
serializer: Option<CombinedSerializer>, | ||||
required: bool, | ||||
exclude_if: Option<Py<PyAny>>, | ||||
) -> Self { | ||||
let alias_py = alias | ||||
.as_ref() | ||||
|
@@ -50,6 +52,7 @@ impl SerField { | |||
alias_py, | ||||
serializer, | ||||
required, | ||||
exclude_if, | ||||
} | ||||
} | ||||
|
||||
|
@@ -72,6 +75,18 @@ impl SerField { | |||
} | ||||
} | ||||
|
||||
fn exclude_if(exclude_if_callable: &Option<Py<PyAny>>, value: &Bound<'_, PyAny>) -> PyResult<bool> { | ||||
if let Some(exclude_if_callable) = exclude_if_callable { | ||||
let py = value.py(); | ||||
let result = exclude_if_callable.call1(py, (value,))?; | ||||
let exclude = result.extract::<bool>(py)?; | ||||
if exclude { | ||||
return Ok(true); | ||||
} | ||||
} | ||||
Ok(false) | ||||
} | ||||
|
||||
fn exclude_default(value: &Bound<'_, PyAny>, extra: &Extra, serializer: &CombinedSerializer) -> PyResult<bool> { | ||||
if extra.exclude_defaults { | ||||
if let Some(default) = serializer.get_default(value.py())? { | ||||
|
@@ -80,6 +95,7 @@ fn exclude_default(value: &Bound<'_, PyAny>, extra: &Extra, serializer: &Combine | |||
} | ||||
} | ||||
} | ||||
// If neither condition is met, do not exclude the field | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets avoid diff noise:
Suggested change
|
||||
Ok(false) | ||||
} | ||||
|
||||
|
@@ -176,16 +192,16 @@ impl GeneralFieldsSerializer { | |||
if let Some((next_include, next_exclude)) = self.filter.key_filter(&key, include, exclude)? { | ||||
if let Some(field) = op_field { | ||||
if let Some(ref serializer) = field.serializer { | ||||
if !exclude_default(&value, &field_extra, serializer)? { | ||||
let value = serializer.to_python( | ||||
&value, | ||||
next_include.as_ref(), | ||||
next_exclude.as_ref(), | ||||
&field_extra, | ||||
)?; | ||||
let output_key = field.get_key_py(output_dict.py(), &field_extra); | ||||
output_dict.set_item(output_key, value)?; | ||||
if exclude_default(&value, &field_extra, serializer)? { | ||||
continue; | ||||
} | ||||
if exclude_if(&field.exclude_if, &value)? { | ||||
continue; | ||||
} | ||||
let value = | ||||
serializer.to_python(&value, next_include.as_ref(), next_exclude.as_ref(), &field_extra)?; | ||||
let output_key = field.get_key_py(output_dict.py(), &field_extra); | ||||
output_dict.set_item(output_key, value)?; | ||||
} | ||||
|
||||
if field.required { | ||||
|
@@ -263,17 +279,21 @@ impl GeneralFieldsSerializer { | |||
if let Some((next_include, next_exclude)) = filter { | ||||
if let Some(field) = self.fields.get(key_str) { | ||||
if let Some(ref serializer) = field.serializer { | ||||
if !exclude_default(&value, &field_extra, serializer).map_err(py_err_se_err)? { | ||||
let s = PydanticSerializer::new( | ||||
&value, | ||||
serializer, | ||||
next_include.as_ref(), | ||||
next_exclude.as_ref(), | ||||
&field_extra, | ||||
); | ||||
let output_key = field.get_key_json(key_str, &field_extra); | ||||
map.serialize_entry(&output_key, &s)?; | ||||
if exclude_default(&value, &field_extra, serializer).map_err(py_err_se_err)? { | ||||
continue; | ||||
} | ||||
if exclude_if(&field.exclude_if, &value).map_err(py_err_se_err)? { | ||||
continue; | ||||
} | ||||
let s = PydanticSerializer::new( | ||||
&value, | ||||
serializer, | ||||
next_include.as_ref(), | ||||
next_exclude.as_ref(), | ||||
&field_extra, | ||||
); | ||||
let output_key = field.get_key_json(key_str, &field_extra); | ||||
map.serialize_entry(&output_key, &s)?; | ||||
} | ||||
} else if self.mode == FieldsMode::TypedDictAllow { | ||||
let output_key = infer_json_key(&key, &field_extra).map_err(py_err_se_err)?; | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.