Skip to content

Commit

Permalink
Support new types.UnionType
Browse files Browse the repository at this point in the history
  • Loading branch information
dkraczkowski committed May 28, 2024
1 parent 248948e commit 0af3249
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 138 deletions.
15 changes: 13 additions & 2 deletions chili/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@
from functools import lru_cache
from inspect import isclass as is_class
from typing import Any, Callable, ClassVar, Dict, List, NewType, Optional, Type, Union, get_type_hints

from chili.error import SerialisationError


try:
from types import UnionType
_SUPPORT_NEW_UNION = True
except ImportError:
_SUPPORT_NEW_UNION = False
UnionType = object()


AnnotatedTypeNames = {"AnnotatedMeta", "_AnnotatedAlias"}
_GenericAlias = getattr(typing, "_GenericAlias")
_PROPERTIES = "__typed_properties__"
Expand Down Expand Up @@ -74,8 +82,11 @@ def is_serialisable(type_name: Type) -> bool:


def is_optional(type_name: Type) -> bool:
is_new_union = False
if _SUPPORT_NEW_UNION:
is_new_union = type(type_name) is UnionType
return (
get_origin_type(type_name) is Union
(get_origin_type(type_name) is Union or type(type_name) is Union or is_new_union)
and bool(get_type_args(type_name))
and get_type_args(type_name)[-1] is type(None) # noqa
)
Expand Down
Loading

0 comments on commit 0af3249

Please sign in to comment.