question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to deserialize nested types of union and tuple

See original GitHub issue

Python 3.9 pyserde[toml]==0.3.1

from dataclasses import dataclass
from typing import Union
from serde import toml, deserialize


@deserialize
@dataclass
class TestClass:
    a: Union[None, Union[int, float], tuple[Union[int, float], Union[int, float]]]


if __name__ == '__main__':
    test_class = toml.from_toml(TestClass, 'a = [4, 1]')
    print(test_class)

Running the above code will throw an error:

serde.core.SerdeError: Can not deserialize [4, 1] of type List into Union[NoneType, int, float, Tuple[Union[int, float], Union[int, float]]]. Reason: 'union_de_int_float'

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ydyllacommented, Apr 13, 2021

It seems this loop runs only once. We need to expand union args and recursively generate function IMO. @ydylla What do you think?

Yes, but the recursion should happen in iter_unions there it should be:

if is_union(cls):
    yield cls
    for arg in type_args(cls):
        yield from iter_unions(arg)

I did not really test Unions in Unions, the only existing test is too simple and python is actually optimizing it away 😃 Sadly the fix in iter_unions produces a follow up issue. A nested tuple is flattened, but it shouldn’t.

@deserialize
@serialize
@dataclass
class B:
    v: Union[UUID, Tuple[Union[UUID, int]]]

to_dict(B((1,)))
# becomes {'v': 1} but should be {'v': (1,)}

from_dict(B,{"v":(1,)})
# becomes B(v=1) but should be B(v=(1,))

We probably need an own render mode that is used within union functions similar to DeField.iterbased to prevent this.

0reactions
yukinaritcommented, May 7, 2021

Hi @uheee

Just wanted to let you know v0.3.2 is pushed to PyPI! 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - JSON Deserialization Despair (unable to ... - Stack Overflow
You basically need to tell the Serializer all the types it may encounter while serializing/deserializing your object.
Read more >
Developers using JsonSerializer want it to handle common F# ...
We should make sure that the JSON serializer can handle types / structures that commonly occur in F#, such as Discriminated unions Option ......
Read more >
Jackson Exceptions - Problems and Solutions - Baeldung
Here we'll try to deserialize an instance from class Zoo that has a ... This exception is thrown if Jackson can't access the...
Read more >
Migrate from Newtonsoft.Json to System.Text.Json - .NET
Polymorphic deserialization. Newtonsoft.Json has a TypeNameHandling setting that adds type-name metadata to the JSON while serializing. It uses ...
Read more >
Body - Nested Models - FastAPI
Declare a list with a type parameter. To declare types that have type parameters (internal types), like list , dict , tuple :...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found