Merge remote-tracking branch 'upstream/main' into more-env-var-cleanup

This commit is contained in:
Benedikt Bartscher 2024-10-29 21:17:54 +01:00
commit 9e7e1084e0
No known key found for this signature in database
3 changed files with 34 additions and 7 deletions

View File

@ -645,7 +645,7 @@ class Component(BaseComponent, ABC):
# Look for component specific triggers, # Look for component specific triggers,
# e.g. variable declared as EventHandler types. # e.g. variable declared as EventHandler types.
for field in self.get_fields().values(): for field in self.get_fields().values():
if types._issubclass(field.type_, EventHandler): if types._issubclass(field.outer_type_, EventHandler):
args_spec = None args_spec = None
annotation = field.annotation annotation = field.annotation
if (metadata := getattr(annotation, "__metadata__", None)) is not None: if (metadata := getattr(annotation, "__metadata__", None)) is not None:

View File

@ -22,7 +22,7 @@ from typing import (
get_args, get_args,
) )
from typing_extensions import get_type_hints from typing_extensions import Annotated, get_type_hints
from reflex.utils.exceptions import ConfigError, EnvironmentVarValueError from reflex.utils.exceptions import ConfigError, EnvironmentVarValueError
from reflex.utils.types import GenericType, is_union, value_inside_optional from reflex.utils.types import GenericType, is_union, value_inside_optional
@ -214,8 +214,8 @@ def interpret_int_env(value: str, field_name: str) -> int:
) from ve ) from ve
def interpret_path_env(value: str, field_name: str) -> Path: def interpret_existing_path_env(value: str, field_name: str) -> ExistingPath:
"""Interpret a path environment variable value. """Interpret a path environment variable value as an existing path.
Args: Args:
value: The environment variable value. value: The environment variable value.
@ -233,6 +233,19 @@ def interpret_path_env(value: str, field_name: str) -> Path:
return path return path
def interpret_path_env(value: str, field_name: str) -> Path:
"""Interpret a path environment variable value.
Args:
value: The environment variable value.
field_name: The field name.
Returns:
The interpreted value.
"""
return Path(value)
def interpret_enum_env(value: str, field_type: GenericType, field_name: str) -> Any: def interpret_enum_env(value: str, field_type: GenericType, field_name: str) -> Any:
"""Interpret an enum environment variable value. """Interpret an enum environment variable value.
@ -286,6 +299,8 @@ def interpret_env_var_value(
return interpret_int_env(value, field_name) return interpret_int_env(value, field_name)
elif field_type is Path: elif field_type is Path:
return interpret_path_env(value, field_name) return interpret_path_env(value, field_name)
elif field_type is ExistingPath:
return interpret_existing_path_env(value, field_name)
elif inspect.isclass(field_type) and issubclass(field_type, enum.Enum): elif inspect.isclass(field_type) and issubclass(field_type, enum.Enum):
return interpret_enum_env(value, field_type, field_name) return interpret_enum_env(value, field_type, field_name)
@ -410,6 +425,13 @@ if TYPE_CHECKING:
return default return default
class PathExistsFlag:
"""Flag to indicate that a path must exist."""
ExistingPath = Annotated[Path, PathExistsFlag]
class EnvironmentVariables: class EnvironmentVariables:
"""Environment variables class to instantiate environment variables.""" """Environment variables class to instantiate environment variables."""
@ -438,7 +460,7 @@ class EnvironmentVariables:
REFLEX_WEB_WORKDIR: EnvVar[Path] = env_var(Path(constants.Dirs.WEB)) REFLEX_WEB_WORKDIR: EnvVar[Path] = env_var(Path(constants.Dirs.WEB))
# Path to the alembic config file # Path to the alembic config file
ALEMBIC_CONFIG: EnvVar[Path] = env_var(Path(constants.ALEMBIC_CONFIG)) ALEMBIC_CONFIG: EnvVar[ExistingPath] = env_var(Path(constants.ALEMBIC_CONFIG))
# Disable SSL verification for HTTPX requests. # Disable SSL verification for HTTPX requests.
SSL_NO_VERIFY: EnvVar[bool] = env_var(False) SSL_NO_VERIFY: EnvVar[bool] = env_var(False)
@ -567,7 +589,7 @@ class Config(Base):
telemetry_enabled: bool = True telemetry_enabled: bool = True
# The bun path # The bun path
bun_path: Path = constants.Bun.DEFAULT_PATH bun_path: ExistingPath = constants.Bun.DEFAULT_PATH
# List of origins that are allowed to connect to the backend API. # List of origins that are allowed to connect to the backend API.
cors_allowed_origins: List[str] = ["*"] cors_allowed_origins: List[str] = ["*"]
@ -691,7 +713,7 @@ class Config(Base):
) )
# Interpret the value. # Interpret the value.
value = interpret_env_var_value(env_var, field.type_, field.name) value = interpret_env_var_value(env_var, field.outer_type_, field.name)
# Set the value. # Set the value.
updated_values[key] = value updated_values[key] = value

View File

@ -1489,6 +1489,11 @@ if sys.version_info >= (3, 10):
""" """
return self return self
@overload
def __call__(
self: EventCallback[Q, T],
) -> EventCallback[Q, T]: ...
@overload @overload
def __call__( def __call__(
self: EventCallback[Concatenate[V, Q], T], value: V | Var[V] self: EventCallback[Concatenate[V, Q], T], value: V | Var[V]