Skip to content

Cannot deserialise a str payload with mgspec.Raw #317

Description

@davfsa

Description

When deserialising a payload and specifying one of the fields of type msgspec.Raw, it prohibits passing payload as str.

Code to reproduce:

import msgspec

class Foo(msgspec.Struct):
    d: msgspec.Raw

data = '{"d": {"test": "A"}}'

decoder = msgspec.json.Decoder(Foo)
decoder.decode(data)

Output:

# python3 --version
Python 3.10.6

# python3 -m pip freeze | grep msgspec
msgspec==0.13.1

# python3 foo.py
Traceback (most recent call last):
  File "./foo.py", line 10, in <module>
    decoder.decode(data)
TypeError: a bytes-like object is required, not 'str'

Changing the payload to a bytes works as expected. Also worth noting that removing Raw and changing it to a proper type works also as expected.


I would like to have a go at fixing this bug, but don't know where to start, as there is no code that points to Raw not allowing a string (which if you pass a value directly to it, it works as expected):

>>> import msgspec
>>> msgspec.Raw("test")
<msgspec.Raw object at 0x7c3889bb70>

so will have to look further into it. Any help in the right direction is appreciated :)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions