Repository navigation
fix(auto): dump by field name; leave excluded fields out of evaluation #380
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
Changes from 1 commit
3dbeed9
cbd3aa0
8b6bcdd
10c60a0
9477178
ec3cf4b
716b03f
cbd8c4b
d258055
13bc7d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- An excluded value pydantic cannot serialize stays out instead of aborting evaluate(). - Excluded fields are compared inside dicts and nested lists, and on a StructuredModel (keeping its own key spelling). - A field serializer, model_serializer or RootModel keeps pydantic's output. - A model-holding field is left out of the parent dump rather than serialized twice. - The exclude_if test skips on a pydantic without it.
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,10 +18,11 @@ | |
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from typing import Any, Dict, List, Optional, Type, Union | ||
|
|
||
| from pydantic import BaseModel | ||
| from pydantic_core import to_jsonable_python | ||
| from pydantic import BaseModel, PlainSerializer, RootModel, WrapSerializer | ||
| from pydantic_core import PydanticSerializationError, to_jsonable_python | ||
|
|
||
| from ..structured_object_evaluator.models.structured_model import StructuredModel | ||
| from .builder import specs_for, structured_model_for | ||
|
|
@@ -330,36 +331,83 @@ def _dump(instance: BaseModel) -> Dict[str, Any]: | |
| return _wire(instance) | ||
|
|
||
|
|
||
| def _wire(value: Any) -> Any: | ||
| """Dump ``value`` keyed by field name, keeping every declared field. | ||
| def _wire(value: Any, own_keys: bool = False) -> Any: | ||
| """Dump ``value`` in JSON form, keeping every declared field. | ||
|
|
||
| A ``StructuredModel`` validates back through its own class, so its own dump | ||
| round-trips. A plain model feeds a shadow model, which reads field names, | ||
| so ``by_alias=False`` overrides ``serialize_by_alias=True`` (#378). A field | ||
| the dump leaves out (``exclude=True``, ``exclude_if``) is still compared, | ||
| so it is rebuilt from the attribute rather than read as blank (#379). So | ||
| is any field holding a model, since a nested model can drop fields too. | ||
| A field the dump leaves out (``exclude=True``, ``exclude_if``) is blank on | ||
| both sides, and blank against blank scores as a match, so it is rebuilt | ||
| from the attribute (#379). So is a field holding a model, since a nested | ||
| model can drop fields too; that field is left out of the parent's dump | ||
| rather than serialized twice. | ||
|
|
||
| Keys: a plain model feeds a shadow model, which reads field names, so it | ||
| dumps with ``by_alias=False`` (#378). A ``StructuredModel`` validates | ||
| through its own classes, so it and everything under it (``own_keys``) | ||
| keep their own config's key spelling. | ||
|
|
||
| A field with its own serializer, a model with a ``model_serializer`` and a | ||
| ``RootModel`` keep pydantic's output: the author chose that wire form. | ||
| """ | ||
| if isinstance(value, StructuredModel): | ||
| return value.model_dump(mode="json") | ||
| if isinstance(value, BaseModel): | ||
| out = value.model_dump(mode="json", by_alias=False) | ||
| for name in type(value).model_fields: | ||
| raw = getattr(value, name) | ||
| if name not in out or _holds_model(raw): | ||
| out[name] = _wire(raw) | ||
| own_keys = own_keys or isinstance(value, StructuredModel) | ||
| dump_kwargs = {} if own_keys else {"by_alias": False} | ||
| cls = type(value) | ||
| if ( | ||
| isinstance(value, RootModel) | ||
| or cls.__pydantic_decorators__.model_serializers | ||
| ): | ||
| return value.model_dump(mode="json", **dump_kwargs) | ||
| rebuild = { | ||
| name | ||
| for name, field in cls.model_fields.items() | ||
| if _holds_model(getattr(value, name)) | ||
| and not _has_field_serializer(cls, name, field) | ||
| } | ||
| out = value.model_dump(mode="json", exclude=rebuild or None, **dump_kwargs) | ||
| by_alias = own_keys and bool(cls.model_config.get("serialize_by_alias")) | ||
| for name, field in cls.model_fields.items(): | ||
| key = ( | ||
| (field.serialization_alias or field.alias or name) if by_alias else name | ||
| ) | ||
| if name in rebuild: | ||
|
Contributor
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. Medium: excluded model-holding fields abort instead of staying out. A field that is both Reproduction (dev returns class Handle: pass
class Inner(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)
h: Handle
class Doc(BaseModel):
name: str
meta: Optional[Inner] = Field(default=None, exclude=True)
d = Doc(name="a", meta=Inner(h=Handle()))
stickler.evaluate(d, d)Fix: apply the same guard when
Contributor
Author
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. Fixed. |
||
| out[key] = _wire(getattr(value, name), own_keys) | ||
| elif key not in out: | ||
|
Contributor
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. Medium: Measured on this branch with a passthrough a = SM.from_json({"name": "x", "zzz": 1})
_dump(a) # {'name': 'x', 'zzz': 1, 'extra_fields': {'zzz': 1}}
SM.from_json(_dump(a)).extra_fields # {'extra_fields': {'zzz': 1}, 'zzz': 1}The same happens for a
Contributor
Author
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. Good catch, thanks. Fixed in |
||
| try: | ||
| out[key] = _wire(getattr(value, name), own_keys) | ||
| except PydanticSerializationError: | ||
| # A type pydantic cannot serialize could not be compared | ||
| # anyway; excluding it is how such fields are usually kept | ||
| # out of a dump, so leave it out rather than abort. | ||
| pass | ||
| return out | ||
| if isinstance(value, (list, tuple)) and _holds_model(value): | ||
| return [_wire(item) for item in value] | ||
| return to_jsonable_python(value, by_alias=False) | ||
| if not _holds_model(value): | ||
| return to_jsonable_python(value) | ||
| if isinstance(value, dict): | ||
| return {_json_key(k): _wire(v, own_keys) for k, v in value.items()} | ||
| return [_wire(item, own_keys) for item in value] | ||
|
|
||
|
|
||
| def _holds_model(value: Any) -> bool: | ||
| if isinstance(value, BaseModel): | ||
| return True | ||
| return isinstance(value, (list, tuple)) and any( | ||
| isinstance(item, BaseModel) for item in value | ||
| ) | ||
| if isinstance(value, dict): | ||
| return any(_holds_model(v) for v in value.values()) | ||
| if isinstance(value, (list, tuple)): | ||
| return any(_holds_model(item) for item in value) | ||
| return False | ||
|
|
||
|
|
||
| def _has_field_serializer(cls: Type[BaseModel], name: str, field: Any) -> bool: | ||
| decorators = cls.__pydantic_decorators__.field_serializers.values() | ||
| return any( | ||
| name in d.info.fields or "*" in d.info.fields for d in decorators | ||
| ) or any(isinstance(m, (PlainSerializer, WrapSerializer)) for m in field.metadata) | ||
|
|
||
|
|
||
| def _json_key(key: Any) -> str: | ||
| """Spell a mapping key the way ``model_dump(mode="json")`` does.""" | ||
| key = to_jsonable_python(key) | ||
| return key if isinstance(key, str) else json.dumps(key) | ||
|
|
||
|
|
||
| def _shared_class(gt: BaseModel, pred: BaseModel) -> Type[BaseModel]: | ||
|
|
||
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.
#378 is still open for any
StructuredModelthat usesserialization_alias. Keys under aStructuredModelare kept as dumped because it "validates through its own config". Pydantic validation readsalias/validation_alias, notserialization_alias, so those keys are never read back. Measured on this branch:class SM3(StructuredModel): model_config = ConfigDict(serialize_by_alias=True); code: str = Field("", serialization_alias="Code"); other: str = "", thenevaluate(SM3(code="A", other="q"), SM3(code="BBBB", other="q"))givesfield_scores={'code': 1.0, ...}.compare_with()scorescode0.0. A plain model nested under aStructuredModelhas the same problem (wrongInner.codescoressm: 1.0). Fix: rename a key to the field's validation key (aliasor name) wheneverdumped_keydiffers from it, not only whenown_keysis false.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.
You're right, thanks. Under a
StructuredModelthe key is now the one validation reads: an alias stays, and aserialization_aliasbecomes the field name.SM3scorescode0.0 in both readers, and the nested plain case drops below 1.0. Seetest_a_serialization_alias_is_read_back.