migrate config to pydantic v2
This commit is contained in:
parent
8b2d0b40bc
commit
ce06bf0114
@ -6,14 +6,14 @@ import importlib
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
from typing import Any, Dict, List, Optional, Set
|
from typing import Any, Dict, List, Optional, Set, get_args
|
||||||
|
|
||||||
import pydantic
|
import pydantic
|
||||||
from reflex_cli.constants.hosting import Hosting
|
from reflex_cli.constants.hosting import Hosting
|
||||||
|
|
||||||
from reflex import constants
|
from reflex import constants
|
||||||
from reflex.base import Base
|
from reflex.base import Base
|
||||||
from reflex.utils import console
|
from reflex.utils import console, types
|
||||||
|
|
||||||
|
|
||||||
class DBConfig(Base):
|
class DBConfig(Base):
|
||||||
@ -251,7 +251,7 @@ class Config(Base):
|
|||||||
"""
|
"""
|
||||||
updated_values = {}
|
updated_values = {}
|
||||||
# Iterate over the fields.
|
# Iterate over the fields.
|
||||||
for key, field in self.__fields__.items():
|
for key, field in self.model_fields.items():
|
||||||
# The env var name is the key in uppercase.
|
# The env var name is the key in uppercase.
|
||||||
env_var = os.environ.get(key.upper())
|
env_var = os.environ.get(key.upper())
|
||||||
|
|
||||||
@ -264,14 +264,18 @@ class Config(Base):
|
|||||||
|
|
||||||
# Convert the env var to the expected type.
|
# Convert the env var to the expected type.
|
||||||
try:
|
try:
|
||||||
if issubclass(field.type_, bool):
|
if types._issubclass(field.annotation, bool):
|
||||||
# special handling for bool values
|
# special handling for bool values
|
||||||
env_var = env_var.lower() in ["true", "1", "yes"]
|
env_var = env_var.lower() in ["true", "1", "yes"]
|
||||||
|
elif types.is_generic_alias(field.annotation):
|
||||||
|
env_var = get_args(field.annotation)[0](env_var)
|
||||||
|
elif field.annotation:
|
||||||
|
env_var = field.annotation(env_var)
|
||||||
else:
|
else:
|
||||||
env_var = field.type_(env_var)
|
raise ValueError(f"Invalid type {field.annotation}")
|
||||||
except ValueError:
|
except ValueError:
|
||||||
console.error(
|
console.error(
|
||||||
f"Could not convert {key.upper()}={env_var} to type {field.type_}"
|
f"Could not convert {key.upper()}={env_var} to type {field.annotation}"
|
||||||
)
|
)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user