Skip to content

Commit b171610

Browse files
authored
Bumped pydantic to v2 (#10)
* bumped pydantic to v2 Signed-off-by: Tiago Santana <[email protected]> * add pydantic.v1 compatibility in pydantic tools and validatefunction Signed-off-by: Tiago Santana <[email protected]> * replace deprecated parse_obj() with model_validate() Signed-off-by: Tiago Santana <[email protected]> * replace deprecated parse_raw() with model_validate_json() Signed-off-by: Tiago Santana <[email protected]> * replace deprecated update_forward_refs() with model_rebuild() Signed-off-by: Tiago Santana <[email protected]> * replace deprecated .json() with .model_dump_json() Signed-off-by: Tiago Santana <[email protected]> * replace missing deprecated parse_raw() with model_validate_json() Signed-off-by: Tiago Santana <[email protected]> * fix import ValidationError Signed-off-by: Tiago Santana <[email protected]> * remove None default value in raw_value Signed-off-by: Tiago Santana <[email protected]> * changed pydantic version Signed-off-by: Tiago Santana <[email protected]> * reverse changes in defaults url in test config Signed-off-by: Tiago Santana <[email protected]> * fix payload arg in MessagePayload Signed-off-by: Tiago Santana <[email protected]> * add missing fix payload arg in app Signed-off-by: Tiago Santana <[email protected]> * replace parse_obj_as with TypeAdapter. fix import v1 validationerror Signed-off-by: Tiago Santana <[email protected]> * replaced validatedFunction with v2 validate_call. replaced missing parse_raw_as with validate_json Signed-off-by: Tiago Santana <[email protected]> * bumped version Signed-off-by: Tiago Santana <[email protected]> * changed tabulate version Signed-off-by: Tiago Santana <[email protected]> * fix mkdocs build by set version of mkdocs and add mognet request class description Signed-off-by: Tiago Santana <[email protected]> --------- Signed-off-by: Tiago Santana <[email protected]>
1 parent 77002ce commit b171610

File tree

16 files changed

+1180
-824
lines changed

16 files changed

+1180
-824
lines changed

demo/test/test_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_full_a_to_z():
2323
assert submit_response.ok
2424

2525
# 2. Get the job to wait for...
26-
job = Job.parse_obj(submit_response.json())
26+
job = Job.model_validate(submit_response.json())
2727

2828
# 3. Wait for it, through polling...
2929
while True:
@@ -33,7 +33,7 @@ def test_full_a_to_z():
3333

3434
assert result_response.ok
3535

36-
result = UploadJobResult.parse_obj(result_response.json())
36+
result = UploadJobResult.model_validate(result_response.json())
3737

3838
if result.job_status not in READY_STATES:
3939
time.sleep(1)

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ from mognet import App, task, Context, AppConfig
3535
#
3636
# Note that we also don't specify the URLs to the Redis and RabbitMQ themselves,
3737
# opting instead for the defaults.
38-
config = AppConfig.parse_obj({
38+
config = AppConfig.model_validate({
3939
"result_backend": {
4040
# Assumes a Redis on localhost:6379 with no credentials
4141
"redis": {}

mognet/app/app.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,14 @@ async def get_current_status_of_nodes(
194194
payload=MessagePayload(
195195
id=str(request.id),
196196
kind="Query",
197-
payload=request,
197+
payload=request.model_dump(),
198198
)
199199
)
200200

201201
try:
202202
async for response in responses:
203203
try:
204-
yield StatusResponseMessage.parse_obj(response)
204+
yield StatusResponseMessage.model_validate(response)
205205
except asyncio.CancelledError:
206206
break
207207
except Exception as exc: # pylint: disable=broad-except
@@ -260,7 +260,7 @@ async def submit(self, req: "Request", context: Optional[Context] = None) -> Res
260260
payload = MessagePayload(
261261
id=str(req.id),
262262
kind="Request",
263-
payload=req,
263+
payload=req.model_dump(),
264264
priority=req.priority,
265265
)
266266

@@ -407,7 +407,7 @@ async def revoke(self, request_id: uuid.UUID, *, force: bool = False) -> Result:
407407
payload = MessagePayload(
408408
id=str(uuid.uuid4()),
409409
kind=Revoke.MESSAGE_KIND,
410-
payload=Revoke(id=request_id),
410+
payload=Revoke(id=request_id).model_dump(),
411411
)
412412

413413
await self.broker.send_control_message(payload)
@@ -682,7 +682,7 @@ async def _process_control_message(self, msg: IncomingMessagePayload):
682682

683683
try:
684684
if msg.kind == Revoke.MESSAGE_KIND:
685-
abort = Revoke.parse_obj(msg.payload)
685+
abort = Revoke.model_validate(msg.payload)
686686

687687
_log.debug("Received request to revoke request id=%r", abort.id)
688688

@@ -704,7 +704,7 @@ async def _process_control_message(self, msg: IncomingMessagePayload):
704704
return
705705

706706
if msg.kind == "Query":
707-
query = QueryRequestMessage.parse_obj(msg.payload)
707+
query = QueryRequestMessage.model_validate(msg.payload)
708708

709709
if query.name == "Status":
710710
# Get the status of this worker and reply to the incoming message
@@ -723,7 +723,7 @@ async def _process_control_message(self, msg: IncomingMessagePayload):
723723
)
724724

725725
payload = MessagePayload(
726-
id=str(reply.id), kind=reply.kind, payload=reply
726+
id=str(reply.id), kind=reply.kind, payload=reply.model_dump()
727727
)
728728

729729
return await self.broker.send_reply(msg, payload)

mognet/app/app_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class AppConfig(BaseModel):
8888
@classmethod
8989
def from_file(cls, file_path: str) -> "AppConfig":
9090
with open(file_path, "r", encoding="utf-8") as config_file:
91-
return cls.parse_raw(config_file.read())
91+
return cls.model_validate_json(config_file.read())
9292

9393
# Maximum number of attempts to connect
9494
max_reconnect_retries: int = 5

mognet/backend/redis_result_backend.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from typing import Any, AnyStr, Dict, Iterable, List, Optional, Set
1111
from uuid import UUID
1212

13-
from pydantic.tools import parse_raw_as
13+
from pydantic import TypeAdapter
1414
from redis.asyncio import Redis, from_url
1515
from redis.exceptions import ConnectionError, TimeoutError
1616

@@ -142,7 +142,7 @@ async def get_or_create(self, result_id: UUID) -> Result:
142142
return self._decode_result(value)
143143

144144
def _encode_result_value(self, value: ResultValueHolder) -> Dict[str, bytes]:
145-
contents = value.json().encode()
145+
contents = value.model_dump_json().encode()
146146
encoding = b"null"
147147

148148
if self.config.redis.result_value_encoding == Encoding.GZIP:
@@ -164,7 +164,7 @@ def _decode_result_value(self, encoded: Dict[bytes, bytes]) -> ResultValueHolder
164164
if encoded.get(b"content_type") != _json_bytes("application/json"):
165165
raise ValueError(f"Unknown content_type={encoded.get(b'content_type')!r}")
166166

167-
return ResultValueHolder.parse_raw(contents, content_type="application/json")
167+
return ResultValueHolder.model_validate_json(contents)
168168

169169
@_retry
170170
async def set(self, result_id: UUID, result: Result):
@@ -322,7 +322,7 @@ async def waiter():
322322
while True:
323323
raw_state = await shield(self._redis.hget(key, "state")) or b"null"
324324

325-
state = parse_raw_as(t, raw_state)
325+
state = TypeAdapter(t).validate_json(raw_state)
326326

327327
if state is None:
328328
raise ResultValueLost(result_id)
@@ -432,7 +432,7 @@ def _decode_result(self, json_dict: Dict[bytes, bytes]) -> Result:
432432

433433

434434
def _encode_result(result: Result) -> Dict[str, bytes]:
435-
json_dict: dict = json.loads(result.json())
435+
json_dict: dict = json.loads(result.model_dump_json())
436436
return _dict_to_json_dict(json_dict)
437437

438438

mognet/broker/amqp_broker.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ async def send_task_message(self, queue: str, payload: MessagePayload):
167167
amqp_queue = self._task_queue_name(queue)
168168

169169
msg = Message(
170-
body=payload.json().encode(),
170+
body=payload.model_dump_json().encode(),
171171
content_type="application/json",
172172
content_encoding="utf-8",
173173
priority=payload.priority,
@@ -201,7 +201,7 @@ async def consume_control_queue(
201201
@_retry
202202
async def send_control_message(self, payload: MessagePayload):
203203
msg = Message(
204-
body=payload.json().encode(),
204+
body=payload.model_dump_json().encode(),
205205
content_type="application/json",
206206
content_encoding="utf-8",
207207
message_id=payload.id,
@@ -226,7 +226,7 @@ async def _send_query_message(self, payload: MessagePayload):
226226
await callback_queue.bind(self._direct_exchange)
227227

228228
msg = Message(
229-
body=payload.json().encode(),
229+
body=payload.model_dump_json().encode(),
230230
content_type="application/json",
231231
content_encoding="utf-8",
232232
message_id=payload.id,
@@ -257,7 +257,7 @@ async def send_query_message(
257257
msg = _AmqpIncomingMessagePayload(
258258
broker=self, incoming_message=message, **contents
259259
)
260-
yield QueryResponseMessage.parse_obj(msg.payload)
260+
yield QueryResponseMessage.model_validate(msg.payload)
261261
finally:
262262
if callback_queue is not None:
263263
await callback_queue.delete()
@@ -358,7 +358,7 @@ async def send_reply(self, message: IncomingMessagePayload, reply: MessagePayloa
358358
raise ValueError("Message has no reply_to set")
359359

360360
msg = Message(
361-
body=reply.json().encode(),
361+
body=reply.model_dump_json().encode(),
362362
content_type="application/json",
363363
content_encoding="utf-8",
364364
message_id=reply.id,

mognet/broker/memory_broker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ async def send_query_message(
135135

136136
while True:
137137
response = await q.get()
138-
yield QueryResponseMessage.parse_obj(response.payload)
138+
yield QueryResponseMessage.model_validate(response.payload)
139139
finally:
140140
self._callback_queues.pop(queue_id, None)
141141

mognet/cli/nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ async def read_status():
104104
typer.echo(tabulate.tabulate(table_data, headers=table_headers))
105105

106106
elif format == "json":
107-
typer.echo(report.json(indent=json_indent, ensure_ascii=False))
107+
typer.echo(report.model_dump_json(indent=json_indent))
108108

109109
if not poll:
110110
break

mognet/cli/tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def build_tree(n: ResultTree, parent: Optional[ResultTree] = None):
158158
t.show()
159159

160160
if format == "json":
161-
print(tree.json(indent=json_indent, ensure_ascii=False))
161+
print(tree.model_dump_json(indent=json_indent))
162162

163163
if not poll:
164164
break

mognet/exceptions/task_exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pydantic import BaseModel
2-
from pydantic.error_wrappers import ValidationError
2+
from pydantic import ValidationError
33
from typing import Any, Dict, List, Tuple, Union
44

55
# Taken from pydantic.error_wrappers
@@ -34,4 +34,4 @@ def __init__(self, errors: List[InvalidErrorInfo]) -> None:
3434

3535
@classmethod
3636
def from_validation_error(cls, validation_error: ValidationError):
37-
return cls([InvalidErrorInfo.parse_obj(e) for e in validation_error.errors()])
37+
return cls([InvalidErrorInfo.model_validate(e) for e in validation_error.errors()])

0 commit comments

Comments
 (0)