Skip to content

Update zocalo to pydantic 2 #255

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

Merged
merged 4 commits into from
Jul 19, 2024
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
178 changes: 128 additions & 50 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pyyaml = "^6.0"
graypy = "^2.0"
marshmallow = "^3.19"
requests = "^2.31"
pydantic = "<2"
pydantic = "^2"
workflows = "^2.27"

[tool.poetry.group.dev.dependencies]
Expand Down
4 changes: 3 additions & 1 deletion requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
annotated-types==0.7.0 ; python_version >= "3.8" and python_version < "4.0"
bidict==0.23.1 ; python_version >= "3.8" and python_version < "4.0"
certifi==2024.7.4 ; python_version >= "3.8" and python_version < "4.0"
charset-normalizer==3.3.2 ; python_version >= "3.8" and python_version < "4.0"
Expand All @@ -12,7 +13,8 @@ marshmallow==3.21.3 ; python_version >= "3.8" and python_version < "4.0"
packaging==24.1 ; python_version >= "3.8" and python_version < "4.0"
pika==1.3.2 ; python_version >= "3.8" and python_version < "4.0"
pluggy==1.5.0 ; python_version >= "3.8" and python_version < "4.0"
pydantic==1.10.17 ; python_version >= "3.8" and python_version < "4.0"
pydantic-core==2.20.1 ; python_version >= "3.8" and python_version < "4.0"
pydantic==2.8.2 ; python_version >= "3.8" and python_version < "4.0"
pytest-cov==5.0.0 ; python_version >= "3.8" and python_version < "4.0"
pytest-mock==3.14.0 ; python_version >= "3.8" and python_version < "4.0"
pytest==8.2.2 ; python_version >= "3.8" and python_version < "4.0"
Expand Down
2 changes: 1 addition & 1 deletion src/zocalo/cli/configure_rabbitmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _(self, permissions: PermissionSpec):
@functools.singledispatch
def _info_to_spec(incoming, infos: list):
cls = type(incoming)
return [cls(**i.dict()) for i in infos]
return [cls(**i.model_dump()) for i in infos]


@functools.singledispatch
Expand Down
36 changes: 17 additions & 19 deletions src/zocalo/util/rabbitmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from typing import Any, Dict, List, Optional, Tuple, Union

import requests
from pydantic import BaseModel, Field
from pydantic import BaseModel, ConfigDict, Field
from workflows.transport import pika_transport

import zocalo.configuration
Expand Down Expand Up @@ -404,9 +404,7 @@ class ExchangeSpec(BaseModel):
vhost: str = Field(
..., description="Virtual host name with non-ASCII characters escaped as in C."
)

class Config:
use_enum_values = True
model_config = ConfigDict(use_enum_values=True)


class ExchangeInfo(ExchangeSpec):
Expand Down Expand Up @@ -456,11 +454,9 @@ class PolicySpec(BaseModel):
alias="apply-to",
description="Which types of object this policy should apply to.",
)

class Config:
use_enum_values = True
validate_all = True
allow_population_by_field_name = True
model_config = ConfigDict(
use_enum_values=True, validate_default=True, populate_by_name=True
)


class QueueState(str, enum.Enum):
Expand Down Expand Up @@ -617,9 +613,7 @@ class UserSpec(BaseModel):
password_hash: str = Field(..., description="Hash of the user password.")
hashing_algorithm: HashingAlgorithm
tags: List[str]

class Config:
use_enum_values = True
model_config = ConfigDict(use_enum_values=True)


def http_api_request(
Expand Down Expand Up @@ -778,7 +772,7 @@ def binding_declare(self, binding: BindingSpec):
endpoint = f"bindings/{binding.vhost}/e/{binding.source}/{binding.destination_type.value}/{binding.destination}"
response = self.post(
endpoint,
json=binding.dict(
json=binding.model_dump(
exclude_defaults=True,
exclude={"vhost", "source", "destination", "destination_type"},
),
Expand Down Expand Up @@ -854,7 +848,7 @@ def exchange_declare(self, exchange: ExchangeSpec):
endpoint = f"exchanges/{exchange.vhost}/{exchange.name}/"
response = self.put(
endpoint,
json=exchange.dict(exclude_defaults=True, exclude={"name", "vhost"}),
json=exchange.model_dump(exclude_defaults=True, exclude={"name", "vhost"}),
)
response.raise_for_status()

Expand All @@ -879,7 +873,7 @@ def set_policy(self, policy: PolicySpec):
endpoint = f"policies/{policy.vhost}/{policy.name}/"
response = self.put(
endpoint,
json=policy.dict(
json=policy.model_dump(
exclude_defaults=True, exclude={"name", "vhost"}, by_alias=True
),
)
Expand Down Expand Up @@ -908,7 +902,8 @@ def queues(
def queue_declare(self, queue: QueueSpec):
endpoint = f"queues/{queue.vhost}/{queue.name}"
response = self.put(
endpoint, json=queue.dict(exclude_defaults=True, exclude={"name", "vhost"})
endpoint,
json=queue.model_dump(exclude_defaults=True, exclude={"name", "vhost"}),
)
response.raise_for_status()

Expand Down Expand Up @@ -948,7 +943,9 @@ def permissions(

def set_permissions(self, permission: PermissionSpec):
endpoint = f"permissions/{permission.vhost}/{permission.user}/"
submission = permission.dict(exclude_defaults=True, exclude={"vhost", "user"})
submission = permission.model_dump(
exclude_defaults=True, exclude={"vhost", "user"}
)
response = self.put(endpoint, json=submission)
response.raise_for_status()

Expand All @@ -959,7 +956,7 @@ def clear_permissions(self, vhost: str, user: str):

def user_put(self, user: UserSpec):
endpoint = f"users/{user.name}/"
submission = user.dict(exclude_defaults=True, exclude={"name"})
submission = user.model_dump(exclude_defaults=True, exclude={"name"})
submission["tags"] = ",".join(submission["tags"])
response = self.put(endpoint, json=submission)
response.raise_for_status()
Expand All @@ -982,7 +979,8 @@ def vhost(self, name: str) -> VHostSpec:
def add_vhost(self, vhost: VHostSpec):
endpoint = f"vhosts/{vhost.name}/"
response = self.put(
endpoint, json=vhost.dict(exclude_defaults=True, exclude={"name", "vhost"})
endpoint,
json=vhost.model_dump(exclude_defaults=True, exclude={"name", "vhost"}),
)
response.raise_for_status()

Expand Down
6 changes: 4 additions & 2 deletions src/zocalo/util/slurm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,14 @@ def get_job_info(self, job_id: int) -> models.JobInfo:
endpoint = f"slurm/{self.version}/job/{job_id}"
response = self.get(endpoint)
job_info_resp = models.OpenapiJobInfoResp(**response.json())
jobinfo = next(iter(dict(job_info_resp.jobs).get("__root__", [])))
jobinfo = next(iter(dict(job_info_resp.jobs).get("root", [])))
return jobinfo

def submit_job(
self, job_submission: models.JobSubmitReq
) -> models.JobSubmitResponseMsg:
endpoint = f"slurm/{self.version}/job/submit"
response = self.post(endpoint, json=job_submission.dict(exclude_defaults=True))
response = self.post(
endpoint, json=job_submission.model_dump(exclude_defaults=True)
)
return models.JobSubmitResponseMsg(**response.json())
Loading
Loading