Skip to content

Commit 6fb4914

Browse files
authored
cache get_type_hints for environment (#4820)
1 parent 40294a7 commit 6fb4914

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

reflex/config.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import sys
1111
import threading
1212
import urllib.parse
13+
from functools import lru_cache
1314
from importlib.util import find_spec
1415
from pathlib import Path
1516
from types import ModuleType
@@ -408,6 +409,19 @@ def set(self, value: T | None) -> None:
408409
os.environ[self.name] = str_value
409410

410411

412+
@lru_cache()
413+
def get_type_hints_environment(cls: type) -> dict[str, Any]:
414+
"""Get the type hints for the environment variables.
415+
416+
Args:
417+
cls: The class.
418+
419+
Returns:
420+
The type hints.
421+
"""
422+
return get_type_hints(cls)
423+
424+
411425
class env_var: # noqa: N801 # pyright: ignore [reportRedeclaration]
412426
"""Descriptor for environment variables."""
413427

@@ -434,7 +448,9 @@ def __set_name__(self, owner: Any, name: str):
434448
"""
435449
self.name = name
436450

437-
def __get__(self, instance: Any, owner: Any):
451+
def __get__(
452+
self, instance: EnvironmentVariables, owner: type[EnvironmentVariables]
453+
):
438454
"""Get the EnvVar instance.
439455
440456
Args:
@@ -444,7 +460,7 @@ def __get__(self, instance: Any, owner: Any):
444460
Returns:
445461
The EnvVar instance.
446462
"""
447-
type_ = get_args(get_type_hints(owner)[self.name])[0]
463+
type_ = get_args(get_type_hints_environment(owner)[self.name])[0]
448464
env_name = self.name
449465
if self.internal:
450466
env_name = f"__{env_name}"

0 commit comments

Comments
 (0)