pyserde icon indicating copy to clipboard operation
pyserde copied to clipboard

`alias` broken for `list`

Open opeik opened this issue 1 year ago • 3 comments

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.

opeik avatar Oct 09 '23 04:10 opeik

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.

yukinarit avatar Oct 18 '23 12:10 yukinarit

Hey @yukinarit, don't stress. I would never demand maintainers fix my problems. Thanks for taking a look 🙇‍♀️

opeik avatar Oct 20 '23 03:10 opeik

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)}")

kaiaw avatar Jan 26 '24 12:01 kaiaw