Skip to content

Commit

Permalink
Subclass AnyUrl instead of annotation for https://pydantic.dev/articl…
Browse files Browse the repository at this point in the history
  • Loading branch information
chaen committed Nov 26, 2024
1 parent e514bb8 commit 333fc81
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
8 changes: 5 additions & 3 deletions diracx-core/src/diracx/core/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ def _apply_default_scheme(value: str) -> str:
return value


ConfigSourceUrl = Annotated[
AnyUrl, UrlConstraints(host_required=False), BeforeValidator(_apply_default_scheme)
]
class AnyUrlWithoutHost(AnyUrl):
_constraints = UrlConstraints(host_required=False)


ConfigSourceUrl = Annotated[AnyUrlWithoutHost, BeforeValidator(_apply_default_scheme)]


class ConfigSource(metaclass=ABCMeta):
Expand Down
21 changes: 14 additions & 7 deletions diracx-core/src/diracx/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,23 @@

from authlib.jose import JsonWebKey
from cryptography.fernet import Fernet
from pydantic import AnyUrl, BeforeValidator, SecretStr, TypeAdapter, UrlConstraints
from pydantic import (
AnyUrl,
BeforeValidator,
FileUrl,
SecretStr,
TypeAdapter,
UrlConstraints,
)
from pydantic_settings import BaseSettings, SettingsConfigDict

T = TypeVar("T")

SqlalchemyDsn = Annotated[
AnyUrl, UrlConstraints(allowed_schemes={"sqlite+aiosqlite", "mysql+aiomysql"})
]

class SqlalchemyDsn(AnyUrl):
_constraints = UrlConstraints(
allowed_schemes=["sqlite+aiosqlite", "mysql+aiomysql"]
)


class _TokenSigningKey(SecretStr):
Expand Down Expand Up @@ -63,9 +72,7 @@ def _apply_default_scheme(value: str) -> str:
return value


LocalFileUrl = Annotated[
AnyUrl, UrlConstraints(host_required=False), BeforeValidator(_apply_default_scheme)
]
LocalFileUrl = Annotated[FileUrl, BeforeValidator(_apply_default_scheme)]


class ServiceSettingsBase(BaseSettings):
Expand Down

0 comments on commit 333fc81

Please sign in to comment.