Skip to content

Commit f6a4b94

Browse files
committed
all settings
1 parent f3fec65 commit f6a4b94

File tree

7 files changed

+78
-64
lines changed

7 files changed

+78
-64
lines changed

packages/postgres-database/src/simcore_postgres_database/utils_projects_nodes.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import datetime
22
import uuid
33
from dataclasses import dataclass
4-
from typing import Any
4+
from typing import Annotated, Any
55

66
import asyncpg.exceptions # type: ignore[import-untyped]
77
import sqlalchemy
88
import sqlalchemy.exc
99
from common_library.async_tools import maybe_await
10+
from common_library.basic_types import DEFAULT_FACTORY
1011
from common_library.errors_classes import OsparcErrorMixin
1112
from pydantic import BaseModel, ConfigDict, Field
1213
from simcore_postgres_database.utils_aiosqlalchemy import map_db_exception
@@ -47,7 +48,9 @@ class ProjectNodesDuplicateNodeError(BaseProjectNodesError):
4748

4849
class ProjectNodeCreate(BaseModel):
4950
node_id: uuid.UUID
50-
required_resources: dict[str, Any] = Field(default_factory=dict)
51+
required_resources: Annotated[dict[str, Any], Field(default_factory=dict)] = (
52+
DEFAULT_FACTORY
53+
)
5154
key: str
5255
version: str
5356
label: str

packages/settings-library/src/settings_library/docker_api_proxy.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from functools import cached_property
2+
from typing import Annotated
23

34
from pydantic import Field, SecretStr
45

@@ -7,12 +8,12 @@
78

89

910
class DockerApiProxysettings(BaseCustomSettings):
10-
DOCKER_API_PROXY_HOST: str = Field(
11-
description="hostname of the docker-api-proxy service"
12-
)
13-
DOCKER_API_PROXY_PORT: PortInt = Field(
14-
8888, description="port of the docker-api-proxy service"
15-
)
11+
DOCKER_API_PROXY_HOST: Annotated[
12+
str, Field(description="hostname of the docker-api-proxy service")
13+
]
14+
DOCKER_API_PROXY_PORT: Annotated[
15+
PortInt, Field(description="port of the docker-api-proxy service")
16+
] = 8888
1617
DOCKER_API_PROXY_SECURE: bool = False
1718

1819
DOCKER_API_PROXY_USER: str

packages/settings-library/tests/conftest.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import sys
66
from pathlib import Path
7+
from typing import Annotated
78

89
import pytest
910
import settings_library
@@ -96,13 +97,15 @@ class _ApplicationSettings(BaseCustomSettings):
9697

9798
# NOTE: by convention, an addon is disabled when APP_ADDON=None, so we make this
9899
# entry nullable as well
99-
APP_OPTIONAL_ADDON: _ModuleSettings | None = Field(
100-
json_schema_extra={"auto_default_from_env": True}
101-
)
100+
APP_OPTIONAL_ADDON: Annotated[
101+
_ModuleSettings | None,
102+
Field(json_schema_extra={"auto_default_from_env": True}),
103+
]
102104

103105
# NOTE: example of a group that cannot be disabled (not nullable)
104-
APP_REQUIRED_PLUGIN: PostgresSettings | None = Field(
105-
json_schema_extra={"auto_default_from_env": True}
106-
)
106+
APP_REQUIRED_PLUGIN: Annotated[
107+
PostgresSettings | None,
108+
Field(json_schema_extra={"auto_default_from_env": True}),
109+
]
107110

108111
return _ApplicationSettings

packages/settings-library/tests/test_base.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import json
88
from collections.abc import Callable
9-
from typing import Any
9+
from typing import Annotated, Any
1010

1111
import pydantic
1212
import pytest
@@ -74,12 +74,10 @@ class M1(BaseCustomSettings):
7474
VALUE_NULLABLE_DEFAULT_VALUE: S | None = S(S_VALUE=42)
7575
VALUE_NULLABLE_DEFAULT_NULL: S | None = None
7676

77-
VALUE_NULLABLE_DEFAULT_ENV: S | None = Field(
78-
json_schema_extra={"auto_default_from_env": True}
79-
)
80-
VALUE_DEFAULT_ENV: S = Field(
81-
json_schema_extra={"auto_default_from_env": True}
82-
)
77+
VALUE_NULLABLE_DEFAULT_ENV: Annotated[
78+
S | None, Field(auto_default_from_env=True)
79+
]
80+
VALUE_DEFAULT_ENV: Annotated[S, Field(auto_default_from_env=True)]
8381

8482
class M2(BaseCustomSettings):
8583
#
@@ -91,14 +89,12 @@ class M2(BaseCustomSettings):
9189
VALUE_NULLABLE_DEFAULT_NULL: S | None = None
9290

9391
# defaults enabled but if not exists, it disables
94-
VALUE_NULLABLE_DEFAULT_ENV: S | None = Field(
95-
json_schema_extra={"auto_default_from_env": True}
96-
)
92+
VALUE_NULLABLE_DEFAULT_ENV: Annotated[
93+
S | None, Field(auto_default_from_env=True)
94+
]
9795

9896
# cannot be disabled
99-
VALUE_DEFAULT_ENV: S = Field(
100-
json_schema_extra={"auto_default_from_env": True}
101-
)
97+
VALUE_DEFAULT_ENV: Annotated[S, Field(auto_default_from_env=True)]
10298

10399
# Changed in version 3.7: Dictionary order is guaranteed to be insertion order
104100
_classes = {"M1": M1, "M2": M2, "S": S}
@@ -108,7 +104,7 @@ class M2(BaseCustomSettings):
108104

109105

110106
def test_create_settings_class(
111-
create_settings_class: Callable[[str], type[BaseCustomSettings]]
107+
create_settings_class: Callable[[str], type[BaseCustomSettings]],
112108
):
113109
M = create_settings_class("M1")
114110

@@ -216,9 +212,12 @@ def test_auto_default_to_none_logs_a_warning(
216212

217213
class SettingsClass(BaseCustomSettings):
218214
VALUE_NULLABLE_DEFAULT_NULL: S | None = None
219-
VALUE_NULLABLE_DEFAULT_ENV: S | None = Field(
220-
json_schema_extra={"auto_default_from_env": True},
221-
)
215+
VALUE_NULLABLE_DEFAULT_ENV: Annotated[
216+
S | None,
217+
Field(
218+
auto_default_from_env=True,
219+
),
220+
] = None
222221

223222
instance = SettingsClass.create_from_envs()
224223
assert instance.VALUE_NULLABLE_DEFAULT_NULL is None
@@ -245,9 +244,12 @@ def test_auto_default_to_not_none(
245244

246245
class SettingsClass(BaseCustomSettings):
247246
VALUE_NULLABLE_DEFAULT_NULL: S | None = None
248-
VALUE_NULLABLE_DEFAULT_ENV: S | None = Field(
249-
json_schema_extra={"auto_default_from_env": True},
250-
)
247+
VALUE_NULLABLE_DEFAULT_ENV: Annotated[
248+
S | None,
249+
Field(
250+
auto_default_from_env=True,
251+
),
252+
] = None
251253

252254
instance = SettingsClass.create_from_envs()
253255
assert instance.VALUE_NULLABLE_DEFAULT_NULL is None
@@ -342,7 +344,7 @@ def test_issubclass_type_error_with_pydantic_models():
342344

343345
# here reproduces the problem with our settings that ANE and PC had
344346
class SettingsClassThatFailed(BaseCustomSettings):
345-
FOO: dict[str, str] | None = Field(default=None)
347+
FOO: dict[str, str] | None = None
346348

347349
SettingsClassThatFailed(FOO={})
348350
assert SettingsClassThatFailed(FOO=None) == SettingsClassThatFailed()
@@ -352,9 +354,7 @@ def test_upgrade_failure_to_pydantic_settings_2_6(
352354
mock_env_devel_environment: EnvVarsDict,
353355
):
354356
class ProblematicSettings(BaseCustomSettings):
355-
WEBSERVER_EMAIL: SMTPSettings | None = Field(
356-
json_schema_extra={"auto_default_from_env": True}
357-
)
357+
WEBSERVER_EMAIL: SMTPSettings | None = None
358358

359359
model_config = SettingsConfigDict(nested_model_default_partial_update=True)
360360

packages/settings-library/tests/test_base_w_postgres.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import os
77
from collections.abc import Callable
8+
from typing import Annotated
89

910
import pytest
1011
from pydantic import AliasChoices, Field, ValidationError, __version__
@@ -53,17 +54,18 @@ class _FakePostgresSettings(BaseCustomSettings):
5354
POSTGRES_USER: str
5455
POSTGRES_PASSWORD: str
5556

56-
POSTGRES_DB: str = Field(...)
57+
POSTGRES_DB: str
58+
POSTGRES_MINSIZE: Annotated[int, Field(ge=1)] = 1
59+
POSTGRES_MAXSIZE: Annotated[int, Field(ge=1)] = 50
5760

58-
POSTGRES_MINSIZE: int = Field(1, ge=1)
59-
POSTGRES_MAXSIZE: int = Field(50, ge=1)
60-
61-
POSTGRES_CLIENT_NAME: str | None = Field(
62-
None,
63-
validation_alias=AliasChoices(
64-
"HOST", "HOSTNAME", "POSTGRES_CLIENT_NAME"
61+
POSTGRES_CLIENT_NAME: Annotated[
62+
str | None,
63+
Field(
64+
validation_alias=AliasChoices(
65+
"HOST", "HOSTNAME", "POSTGRES_CLIENT_NAME"
66+
),
6567
),
66-
)
68+
] = None
6769

6870
#
6971
# Different constraints on WEBSERVER_POSTGRES subsettings
@@ -77,15 +79,17 @@ class S2(BaseCustomSettings):
7779

7880
class S3(BaseCustomSettings):
7981
# cannot be disabled!!
80-
WEBSERVER_POSTGRES_DEFAULT_ENV: _FakePostgresSettings = Field(
81-
json_schema_extra={"auto_default_from_env": True}
82-
)
82+
WEBSERVER_POSTGRES_DEFAULT_ENV: Annotated[
83+
_FakePostgresSettings,
84+
Field(json_schema_extra={"auto_default_from_env": True}),
85+
]
8386

8487
class S4(BaseCustomSettings):
8588
# defaults enabled but if cannot be resolved, it disables
86-
WEBSERVER_POSTGRES_NULLABLE_DEFAULT_ENV: _FakePostgresSettings | None = (
87-
Field(json_schema_extra={"auto_default_from_env": True})
88-
)
89+
WEBSERVER_POSTGRES_NULLABLE_DEFAULT_ENV: Annotated[
90+
_FakePostgresSettings | None,
91+
Field(json_schema_extra={"auto_default_from_env": True}),
92+
]
8993

9094
class S5(BaseCustomSettings):
9195
# defaults disabled but only explicit enabled

packages/settings-library/tests/test_utils_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import logging
77
from collections.abc import Callable
88
from io import StringIO
9-
from typing import Any
9+
from typing import Annotated, Any
1010

1111
import pytest
1212
import typer
@@ -414,7 +414,7 @@ def test_cli_settings_exclude_unset_as_json(
414414

415415
def test_print_as(capsys: pytest.CaptureFixture):
416416
class FakeSettings(BaseCustomSettings):
417-
INTEGER: int = Field(..., description="Some info")
417+
INTEGER: Annotated[int, Field(description="Some info")]
418418
SECRET: SecretStr
419419
URL: AnyHttpUrl
420420

packages/settings-library/tests/test_utils_logging.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from typing import Annotated
23

34
from pydantic import AliasChoices, Field, field_validator
45
from settings_library.base import BaseCustomSettings
@@ -17,17 +18,19 @@ class Settings(BaseCustomSettings, MixinLoggingSettings):
1718
SC_BOOT_MODE: BootModeEnum | None = None
1819

1920
# LOGGING
20-
LOG_LEVEL: str = Field(
21-
"WARNING",
22-
validation_alias=AliasChoices(
23-
"APPNAME_LOG_LEVEL",
24-
"LOG_LEVEL",
21+
LOG_LEVEL: Annotated[
22+
str,
23+
Field(
24+
validation_alias=AliasChoices(
25+
"APPNAME_LOG_LEVEL",
26+
"LOG_LEVEL",
27+
),
2528
),
26-
)
29+
] = "WARNING"
2730

28-
APPNAME_DEBUG: bool = Field(
29-
default=False, description="Starts app in debug mode"
30-
)
31+
APPNAME_DEBUG: Annotated[
32+
bool, Field(description="Starts app in debug mode")
33+
] = False
3134

3235
@field_validator("LOG_LEVEL", mode="before")
3336
@classmethod

0 commit comments

Comments
 (0)