dataclass: support UnionType (A | B)
This commit is contained in:
parent
95c2ffd133
commit
07ccc26d95
1 changed files with 2 additions and 1 deletions
|
@ -3,6 +3,7 @@ from __future__ import annotations
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from munch import Munch
|
from munch import Munch
|
||||||
from typing import ClassVar, Optional, Union, Mapping, Any, get_type_hints, get_origin, get_args, Iterable
|
from typing import ClassVar, Optional, Union, Mapping, Any, get_type_hints, get_origin, get_args, Iterable
|
||||||
|
from types import UnionType
|
||||||
|
|
||||||
|
|
||||||
def munchclass(*args, init=False, **kwargs):
|
def munchclass(*args, init=False, **kwargs):
|
||||||
|
@ -14,7 +15,7 @@ def resolve_type_hint(hint: type) -> Iterable[type]:
|
||||||
args: Iterable[type] = get_args(hint)
|
args: Iterable[type] = get_args(hint)
|
||||||
if origin is Optional:
|
if origin is Optional:
|
||||||
args = set(list(args) + [type(None)])
|
args = set(list(args) + [type(None)])
|
||||||
if origin in [Union, Optional]:
|
if origin in [Union, UnionType, Optional]:
|
||||||
results: list[type] = []
|
results: list[type] = []
|
||||||
for arg in args:
|
for arg in args:
|
||||||
results += resolve_type_hint(arg)
|
results += resolve_type_hint(arg)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue