Skip to content

Commit 8cf3932

Browse files
committed
🎨 Refactor models to use Annotated for field definitions across various schemas
1 parent 11eefc4 commit 8cf3932

File tree

8 files changed

+42
-31
lines changed

8 files changed

+42
-31
lines changed

packages/aws-library/src/aws_library/s3/_models.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import datetime
22
from pathlib import Path
3-
from typing import TypeAlias, cast
3+
from typing import Annotated, TypeAlias, cast
44

55
from models_library.api_schemas_storage.storage_schemas import ETag
66
from models_library.basic_types import SHA256Str
@@ -54,9 +54,10 @@ def as_path(self) -> Path:
5454

5555
class S3DirectoryMetaData(BaseModel, frozen=True):
5656
prefix: S3ObjectPrefix
57-
size: ByteSize | None = Field(
58-
..., description="Size of the directory if computed, None if unknown"
59-
)
57+
size: Annotated[
58+
ByteSize | None,
59+
Field(description="Size of the directory if computed, None if unknown"),
60+
]
6061

6162
def as_path(self) -> Path:
6263
return self.prefix

packages/common-library/tests/test_pydantic_fields_extension.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
from typing import Any, Callable, Literal
1+
from collections.abc import Callable
2+
from typing import Any, Literal
23

34
import pytest
45
from common_library.pydantic_fields_extension import get_type, is_literal, is_nullable
5-
from pydantic import BaseModel, Field
6+
from pydantic import BaseModel
67

78

89
class MyModel(BaseModel):
910
a: int
10-
b: float | None = Field(...)
11+
b: float | None
1112
c: str = "bla"
1213
d: bool | None = None
1314
e: Literal["bla"]

packages/common-library/tests/test_pydantic_validators.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
validate_numeric_string_as_timedelta,
88
)
99
from faker import Faker
10-
from pydantic import BeforeValidator, Field
10+
from pydantic import BeforeValidator
1111
from pydantic_settings import BaseSettings, SettingsConfigDict
1212
from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict
1313

@@ -17,7 +17,7 @@ class Settings(BaseSettings):
1717
APP_NAME: str
1818
REQUEST_TIMEOUT: Annotated[
1919
timedelta, BeforeValidator(_validate_legacy_timedelta_str)
20-
] = Field(default=timedelta(hours=1))
20+
] = timedelta(hours=1)
2121

2222
model_config = SettingsConfigDict()
2323

@@ -45,7 +45,7 @@ def test_validate_timedelta_in_legacy_mode(
4545
):
4646
class Settings(BaseSettings):
4747
APP_NAME: str
48-
REQUEST_TIMEOUT: timedelta = Field(default=timedelta(seconds=40))
48+
REQUEST_TIMEOUT: timedelta = timedelta(seconds=40)
4949

5050
_validate_request_timeout = validate_numeric_string_as_timedelta(
5151
"REQUEST_TIMEOUT"

packages/models-library/src/models_library/api_schemas__common/errors.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import http
2-
from typing import Any
2+
from typing import Annotated, Any
33

44
from pydantic import BaseModel, Field
55

66
from ..basic_types import IDStr
77

88

99
class DefaultApiError(BaseModel):
10-
name: IDStr = Field(
11-
...,
12-
description="Error identifier as a code or a name. "
13-
"Mainly for machine-machine communication purposes.",
10+
name: Annotated[IDStr, Field(description="Exception's class name")]
11+
detail: Annotated[Any | None, Field(description="Human readable error message")] = (
12+
None
1413
)
15-
detail: Any | None = Field(default=None, description="Human readable error message")
1614

1715
@classmethod
1816
def from_status_code(

packages/models-library/src/models_library/api_schemas__common/meta.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Annotated
2+
13
from pydantic import BaseModel, ConfigDict, Field
24

35
from ..basic_types import VersionStr
@@ -6,9 +8,10 @@
68
class BaseMeta(BaseModel):
79
name: str
810
version: VersionStr
9-
released: dict[str, VersionStr] | None = Field(
10-
default=None, description="Maps every route's path tag with a released version"
11-
)
11+
released: Annotated[
12+
dict[str, VersionStr] | None,
13+
Field(description="Maps every route's path tag with a released version"),
14+
] = None
1215

1316
model_config = ConfigDict(
1417
json_schema_extra={

packages/models-library/src/models_library/api_schemas_api_server/api_keys.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Annotated
2+
13
from pydantic import BaseModel, ConfigDict, Field, SecretStr
24

35

@@ -10,7 +12,7 @@ class ApiKeyInDB(BaseModel):
1012
api_key: str
1113
api_secret: str
1214

13-
id_: int = Field(0, alias="id")
15+
id_: Annotated[int, Field(alias="id")] = 0
1416
display_name: str
1517
user_id: int
1618
product_name: str
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1+
from typing import Annotated
2+
13
from pydantic import BaseModel, Field
24

35
from ..generated_models.docker_rest_api import ServiceSpec as DockerServiceSpec
46

57

68
class ServiceSpecifications(BaseModel):
7-
sidecar: DockerServiceSpec | None = Field(
8-
default=None,
9-
description="schedule-time specifications for the service sidecar (follows Docker Service creation API, see https://docs.docker.com/engine/api/v1.25/#operation/ServiceCreate)",
10-
)
11-
service: DockerServiceSpec | None = Field(
12-
default=None,
13-
description="schedule-time specifications specifications for the service (follows Docker Service creation API (specifically only the Resources part), see https://docs.docker.com/engine/api/v1.41/#tag/Service/operation/ServiceCreate",
14-
)
9+
sidecar: Annotated[
10+
DockerServiceSpec | None,
11+
Field(
12+
description="schedule-time specifications for the service sidecar (follows Docker Service creation API, see https://docs.docker.com/engine/api/v1.25/#operation/ServiceCreate)",
13+
),
14+
] = None
15+
service: Annotated[
16+
DockerServiceSpec | None,
17+
Field(
18+
description="schedule-time specifications specifications for the service (follows Docker Service creation API (specifically only the Resources part), see https://docs.docker.com/engine/api/v1.41/#tag/Service/operation/ServiceCreate",
19+
),
20+
] = None
1521

1622

17-
class ServiceSpecificationsGet(ServiceSpecifications):
18-
...
23+
class ServiceSpecificationsGet(ServiceSpecifications): ...

packages/models-library/src/models_library/api_schemas_clusters_keeper/clusters.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22
from enum import auto
3+
from typing import Annotated
34

45
from pydantic import AnyUrl, BaseModel, Field
56

@@ -17,7 +18,7 @@ class ClusterState(StrAutoEnum):
1718

1819
class OnDemandCluster(BaseModel):
1920
endpoint: AnyUrl
20-
authentication: ClusterAuthentication = Field(discriminator="type")
21+
authentication: Annotated[ClusterAuthentication, Field(discriminator="type")]
2122
state: ClusterState
2223
user_id: UserID
2324
wallet_id: WalletID | None

0 commit comments

Comments
 (0)