Skip to content

Repeated serde cycles yield different bytes with MessagePackEncoder #287

@JeffGreen

Description

@JeffGreen
  • mashumaro version: 3.16
  • Python version: 3.12
  • Operating System: Ubuntu

Description

At least with the MessagePackEncoder, the library seems to encode variables with their -actual- type instead of their annotated type. In the example below, I pass the value 1 into a dataclass that has a type annotation of float. This "breaks" serialization in the sense that the first time we encode the object, it's encoded (incorrectly) as an int, but when we decode it, it's decoded as a float and thus repeated cycles of encoding and decoding of the "same" object result in different byte streams.

What I Did

from dataclasses import dataclass

from mashumaro.codecs.msgpack import MessagePackDecoder, MessagePackEncoder
from mashumaro.mixins.msgpack import DataClassMessagePackMixin


@dataclass
class TestClass(DataClassMessagePackMixin):
    my_var: float

enc = MessagePackEncoder(shape_type=TestClass)
enc_bytes = enc.encode(TestClass(1))
print(enc_bytes) # prints b'\x81\xa6my_var\x01'

dec = MessagePackDecoder(shape_type=TestClass)
decoded = dec.decode(enc_bytes)
print(decoded)

re_enc_bytes = enc.encode(decoded)
print(re_enc_bytes) # now prints b'\x81\xa6my_var\xcb?\xf0\x00\x00\x00\x00\x00\x00'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions