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

Fix dict[str, Any] serialization #618

Merged
merged 1 commit into from
Dec 6, 2024
Merged

Conversation

kmsquire
Copy link
Collaborator

@kmsquire kmsquire commented Dec 4, 2024

When a dict inside a class had values of type Any and the dict contained non-primitive types, pyserde would enter an infinite recusion. See #617 for details.

The fix here causes non-primitive values to be serialized as strings when calling to_dict. Arguably, this loses information, as a dict should be able to store the values without serializing them to strings first. However, this same behavior exists when a dataclass variable is typed as Any and contains a non-primitive object (pyserde serializes it to a string):

import serde
import uuid
from typing import Any

@serde.serde
class A:
    a: Any

foo = A(uuid.UUID('05b69d9a-3783-46ea-a811-35fa4b72ddac'))
print(to_dict(a))  # {'a': '05b69d9a-3783-46ea-a811-35fa4b72ddac'}

Fixes #617

@kmsquire kmsquire requested a review from yukinarit December 4, 2024 01:47
@kmsquire kmsquire self-assigned this Dec 4, 2024
    When a dict inside a class had values of type Any and the
    dict contained non-primitive types, pyserde would enter
    an infinite recusion.  See #617 for details.

    The fix here causes non-primitive values to be serialized
    as strings when calling to_dict.  Arguably, this loses
    information, as a dict should be able to store the values
    without serializing them to strings first.  However, this
    same behavior exists when a dataclass variable is typed
    as Any and contains a non-primitive object (pyserde
    serializes it to a string):

    ```
    import serde
    import uuid
    from typing import Any

    @serde.serde
    class A:
        a: Any

    foo = A(uuid.UUID('05b69d9a-3783-46ea-a811-35fa4b72ddac'))
    print(to_dict(a))  # {'a': '05b69d9a-3783-46ea-a811-35fa4b72ddac'}
    ```
@kmsquire kmsquire force-pushed the feature/fix-dict-str-any branch from bbe8c61 to 613c4f5 Compare December 4, 2024 02:20
return CACHE.serialize(
c or o.__class__,
se_cls,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If Any is passed in this argument, it will be an infinite recursion?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

@yukinarit yukinarit added the bug Bug report or fix label Dec 5, 2024
Copy link
Owner

@yukinarit yukinarit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yukinarit yukinarit merged commit 1562940 into main Dec 6, 2024
7 checks passed
@yukinarit yukinarit deleted the feature/fix-dict-str-any branch December 6, 2024 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug report or fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pyserde unable to serialize a UUID inside of a dict[str, Any] in an object
2 participants