Skip to content

Commit

Permalink
Fix #7, bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
brentyi committed Sep 1, 2022
1 parent 6356df9 commit 40e01c7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
11 changes: 9 additions & 2 deletions dcargs/extras/_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import dataclasses
import enum
import functools
from typing import IO, Any, Optional, Set, Type, TypeVar, Union

import yaml
from typing_extensions import get_origin
from typing_extensions import get_args, get_origin

from .. import _fields, _resolver

Expand Down Expand Up @@ -48,15 +49,21 @@ def _get_contained_special_types_from_type(
contained_dataclasses = {cls}

def handle_type(typ) -> Set[Type]:
print(typ)
# Handle dataclasses.
if _resolver.is_dataclass(typ) and typ not in parent_contained_dataclasses:
return _get_contained_special_types_from_type(
typ,
_parent_contained_dataclasses=contained_dataclasses
| parent_contained_dataclasses,
)

# Handle enums.
elif type(typ) is enum.EnumMeta:
return {typ}
return set()

# Handle Union, Annotated, List, etc. No-op when there are no args.
return functools.reduce(set.union, map(handle_type, get_args(typ)), set())

# Handle generics.
for typ in type_from_typevar.values():
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dcargs"
version = "0.2.6"
version = "0.2.7"
description = "Strongly typed, zero-effort CLI interfaces"
authors = ["brentyi <[email protected]>"]
include = ["./dcargs/**/*"]
Expand Down
41 changes: 41 additions & 0 deletions tests/test_generics_and_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Generic, List, Tuple, Type, TypeVar, Union

import pytest
from typing_extensions import Annotated

import dcargs

Expand Down Expand Up @@ -346,3 +347,43 @@ def main(x: ActualParentClass[int] = ChildClass(5, 5)) -> ActualParentClass:
return x

assert dcargs.cli(main, args="--x.x 3".split(" ")) == ChildClass(3, 5, 3)


def test_pculbertson():
# https://github.com/brentyi/dcargs/issues/7
from typing import Union

@dataclasses.dataclass
class TypeA:
data: int

@dataclasses.dataclass
class TypeB:
data: int

@dataclasses.dataclass
class Wrapper:
subclass: Union[TypeA, TypeB] = TypeA(1)

wrapper1 = Wrapper() # Create Wrapper object.
wrapper2 = dcargs.extras.from_yaml(
Wrapper, dcargs.extras.to_yaml(wrapper1)
) # Errors, no constructor for TypeA


def test_annotated():
# https://github.com/brentyi/dcargs/issues/7
from typing import Union

@dataclasses.dataclass
class TypeA:
data: int

@dataclasses.dataclass
class Wrapper:
subclass: Annotated[int, TypeA] = TypeA(1)

wrapper1 = Wrapper() # Create Wrapper object.
wrapper2 = dcargs.extras.from_yaml(
Wrapper, dcargs.extras.to_yaml(wrapper1)
) # Errors, no constructor for TypeA

0 comments on commit 40e01c7

Please sign in to comment.