Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions reflex/utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ def __bool__(self) -> bool:


@lru_cache
def _get_origin_cached(tp: Any):
return get_origin_og(tp)


def get_origin(tp: Any):
"""Get the origin of a class.

Expand All @@ -129,7 +133,11 @@ def get_origin(tp: Any):
Returns:
The origin of the class.
"""
return get_origin_og(tp)
return (
origin
if (origin := getattr(tp, "__origin__", None)) is not None
else _get_origin_cached(tp)
)


@lru_cache
Expand Down Expand Up @@ -190,7 +198,6 @@ def is_none(cls: GenericType) -> bool:
return cls is type(None) or cls is None


@lru_cache
def is_union(cls: GenericType) -> bool:
"""Check if a class is a Union.

Expand All @@ -200,10 +207,12 @@ def is_union(cls: GenericType) -> bool:
Returns:
Whether the class is a Union.
"""
return get_origin(cls) in UnionTypes
origin = getattr(cls, "__origin__", None)
if origin is Union:
return True
return origin is None and isinstance(cls, types.UnionType)


@lru_cache
def is_literal(cls: GenericType) -> bool:
"""Check if a class is a Literal.

Expand All @@ -213,7 +222,7 @@ def is_literal(cls: GenericType) -> bool:
Returns:
Whether the class is a literal.
"""
return get_origin(cls) is Literal
return getattr(cls, "__origin__", None) is Literal


def has_args(cls: type) -> bool:
Expand Down
Loading