I need to serialize a dataclass that has IntFlag fields and use the omit_default option, but it results in a SyntaxError unless the field is declared without a default.
What I Did
Minimal example:
from dataclasses import dataclass
from enum import IntFlag, auto
from mashumaro.config import BaseConfig
from mashumaro.mixins.dict import DataClassDictMixin
class Flags(IntFlag):
a = auto()
@dataclass
class Data(DataClassDictMixin):
flags: Flags = Flags.a
class Config(BaseConfig):
omit_default = True
print(Data().to_dict())
Result:
Traceback (most recent call last):
File "/tmp/m.py", line 13, in <module>
class Data(DataClassDictMixin):
File "/nix/store/m5i9ys5n65cc01955ngifyyy9nd3nd56-python3.11-mashumaro-3.11/lib/python3.11/site-packages/mashumaro/mixins/dict.py", line 25, in __init_subclass__
compile_mixin_packer(cls, **builder_params["packer"])
File "/nix/store/m5i9ys5n65cc01955ngifyyy9nd3nd56-python3.11-mashumaro-3.11/lib/python3.11/site-packages/mashumaro/core/meta/mixin.py", line 29, incompile_mixin_packer
builder.add_pack_method()
File "/nix/store/m5i9ys5n65cc01955ngifyyy9nd3nd56-python3.11-mashumaro-3.11/lib/python3.11/site-packages/mashumaro/core/meta/code/builder.py", line1189, in add_pack_method
self.compile()
File "/nix/store/m5i9ys5n65cc01955ngifyyy9nd3nd56-python3.11-mashumaro-3.11/lib/python3.11/site-packages/mashumaro/core/meta/code/builder.py", line306, in compile
exec(code, self.globals, self.__dict__)
File "<string>", line 4
if value != <Flags.a: 1>:
^
SyntaxError: invalid syntax
Description
I need to serialize a dataclass that has
IntFlag
fields and use theomit_default
option, but it results in aSyntaxError
unless the field is declared without a default.What I Did
Minimal example:
Result: