Skip to content

Commit bcc60b5

Browse files
🐛 Clean Pydantic model_dumps warnings (#7358)
Co-authored-by: sanderegg <[email protected]>
1 parent 7dd5842 commit bcc60b5

File tree

6 files changed

+62
-36
lines changed

6 files changed

+62
-36
lines changed

packages/models-library/src/models_library/docker.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ def from_key(cls, key: str) -> "DockerLabelKey":
6363
_UNDEFINED_LABEL_VALUE_INT: Final[str] = "0"
6464

6565

66-
DOCKER_TASK_EC2_INSTANCE_TYPE_PLACEMENT_CONSTRAINT_KEY: Final[
67-
DockerLabelKey
68-
] = TypeAdapter(DockerLabelKey).validate_python("ec2-instance-type")
66+
DOCKER_TASK_EC2_INSTANCE_TYPE_PLACEMENT_CONSTRAINT_KEY: Final[DockerLabelKey] = (
67+
TypeAdapter(DockerLabelKey).validate_python("ec2-instance-type")
68+
)
6969

7070

7171
def to_simcore_runtime_docker_label_key(key: str) -> DockerLabelKey:
@@ -122,18 +122,24 @@ def _backwards_compatibility(cls, values: dict[str, Any]) -> dict[str, Any]:
122122

123123
mapped_values.setdefault(
124124
f"{_SIMCORE_RUNTIME_DOCKER_LABEL_PREFIX}memory-limit",
125-
_UNDEFINED_LABEL_VALUE_INT,
125+
values.get("memory_limit", _UNDEFINED_LABEL_VALUE_INT),
126126
)
127127

128128
def _convert_nano_cpus_to_cpus(nano_cpu: str) -> str:
129129
with contextlib.suppress(ValidationError):
130-
return f"{TypeAdapter(float).validate_python(nano_cpu) / (1.0*10**9):.2f}"
130+
return f"{TypeAdapter(float).validate_python(nano_cpu) / (1.0 * 10**9):.2f}"
131131
return _UNDEFINED_LABEL_VALUE_INT
132132

133133
mapped_values.setdefault(
134134
f"{_SIMCORE_RUNTIME_DOCKER_LABEL_PREFIX}cpu-limit",
135-
_convert_nano_cpus_to_cpus(
136-
values.get("nano_cpus_limit", _UNDEFINED_LABEL_VALUE_INT)
135+
values.get(
136+
"cpu_limit",
137+
_convert_nano_cpus_to_cpus(
138+
values.get(
139+
"nano_cpus_limit",
140+
_UNDEFINED_LABEL_VALUE_INT,
141+
)
142+
),
137143
),
138144
)
139145
return mapped_values

packages/models-library/tests/test_docker.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55

66
from typing import Any
7+
from uuid import UUID
78

89
import pytest
910
from faker import Faker
@@ -13,7 +14,7 @@
1314
DockerLabelKey,
1415
StandardSimcoreDockerLabels,
1516
)
16-
from pydantic import TypeAdapter, ValidationError
17+
from pydantic import ByteSize, TypeAdapter, ValidationError
1718

1819
_faker = Faker()
1920

@@ -83,11 +84,11 @@ def test_docker_label_key(label_key: str, valid: bool):
8384
True,
8485
),
8586
(
86-
f"registry:5000/si.m--c_ore/services/1234/jupyter-smash:{'A'*128}",
87+
f"registry:5000/si.m--c_ore/services/1234/jupyter-smash:{'A' * 128}",
8788
True,
8889
),
8990
(
90-
f"registry:5000/si.m--c_ore/services/1234/jupyter-smash:{'A'*129}",
91+
f"registry:5000/si.m--c_ore/services/1234/jupyter-smash:{'A' * 129}",
9192
False,
9293
),
9394
),
@@ -122,3 +123,17 @@ def test_simcore_service_docker_label_keys(obj_data: dict[str, Any]):
122123
).validate_python(exported_dict)
123124
assert re_imported_docker_label_keys
124125
assert simcore_service_docker_label_keys == re_imported_docker_label_keys
126+
127+
128+
def test_simcore_service_docker_label_keys_construction():
129+
simcore_service_docker_label_keys = StandardSimcoreDockerLabels(
130+
user_id=8268,
131+
project_id=UUID("5ea24ce0-0e4d-4ee6-a3f1-e4799752a684"),
132+
node_id=UUID("c17c6279-23c6-412f-8826-867323a7711a"),
133+
product_name="osparc",
134+
simcore_user_agent="oePqmjQbZndJghceKRJR",
135+
swarm_stack_name="UNDEFINED_DOCKER_LABEL", # NOTE: there is currently no need for this label in the comp backend
136+
memory_limit=ByteSize(23424324),
137+
cpu_limit=1.0,
138+
)
139+
assert simcore_service_docker_label_keys.cpu_limit == 1.0

services/director-v2/src/simcore_service_director_v2/models/dynamic_services_scheduler.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,8 @@ def as_label_data(self) -> str:
541541
# compose_spec needs to be json encoded before encoding it to json
542542
# and storing it in the label
543543
return self.model_copy(
544-
update={"compose_spec": json.dumps(self.compose_spec)}, deep=True
544+
update={"compose_spec": json.dumps(self.compose_spec)},
545+
deep=True,
545546
).model_dump_json()
546547

547548
model_config = ConfigDict(extra="allow", populate_by_name=True)

services/director-v2/src/simcore_service_director_v2/modules/dynamic_sidecar/docker_compose_specs.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ def _update_paths_mappings(
128128
)
129129
env_vars["DY_SIDECAR_PATH_INPUTS"] = f"{path_mappings.inputs_path}"
130130
env_vars["DY_SIDECAR_PATH_OUTPUTS"] = f"{path_mappings.outputs_path}"
131-
env_vars[
132-
"DY_SIDECAR_STATE_PATHS"
133-
] = f"{json_dumps( { f'{p}' for p in path_mappings.state_paths } )}"
131+
env_vars["DY_SIDECAR_STATE_PATHS"] = (
132+
f"{json_dumps( { f'{p}' for p in path_mappings.state_paths } )}"
133+
)
134134

135135
service_content["environment"] = _EnvironmentSection.export_as_list(env_vars)
136136

@@ -241,15 +241,17 @@ def _update_container_labels(
241241
spec_service_key, default_limits
242242
)
243243

244-
label_keys = StandardSimcoreDockerLabels.model_construct(
245-
user_id=user_id,
246-
project_id=project_id,
247-
node_id=node_id,
248-
simcore_user_agent=simcore_user_agent,
249-
product_name=product_name,
250-
swarm_stack_name=swarm_stack_name,
251-
memory_limit=ByteSize(container_limits["memory"]),
252-
cpu_limit=container_limits["cpu"],
244+
label_keys = StandardSimcoreDockerLabels.model_validate(
245+
{
246+
"user_id": user_id,
247+
"project_id": project_id,
248+
"node_id": node_id,
249+
"simcore_user_agent": simcore_user_agent,
250+
"product_name": product_name,
251+
"swarm_stack_name": swarm_stack_name,
252+
"memory_limit": ByteSize(container_limits["memory"]),
253+
"cpu_limit": container_limits["cpu"],
254+
}
253255
)
254256
docker_labels = [
255257
f"{k}={v}" for k, v in label_keys.to_simcore_runtime_docker_labels().items()

services/director-v2/src/simcore_service_director_v2/utils/dask.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -315,17 +315,19 @@ def compute_task_labels(
315315
ValidationError
316316
"""
317317
product_name = run_metadata.get("product_name", UNDEFINED_DOCKER_LABEL)
318-
standard_simcore_labels = StandardSimcoreDockerLabels.model_construct(
319-
user_id=user_id,
320-
project_id=project_id,
321-
node_id=node_id,
322-
product_name=product_name,
323-
simcore_user_agent=run_metadata.get(
324-
"simcore_user_agent", UNDEFINED_DOCKER_LABEL
325-
),
326-
swarm_stack_name=UNDEFINED_DOCKER_LABEL, # NOTE: there is currently no need for this label in the comp backend
327-
memory_limit=node_requirements.ram,
328-
cpu_limit=node_requirements.cpu,
318+
standard_simcore_labels = StandardSimcoreDockerLabels.model_validate(
319+
{
320+
"user_id": user_id,
321+
"project_id": project_id,
322+
"node_id": node_id,
323+
"product_name": product_name,
324+
"simcore_user_agent": run_metadata.get(
325+
"simcore_user_agent", UNDEFINED_DOCKER_LABEL
326+
),
327+
"swarm_stack_name": UNDEFINED_DOCKER_LABEL, # NOTE: there is currently no need for this label in the comp backend
328+
"memory_limit": node_requirements.ram,
329+
"cpu_limit": node_requirements.cpu,
330+
}
329331
).to_simcore_runtime_docker_labels()
330332
return standard_simcore_labels | TypeAdapter(ContainerLabelsDict).validate_python(
331333
{
@@ -633,7 +635,7 @@ def check_if_cluster_is_able_to_run_pipeline(
633635

634636

635637
async def wrap_client_async_routine(
636-
client_coroutine: Coroutine[Any, Any, Any] | Any | None
638+
client_coroutine: Coroutine[Any, Any, Any] | Any | None,
637639
) -> Any:
638640
"""Dask async behavior does not go well with Pylance as it returns
639641
a union of types. this wrapper makes both mypy and pylance happy"""

services/director-v2/tests/unit/test_modules_dynamic_sidecar_docker_compose_specs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def test_regression_service_has_no_reservations():
227227
f"{to_simcore_runtime_docker_label_key('user-id')}={USER_ID}",
228228
f"{to_simcore_runtime_docker_label_key('node-id')}={NODE_ID}",
229229
f"{to_simcore_runtime_docker_label_key('swarm-stack-name')}={SWARM_STACK_NAME}",
230-
f"{to_simcore_runtime_docker_label_key('cpu-limit')}=0",
230+
f"{to_simcore_runtime_docker_label_key('cpu-limit')}=0.0",
231231
f"{to_simcore_runtime_docker_label_key('memory-limit')}=0",
232232
]
233233
)

0 commit comments

Comments
 (0)