Skip to content

Commit

Permalink
Merge pull request #438 from acolley-gel/main
Browse files Browse the repository at this point in the history
Fix frozenset union with prim #437
  • Loading branch information
yukinarit authored Oct 25, 2023
2 parents fead102 + 44274ce commit d9c19c1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion serde/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def is_list_instance(obj: Any, typ: Type[Any]) -> bool:


def is_set_instance(obj: Any, typ: Type[Any]) -> bool:
if not isinstance(obj, set):
if not isinstance(obj, (set, frozenset)):
return False
if len(obj) == 0 or is_bare_set(typ):
return True
Expand Down
11 changes: 10 additions & 1 deletion tests/test_union.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
from dataclasses import dataclass
from ipaddress import IPv4Address
from typing import Dict, Generic, List, NewType, Optional, Tuple, TypeVar, Union
from typing import Dict, FrozenSet, Generic, List, NewType, Optional, Tuple, TypeVar, Union
from uuid import UUID

import pytest
Expand Down Expand Up @@ -763,3 +763,12 @@ class Bar:
# untagged tagged
s = to_json(bar, cls=Untagged(Union[Foo, Bar]))
assert bar == from_json(Untagged(Union[Foo, Bar]), s)


def test_union_frozenset_with_prim():
@serde
@dataclass
class Foo:
a: Union[FrozenSet[int], int]

assert to_dict(Foo(frozenset({1}))) == {"a": {1}}

0 comments on commit d9c19c1

Please sign in to comment.