Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Fix deprecated stuff (Pydantic v2) #6732

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions api/specs/web-server/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import Any, ClassVar, NamedTuple

import yaml
from common_library.pydantic_fields_extension import get_type
from fastapi import FastAPI
from models_library.basic_types import LogLevel
from pydantic import BaseModel, Field
Expand Down Expand Up @@ -116,8 +117,8 @@ def assert_handler_signature_against_model(

# query and path parameters
implemented_params = [
ParamSpec(field.name, field.type_, field.field_info)
for field in model_cls.__fields__.values()
ParamSpec(name, get_type(info), info)
for name, info in model_cls.model_fields.items()
]

assert {p.name for p in implemented_params}.issubset( # nosec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
def iter_service_docker_data() -> Iterator[ServiceMetaDataPublished]:
for meta_obj in catalog.iter_metadata():
# NOTE: the originals are this way not modified from outside
copied_meta_obj = meta_obj.copy(deep=True)
copied_meta_obj = meta_obj.model_copy(deep=True)
assert is_function_service(copied_meta_obj.key) # nosec
yield copied_meta_obj

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def create_computation_cb(url, **kwargs) -> CallbackResult:
}
returned_computation = ComputationTask.model_validate(
ComputationTask.model_config["json_schema_extra"]["examples"][0]
).copy(
).model_copy(
update={
"id": f"{kwargs['json']['project_id']}",
"state": state,
Expand All @@ -133,7 +133,7 @@ def get_computation_cb(url, **kwargs) -> CallbackResult:
node_states = FULL_PROJECT_NODE_STATES
returned_computation = ComputationTask.model_validate(
ComputationTask.model_config["json_schema_extra"]["examples"][0]
).copy(
).model_copy(
update={
"id": Path(url.path).name,
"state": state,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async def connect_to_db(app: web.Application, settings: PostgresSettings) -> Non
- sets an engine in app state (use `get_async_engine(app)` to retrieve)
"""
if settings.POSTGRES_CLIENT_NAME:
settings = settings.copy(
settings = settings.model_copy(
update={"POSTGRES_CLIENT_NAME": settings.POSTGRES_CLIENT_NAME + "-asyncpg"}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def parse_request_query_parameters_as(
# query parameters with the same key. However, we are not using such cases anywhere at the moment.
data = dict(request.query)

if hasattr(parameters_schema_cls, "parse_obj"):
if hasattr(parameters_schema_cls, "model_validate"):
return parameters_schema_cls.model_validate(data)
model: ModelClass = TypeAdapter(parameters_schema_cls).validate_python(data)
return model
Expand Down
2 changes: 1 addition & 1 deletion packages/service-library/src/servicelib/project_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async def lock_project(
value=True,
owner=owner,
status=status,
).json(),
).model_dump_json(),
):
msg = f"Lock for project {project_uuid!r} owner {owner!r} could not be acquired"
raise ProjectLockError(msg)
Expand Down
4 changes: 2 additions & 2 deletions packages/service-library/src/servicelib/services_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def get_status_as_dict(
) -> dict:
"""shared between different backend services to guarantee same result to frontend"""
return (
status.dict(by_alias=True)
status.model_dump(by_alias=True)
if isinstance(status, DynamicServiceGet)
else status.dict()
else status.model_dump()
)
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async def _test_task_context_decorator(
) -> web.StreamResponse:
"""this task context callback tries to get the user_id from the query if available"""
query_param = parse_request_query_parameters_as(query_model, request)
request[RQT_LONG_RUNNING_TASKS_CONTEXT_KEY] = query_param.dict()
request[RQT_LONG_RUNNING_TASKS_CONTEXT_KEY] = query_param.model_dump()
return await handler(request)

return _test_task_context_decorator
Expand Down
22 changes: 0 additions & 22 deletions packages/settings-library/src/settings_library/utils_encoders.py

This file was deleted.

2 changes: 1 addition & 1 deletion services/agent/tests/unit/test_api_rest__health.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
def test_health_ok(test_client: TestClient):
response = test_client.get("/health")
assert response.status_code == status.HTTP_200_OK
assert HealthCheckGet.parse_obj(response.json())
assert HealthCheckGet.model_validate(response.json())
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ async def _get_changed_tasks_from_backend(
return [
(
task,
task.copy(update={"state": backend_state}),
task.model_copy(update={"state": backend_state}),
)
for task, backend_state in zip(
processing_tasks, tasks_backend_status, strict=True
Expand Down
2 changes: 1 addition & 1 deletion services/director-v2/tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def fake_service_specifications(faker: Faker) -> dict[str, Any]:
},
},
}
).dict(by_alias=True, exclude_unset=True)
).model_dump(by_alias=True, exclude_unset=True)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ async def test_mark_all_services_in_wallet_for_removal(
) -> None:
for wallet_id in [WalletID(1), WalletID(2)]:
for _ in range(2):
new_scheduler_data = scheduler_data.copy(deep=True)
new_scheduler_data = scheduler_data.model_copy(deep=True)
new_scheduler_data.node_uuid = faker.uuid4(cast_to=None)
new_scheduler_data.service_name = ServiceName(
f"fake_{new_scheduler_data.node_uuid}"
Expand Down
2 changes: 1 addition & 1 deletion services/director-v2/tests/unit/test_utils_computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def test_get_pipeline_state_from_task_states(
fake_task: CompTaskAtDB,
):
tasks: list[CompTaskAtDB] = [
fake_task.copy(deep=True, update={"state": s}) for s in task_states
fake_task.model_copy(deep=True, update={"state": s}) for s in task_states
]

pipeline_state: RunningState = get_pipeline_state_from_task_states(tasks)
Expand Down
4 changes: 2 additions & 2 deletions services/director-v2/tests/unit/test_utils_dags.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,13 +422,13 @@ def pipeline_test_params(

# resolve the comp_tasks
resolved_list_comp_tasks = [
c.copy(update={"node_id": node_name_to_uuid_map[c.node_id]})
c.model_copy(update={"node_id": node_name_to_uuid_map[c.node_id]})
for c in list_comp_tasks
]

# resolved the expected output

resolved_expected_pipeline_details = expected_pipeline_details_output.copy(
resolved_expected_pipeline_details = expected_pipeline_details_output.model_copy(
update={
"adjacency_list": {
NodeID(node_name_to_uuid_map[node_a]): [
Expand Down
9 changes: 7 additions & 2 deletions services/director-v2/tests/unit/with_dbs/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,14 @@ def creator(user: dict[str, Any], **cluster_kwargs) -> Cluster:
for gid, rights in cluster_kwargs["access_rights"].items():
conn.execute(
pg_insert(cluster_to_groups)
.values(cluster_id=created_cluster.id, gid=gid, **rights.dict())
.values(
cluster_id=created_cluster.id,
gid=gid,
**rights.model_dump(),
)
.on_conflict_do_update(
index_elements=["gid", "cluster_id"], set_=rights.dict()
index_elements=["gid", "cluster_id"],
set_=rights.model_dump(),
)
)
access_rights_in_db = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ async def test_container_create_outputs_dirs(
assert mock_event_filter_enqueue.call_count == 0

json_outputs_labels = {
k: v.dict(by_alias=True) for k, v in mock_outputs_labels.items()
k: v.model_dump(by_alias=True) for k, v in mock_outputs_labels.items()
}
response = await test_client.post(
f"/{API_VTAG}/containers/ports/outputs/dirs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_invalid_http_basic_auth(
):
response = client.post(
f"/{API_VTAG}/invitations",
json=invitation_data.dict(),
json=invitation_data.model_dump(),
auth=invalid_basic_auth,
)
assert response.status_code == status.HTTP_401_UNAUTHORIZED, f"{response.json()=}"
2 changes: 1 addition & 1 deletion services/invitations/tests/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_invite_user_and_check_invitation(
}

expected = {
**invitation_data.dict(exclude={"product"}),
**invitation_data.model_dump(exclude={"product"}),
"product": environs["INVITATIONS_DEFAULT_PRODUCT"],
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async def do_setup(self) -> None:
assert isinstance(self.log, logging.Logger) # nosec
self.log.info(
"osparc-gateway-server application settings:\n%s",
self.settings.json(indent=2),
self.settings.model_dump_json(indent=2),
)

if self.settings.SC_BOOT_MODE in [BootModeEnum.DEBUG]:
Expand Down
2 changes: 1 addition & 1 deletion services/payments/scripts/example_payment_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ def create_app():
override_fastapi_openapi_method(app)

app.state.settings = Settings.create_from_envs()
logging.info(app.state.settings.json(indent=2))
logging.info(app.state.settings.model_dump_json(indent=2))

for factory in (
create_payment_router,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def from_http_status_error(
def get_detailed_message(self) -> str:
err_json = "null"
if model := getattr(self, "model", None):
err_json = model.json(indent=1)
err_json = model.model_dump_json(indent=1)

curl_cmd = "null"
if http_status_error := getattr(self, "http_status_error", None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ def evaluate(ctx: typer.Context) -> None:
assert ctx # nosec
settings = MinimalApplicationSettings.create_from_envs()
err_console.print(
f"[yellow]running with configuration:\n{settings.json()}[/yellow]"
f"[yellow]running with configuration:\n{settings.model_dump_json()}[/yellow]"
)
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async def get_or_create_api_key(
api_key=api_key,
api_secret=api_secret,
)
return ApiKeyGet.construct(
return ApiKeyGet.model_construct(
display_name=row.display_name, api_key=row.api_key, api_secret=row.api_secret
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ async def replace_service_input_outputs(

# replace if above is successful
for input_key, new_input in zip(service["inputs"], new_inputs, strict=True):
service["inputs"][input_key] = new_input.dict(**export_options)
service["inputs"][input_key] = new_input.model_dump(**export_options)

for output_key, new_output in zip(service["outputs"], new_outputs, strict=True):
service["outputs"][output_key] = new_output.dict(**export_options)
service["outputs"][output_key] = new_output.model_dump(**export_options)


def can_connect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ async def from_catalog_service_api_model(

if ureg and (unit_html := get_html_formatted_unit(port, ureg)):
# we know data is ok since it was validated above
return ServiceInputGet.construct(
return ServiceInputGet.model_construct(
key_id=input_key,
unit_long=unit_html.long,
unit_short=unit_html.short,
Expand Down Expand Up @@ -123,7 +123,7 @@ async def from_catalog_service_api_model(
unit_html: UnitHtmlFormat | None
if ureg and (unit_html := get_html_formatted_unit(port, ureg)):
# we know data is ok since it was validated above
return ServiceOutputGet.construct(
return ServiceOutputGet.model_construct(
key_id=output_key,
unit_long=unit_html.long,
unit_short=unit_html.short,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ async def update_group(request: web.Request):
req_ctx = _GroupsRequestContext.model_validate(request)
path_params = parse_request_path_parameters_as(_GroupPathParams, request)
update: GroupUpdate = await parse_request_body_as(GroupUpdate, request)
new_group_values = update.dict(exclude_unset=True)
new_group_values = update.model_dump(exclude_unset=True)

updated_group = await api.update_user_group(
request.app, req_ctx.user_id, path_params.gid, new_group_values
Expand Down Expand Up @@ -270,7 +270,7 @@ async def update_group_user(request: web.Request):
user_id=req_ctx.user_id,
gid=path_params.gid,
the_user_id_in_group=path_params.uid,
access_rights=update.access_rights.dict(),
access_rights=update.access_rights.model_dump(),
)
assert GroupUserGet.model_validate(user) is not None # nosec
return envelope_json_response(user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async def list_all_user_groups(app: web.Application, user_id: UserID) -> list[Gr
async with get_database_engine(app).acquire() as conn:
groups_db = await _db.get_all_user_groups(conn, user_id=user_id)

return [Group.construct(**group.model_dump()) for group in groups_db]
return [Group.model_construct(**group.model_dump()) for group in groups_db]


async def get_user_group(
Expand Down Expand Up @@ -199,5 +199,5 @@ async def get_group_from_gid(app: web.Application, gid: GroupID) -> Group | None
group_db = await _db.get_group_from_gid(conn, gid=gid)

if group_db:
return Group.construct(**group_db.model_dump())
return Group.model_construct(**group_db.model_dump())
return None
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ async def create_invitation_token(
return await db.create_confirmation(
user_id=user_id,
action=ConfirmationAction.INVITATION.name,
data=data_model.json(),
data=data_model.model_dump_json(),
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,9 @@ async def _get_default_pricing_and_hardware_info(
_MACHINE_TOTAL_RAM_SAFE_MARGIN_RATIO: Final[
float
] = 0.1 # NOTE: machines always have less available RAM than advertised
_SIDECARS_OPS_SAFE_RAM_MARGIN: Final[ByteSize] = TypeAdapter(ByteSize).validate_python("1GiB")
_SIDECARS_OPS_SAFE_RAM_MARGIN: Final[ByteSize] = TypeAdapter(ByteSize).validate_python(
"1GiB"
)
_CPUS_SAFE_MARGIN: Final[float] = 1.4
_MIN_NUM_CPUS: Final[float] = 0.5

Expand Down Expand Up @@ -840,7 +842,7 @@ async def start_project_node(
workbench = project.get("workbench", {})
if not workbench.get(f"{node_id}"):
raise NodeNotFoundError(project_uuid=f"{project_id}", node_uuid=f"{node_id}")
node_details = Node.construct(**workbench[f"{node_id}"])
node_details = Node.model_construct(**workbench[f"{node_id}"])

await _start_dynamic_service(
request,
Expand Down Expand Up @@ -1511,7 +1513,11 @@ async def is_service_deprecated(
app, user_id, service_key, service_version, product_name
)
if deprecation_date := service.get("deprecated"):
deprecation_date_bool: bool = datetime.datetime.now(datetime.UTC) > datetime.datetime.fromisoformat(deprecation_date).replace(tzinfo=datetime.UTC)
deprecation_date_bool: bool = datetime.datetime.now(
datetime.UTC
) > datetime.datetime.fromisoformat(deprecation_date).replace(
tzinfo=datetime.UTC
)

return deprecation_date_bool
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from models_library.socketio import SocketMessageDict
from models_library.users import UserID
from models_library.utils.fastapi_encoders import jsonable_encoder
from pydantic import ConfigDict, BaseModel, Field
from pydantic import BaseModel, ConfigDict, Field


class WebSocketMessageBase(BaseModel):
Expand All @@ -26,6 +26,7 @@ def get_event_type(cls) -> str:
@abstractmethod
def to_socket_dict(self) -> SocketMessageDict:
...

model_config = ConfigDict(frozen=True)


Expand Down Expand Up @@ -58,7 +59,7 @@ class WebSocketProjectProgress(
def from_rabbit_message(
cls, message: ProgressRabbitMessageProject
) -> "WebSocketProjectProgress":
return cls.construct(
return cls.model_construct(
user_id=message.user_id,
project_id=message.project_id,
progress_type=message.progress_type,
Expand Down
Loading
Loading