Skip to content

Commit

Permalink
Use lru_cache on expensive typing-related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
masenf committed Aug 20, 2024
1 parent 9fb4b67 commit ffc05fb
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
3 changes: 2 additions & 1 deletion reflex/ivars/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@
overload,
)

from typing_extensions import ParamSpec, get_origin, get_type_hints, override
from typing_extensions import ParamSpec, get_type_hints, override

from reflex import constants
from reflex.base import Base
from reflex.constants.colors import Color
from reflex.utils import console, imports, serializers, types
from reflex.utils.exceptions import VarDependencyError, VarTypeError, VarValueError
from reflex.utils.format import format_state_name
from reflex.utils.types import get_origin
from reflex.vars import (
ComputedVar,
ImmutableVarData,
Expand Down
4 changes: 1 addition & 3 deletions reflex/ivars/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
overload,
)

from typing_extensions import get_origin

from reflex.utils import types
from reflex.utils.exceptions import VarAttributeError
from reflex.utils.types import GenericType, get_attribute_access_type
from reflex.utils.types import GenericType, get_attribute_access_type, get_origin
from reflex.vars import ImmutableVarData, Var, VarData

from .base import (
Expand Down
4 changes: 1 addition & 3 deletions reflex/ivars/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@
overload,
)

from typing_extensions import get_origin

from reflex import constants
from reflex.constants.base import REFLEX_VAR_OPENING_TAG
from reflex.utils.types import GenericType
from reflex.utils.types import GenericType, get_origin
from reflex.vars import (
ImmutableVarData,
Var,
Expand Down
23 changes: 21 additions & 2 deletions reflex/utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import inspect
import sys
import types
from functools import cached_property, wraps
from functools import cached_property, lru_cache, wraps
from typing import (
Any,
Callable,
Expand All @@ -21,9 +21,11 @@
Union,
_GenericAlias, # type: ignore
get_args,
get_origin,
get_type_hints,
)
from typing import (
get_origin as get_origin_og,
)

import sqlalchemy

Expand Down Expand Up @@ -133,6 +135,20 @@ def __bool__(self) -> bool:
return False


@lru_cache()
def get_origin(tp):
"""Get the origin of a class.
Args:
tp: The class to get the origin of.
Returns:
The origin of the class.
"""
return get_origin_og(tp)


@lru_cache()
def is_generic_alias(cls: GenericType) -> bool:
"""Check whether the class is a generic alias.
Expand All @@ -157,6 +173,7 @@ 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 @@ -169,6 +186,7 @@ def is_union(cls: GenericType) -> bool:
return get_origin(cls) in UnionTypes


@lru_cache()
def is_literal(cls: GenericType) -> bool:
"""Check if a class is a Literal.
Expand Down Expand Up @@ -314,6 +332,7 @@ def get_attribute_access_type(cls: GenericType, name: str) -> GenericType | None
return None # Attribute is not accessible.


@lru_cache()
def get_base_class(cls: GenericType) -> Type:
"""Get the base class of a class.
Expand Down
3 changes: 1 addition & 2 deletions reflex/vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
_GenericAlias, # type: ignore
cast,
get_args,
get_origin,
get_type_hints,
)

Expand All @@ -51,7 +50,7 @@
ParsedImportDict,
parse_imports,
)
from reflex.utils.types import override
from reflex.utils.types import get_origin, override

if TYPE_CHECKING:
from reflex.state import BaseState
Expand Down

0 comments on commit ffc05fb

Please sign in to comment.