From 333fc81bb1576d1040b24cf0468f2d6f878b2a88 Mon Sep 17 00:00:00 2001 From: Christophe Haen Date: Thu, 21 Nov 2024 15:38:23 +0100 Subject: [PATCH] Subclass AnyUrl instead of annotation for https://pydantic.dev/articles/pydantic-v2-10-release#migrate-to-subclassing-instead-of-annotated-approach-for-pydantic-url-types --- .../src/diracx/core/config/__init__.py | 8 ++++--- diracx-core/src/diracx/core/settings.py | 21 ++++++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/diracx-core/src/diracx/core/config/__init__.py b/diracx-core/src/diracx/core/config/__init__.py index 8484944b..e3ee2f90 100644 --- a/diracx-core/src/diracx/core/config/__init__.py +++ b/diracx-core/src/diracx/core/config/__init__.py @@ -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): diff --git a/diracx-core/src/diracx/core/settings.py b/diracx-core/src/diracx/core/settings.py index 6e8ec11c..499c533c 100644 --- a/diracx-core/src/diracx/core/settings.py +++ b/diracx-core/src/diracx/core/settings.py @@ -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): @@ -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):