Skip to content

Commit ab40cc9

Browse files
committed
fix: pass convert_sets argument to union functions
1 parent a8a6456 commit ab40cc9

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

serde/se.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def {{func}}(obj, reuse_instances = {{serde_scope.reuse_instances_default}}, con
370370

371371
def render_union_func(cls: Type, union_args: List[Type]) -> str:
372372
template = """
373-
def {{func}}(obj, reuse_instances):
373+
def {{func}}(obj, reuse_instances, convert_sets):
374374
{% for name in serde_scope.types.keys() %}
375375
{{name}} = serde_scope.types['{{name}}']
376376
{% endfor %}
@@ -553,7 +553,7 @@ def str(self, arg: SeField) -> str:
553553

554554
def union_func(self, arg: SeField) -> str:
555555
func_name = union_func_name(UNION_SE_PREFIX, type_args(arg.type))
556-
return f"serde_scope.funcs['{func_name}']({arg.varname}, reuse_instances)"
556+
return f"serde_scope.funcs['{func_name}']({arg.varname}, reuse_instances, convert_sets)"
557557

558558

559559
def enum_value(cls, e):

tests/test_union.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
from dataclasses import dataclass
33
from ipaddress import IPv4Address
4-
from typing import Dict, List, Optional, Union, Tuple
4+
from typing import Dict, List, Optional, Tuple, Union
55
from uuid import UUID
66

77
import pytest
@@ -292,6 +292,25 @@ class Foo:
292292
assert from_dict(Foo, {'BarBaz': 'foo'}) == Foo('foo')
293293

294294

295+
def test_union_with_list_of_other_class():
296+
@deserialize
297+
@serialize
298+
@dataclass
299+
class A:
300+
a: int
301+
302+
@deserialize
303+
@serialize
304+
@dataclass
305+
class B:
306+
b: Union[List[A], str]
307+
308+
b = B([A(1)])
309+
b_dict = {"b": [{"a": 1}]}
310+
assert to_dict(b) == b_dict
311+
assert from_dict(B, b_dict) == b
312+
313+
295314
# relates to https://github.com/yukinarit/pyserde/issues/113
296315
def test_union_with_union_in_nested_types():
297316
@deserialize

0 commit comments

Comments
 (0)