Fatal1ty / mashumaro

Fast and well tested serialization library
Apache License 2.0
776 stars 45 forks source link

unable to serialize torch.tensor as float #238

Closed Ryang20718 closed 3 months ago

Ryang20718 commented 3 months ago

This snippet of code used to work on 2.9, but is broken on 3.13.1. Wondering if we were also exploiting a bug before?

# from mashumaro.mixins.msgpack import DataClassMessagePackMixin # (3.13.1)
from mashumaro import DataClassMessagePackMixin # 2.9
import torch

class MyClass(DataClassMessagePackMixin):
    tensor: float

y = MyClass()
y.tensor=torch.tensor(224.9999)
print(y.to_msgpack())
Ryang20718 commented 3 months ago

I guess this works

    tensor:float = field(
        metadata={
            "serialize": lambda v: float(v)
        }
    )