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

Converting an object / dict to a struct where the struct contains a Raw field fails with "Expected any, got ..." #699

Closed
indrat opened this issue Jun 5, 2024 · 0 comments · Fixed by #754

Comments

@indrat
Copy link

indrat commented Jun 5, 2024

Description

Description

Converting using msgspec.convert from an object with from_attributes=True to a struct that contains a Raw field doesn't appear to work.

Example

Given a struct like:

class Wrapper(msgspec.Struct):
    result: msgspec.Raw | msgspec.UnsetType = msgspec.UNSET

That is used to deserialise json successfully, e.g b'{"result": {"status": 0}}' => Wrapper(result=<msgspec.Raw object at 0x1028b3bb0>)

However going from an object or dict and trying to msgspec.convert it into a Wrapper doesn't appear to work or am I doing something wrong?

from types import SimpleNamespace
import msgspec

class Wrapper(msgspec.Struct):
    result: msgspec.Raw | msgspec.UnsetType = msgspec.UNSET

wrapped = {"status": 0}
raw_bytes = msgspec.json.encode(wrapped)
raw = msgspec.Raw(raw_bytes)

try:
    print("wrapped: raw")
    ns_a = SimpleNamespace(result=raw)
    print(ns_a)
    # expected that this would work?
    print(msgspec.convert(ns_a, type=Wrapper, from_attributes=True))
except Exception as e:
    print(e) # Expected `any`, got `msgspec.Raw` - at `$.result`

try:
    print("wrapped: nested namespace")
    ns_a = SimpleNamespace(result=SimpleNamespace(status=0))
    print(ns_a)
    # didn't really expect that this would work
    print(msgspec.convert(ns_a, type=Wrapper, from_attributes=True))
except Exception as e:
    print(e) # Expected `any`, got `types.SimpleNamespace` - at `$.result`

try:
    print("wrapped: bytes")
    ns_a = SimpleNamespace(result=raw_bytes)
    print(ns_a)
    # didn't really expect that this would work but thought it might
    print(msgspec.convert(ns_a, type=Wrapper, from_attributes=True))
except Exception as e:
    print(e) # Expected `any`, got `bytes` - at `$.result`

Expected

    ns_a = SimpleNamespace(result=msgspec.Raw(b'{"status":0}'))
    print(msgspec.convert(ns_a, type=Wrapper, from_attributes=True))
    # Wrapper(result=<msgspec.Raw object at 0x1028b3bb0>)`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant