Skip to content

Commit

Permalink
[ENG-4647] Fix env_file handling (#4805)
Browse files Browse the repository at this point in the history
* [ENG-4647] Fix env_file handling

* Import dotenv.load_dotenv early to avoid ImportError while loading
  rxconfig.py
* Read ENV_FILE from the environment explicitly.

fix #4803

* Config.Config: use_enum_values = False

Save enum fields as the enum object rather than the value.
  • Loading branch information
masenf authored Feb 12, 2025
1 parent cb2e7df commit 3f68a27
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions reflex/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
get_origin,
)

from reflex_cli.constants.hosting import Hosting
from typing_extensions import Annotated, get_type_hints

from reflex import constants
from reflex.base import Base
from reflex.utils import console
from reflex.utils.exceptions import ConfigError, EnvironmentVarValueError
from reflex.utils.types import GenericType, is_union, value_inside_optional

Expand All @@ -36,11 +40,11 @@
except ModuleNotFoundError:
import pydantic

from reflex_cli.constants.hosting import Hosting

from reflex import constants
from reflex.base import Base
from reflex.utils import console
try:
from dotenv import load_dotenv # pyright: ignore [reportMissingImports]
except ImportError:
load_dotenv = None


class DBConfig(Base):
Expand Down Expand Up @@ -624,6 +628,7 @@ class Config: # pyright: ignore [reportIncompatibleVariableOverride]
"""Pydantic config for the config."""

validate_assignment = True
use_enum_values = False

# The name of the app (should match the name of the app directory).
app_name: str
Expand Down Expand Up @@ -754,6 +759,9 @@ def __init__(self, *args, **kwargs):
self._non_default_attributes.update(kwargs)
self._replace_defaults(**kwargs)

# Set the log level for this process
console.set_log_level(self.loglevel)

if (
self.state_manager_mode == constants.StateManagerMode.REDIS
and not self.redis_url
Expand Down Expand Up @@ -793,16 +801,15 @@ def update_from_env(self) -> dict[str, Any]:
Returns:
The updated config values.
"""
if self.env_file:
try:
from dotenv import load_dotenv # pyright: ignore [reportMissingImports]

# load env file if exists
load_dotenv(self.env_file, override=True)
except ImportError:
env_file = self.env_file or os.environ.get("ENV_FILE", None)
if env_file:
if load_dotenv is None:
console.error(
"""The `python-dotenv` package is required to load environment variables from a file. Run `pip install "python-dotenv>=1.0.1"`."""
)
else:
# load env file if exists
load_dotenv(env_file, override=True)

updated_values = {}
# Iterate over the fields.
Expand Down

0 comments on commit 3f68a27

Please sign in to comment.