migrate config to pydantic v2

This commit is contained in:
Benedikt Bartscher 2024-02-28 23:57:47 +01:00
parent 8b2d0b40bc
commit ce06bf0114
No known key found for this signature in database

View File

@ -6,14 +6,14 @@ import importlib
import os
import sys
import urllib.parse
from typing import Any, Dict, List, Optional, Set
from typing import Any, Dict, List, Optional, Set, get_args
import pydantic
from reflex_cli.constants.hosting import Hosting
from reflex import constants
from reflex.base import Base
from reflex.utils import console
from reflex.utils import console, types
class DBConfig(Base):
@ -251,7 +251,7 @@ class Config(Base):
"""
updated_values = {}
# 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.
env_var = os.environ.get(key.upper())
@ -264,14 +264,18 @@ class Config(Base):
# Convert the env var to the expected type.
try:
if issubclass(field.type_, bool):
if types._issubclass(field.annotation, bool):
# special handling for bool values
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:
env_var = field.type_(env_var)
raise ValueError(f"Invalid type {field.annotation}")
except ValueError:
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