Skip to content

Commit

Permalink
Support 3.10 unions with |
Browse files Browse the repository at this point in the history
ensure 3.9 list[] and dict[] support
  • Loading branch information
jonapich committed Aug 9, 2024
1 parent 6fdb473 commit 5237c14
Show file tree
Hide file tree
Showing 6 changed files with 1,579 additions and 1,506 deletions.
11 changes: 10 additions & 1 deletion coveo-functools/coveo_functools/flex/deserializer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import enum
import inspect
import logging
import sys
import warnings
from collections import abc
from contextlib import contextmanager
Expand Down Expand Up @@ -34,6 +35,11 @@
from coveo_functools.flex.subclass_adapter import get_subclass_adapter
from coveo_functools.flex.types import TypeHint, is_passthrough_type, PASSTHROUGH_TYPES

if sys.version_info >= (3, 10): # pragma: no cover
from types import UnionType
else:
from typing import Union as UnionType # :shrug:


T = TypeVar("T")

Expand Down Expand Up @@ -180,7 +186,10 @@ def deserialize(
# always puts the real type first. This is only applicable to the thing-or-list-of-things feature.
target_type: TypeHint = args[0] if args else Any

if origin is Union:
if origin in (
Union, # Union[str, int]
UnionType, # str | int (py3.10)
):
if not {*args}.difference(PASSTHROUGH_TYPES):
# Unions of PASSTHROUGH_TYPES are allowed and assumed to be in the proper type already
return cast(T, value)
Expand Down
Loading

0 comments on commit 5237c14

Please sign in to comment.