Skip to content

Mimic __getstate__ logic for dict parsing in pydantic serialization #486

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

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
13 changes: 9 additions & 4 deletions csp/impl/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,15 @@ def create_instance(raw_data, validator):

def serializer(val, handler):
# We don't use 'to_dict' since that works recursively, we ignore underscore leading fields
new_val = {
k: getattr(val, k) for k in val.__full_metadata_typed__ if not k.startswith("_") and hasattr(val, k)
}
return handler(new_val)
# This matches __getstate__, but excludes underscore fields
new_dict = {}
for k in val.__full_metadata_typed__:
if k.startswith("_"):
continue
v = getattr(val, k, csp.UNSET)
if v is not csp.UNSET:
new_dict[k] = v
return handler(new_dict)

return core_schema.no_info_wrap_validator_function(
function=create_instance,
Expand Down
Loading