We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
any
Converting using msgspec.convert from an object with from_attributes=True to a struct that contains a Raw field doesn't appear to work.
msgspec.convert
from_attributes=True
Raw
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>)
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?
Wrapper
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`
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>)`
The text was updated successfully, but these errors were encountered:
convert
Successfully merging a pull request may close this issue.
Description
Description
Converting using
msgspec.convert
from an object withfrom_attributes=True
to a struct that contains aRaw
field doesn't appear to work.Example
Given a struct like:
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 aWrapper
doesn't appear to work or am I doing something wrong?Expected
The text was updated successfully, but these errors were encountered: