jcrist / msgspec

A fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML
https://jcristharif.com/msgspec/
BSD 3-Clause "New" or "Revised" License
2.3k stars 67 forks source link

Bug: dataclass with frozen and slots fails if defaults not provided #567

Closed guacs closed 11 months ago

guacs commented 11 months ago

Description

The following will fail:

from dataclasses import dataclass
import msgspec

@dataclass(frozen=True, slots=True)
class Foo:
    foo_value: int = 0

foo = msgspec.convert({}, Foo) # fails

The traceback:

Traceback (most recent call last):
  File "~/foo.py", line 9, in <module>
    foo = msgspec.convert({}, Foo)
          ^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 4, in __setattr__
dataclasses.FrozenInstanceError: cannot assign to field 'foo_value'

If the value for foo_value is provided in the initial dictionary, there is no error. Also, the error is only raised if slots=True is present.

jcrist commented 11 months ago

Thanks for the excellent reproducible issue, this has been resolved in #569. I'll try to make a release in the next week or so.