pyserde
pyserde copied to clipboard
`alias` broken for `list`
Hi there,
Back again with another potential bug. Using [email protected]
, I've run into deserialization failures for two situations I think should be valid:
@serde
@dataclass
class Foo:
a: Optional[list[int]] = field(alias=["b"], default=None)
x = from_json(Foo, '{"b": [1]}')
assert x.a == [1] 💥
@serde
@dataclass
class Foo:
a: list[int] = field(alias=["b"], default_factory=list)
x = from_json(Foo, '{"b": [1]}')
assert x.a == [1] 💥
Both fail with serde.compat.SerdeError: 'a'
, I wish I could give you a more useful error message.
Hi @opeik
Sorry, I don't have bandwidth to look into this issue. If you're interested in contributing, I am happy to assist you.
Hey @yukinarit, don't stress. I would never demand maintainers fix my problems. Thanks for taking a look 🙇♀️
Just wanted to add to this:
Also doesn't seem to work for nested types
@serde
@dataclass
class Bar:
e: int
f: str
@serde
@dataclass
class Foo:
a: Bar | None = field(default=None, alias=["b"])
# works
s = '{"a": {"e": 5, "f": "baz"}}'
print(f"From Json: {from_json(Foo, s)}")
# works
s = '{"b": null}'
print(f"From Json: {from_json(Foo, s)}")
# doesnt work
s = '{"b": {"e": 5, "f": "baz"}}'
print(f"From Json: {from_json(Foo, s)}")