From 8caf617ec65aa4a4968d779291c0846e077eb51b Mon Sep 17 00:00:00 2001 From: "Thomas J. Fan" Date: Thu, 4 Apr 2024 16:19:17 -0400 Subject: [PATCH] Remove typing-inspect dependency (#2327) * Remove typing-inspect dependencies Signed-off-by: Thomas J. Fan * DOC Adds docstring Signed-off-by: Thomas J. Fan --------- Signed-off-by: Thomas J. Fan Signed-off-by: Jan Fiedler --- flytekit/core/type_engine.py | 17 +++++++++++++++-- pyproject.toml | 1 - 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/flytekit/core/type_engine.py b/flytekit/core/type_engine.py index cf0cccf0f1..7f0ddd445a 100644 --- a/flytekit/core/type_engine.py +++ b/flytekit/core/type_engine.py @@ -28,7 +28,6 @@ from mashumaro.codecs.json import JSONDecoder, JSONEncoder from mashumaro.mixins.json import DataClassJSONMixin from typing_extensions import Annotated, get_args, get_origin -from typing_inspect import is_union_type from flytekit.core.annotation import FlyteAnnotation from flytekit.core.context_manager import FlyteContext @@ -1516,6 +1515,19 @@ def _are_types_castable(upstream: LiteralType, downstream: LiteralType) -> bool: return False +def _is_union_type(t): + """Returns True if t is a Union type.""" + + if sys.version_info >= (3, 10): + import types + + UnionType = types.UnionType + else: + UnionType = None + + return t is typing.Union or get_origin(t) is Union or UnionType and isinstance(t, UnionType) + + class UnionTransformer(TypeTransformer[T]): """ Transformer that handles a typing.Union[T1, T2, ...] @@ -1526,7 +1538,8 @@ def __init__(self): @staticmethod def is_optional_type(t: Type[T]) -> bool: - return is_union_type(t) and type(None) in get_args(t) + """Return True if `t` is a Union or Optional type.""" + return _is_union_type(t) or type(None) in get_args(t) @staticmethod def get_sub_type_in_optional(t: Type[T]) -> Type[T]: diff --git a/pyproject.toml b/pyproject.toml index f6aa192f7e..0373db6f67 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -48,7 +48,6 @@ dependencies = [ "s3fs>=2023.3.0,!=2024.3.1", "statsd>=3.0.0,<4.0.0", "typing_extensions", - "typing-inspect", "urllib3>=1.22,<2.0.0", ] classifiers = [