Skip to content

Commit f9ff0b8

Browse files
committed
[NRL-829] WIP Fix all issues of using deprecated Pydantic v1 usage
1 parent 518232d commit f9ff0b8

File tree

21 files changed

+118
-143
lines changed

21 files changed

+118
-143
lines changed

api/consumer/readDocumentReference/read_document_reference.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ def handler(
5757
)
5858

5959
try:
60-
# TODO-NOW - Fix deprecated parse_raw usage
61-
document_reference = DocumentReference.parse_raw(result.document)
60+
document_reference = DocumentReference.model_validate_json(result.document)
6261
except ValidationError as exc:
6362
logger.log(
6463
LogReference.CONREAD003,

api/consumer/searchDocumentReference/search_document_reference.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,10 @@ def handler(
9191
pointer_types=pointer_types,
9292
):
9393
try:
94-
# TODO-NOW - Fix deprecated parse_raw usage
95-
document_reference = DocumentReference.parse_raw(result.document)
94+
document_reference = DocumentReference.model_validate_json(result.document)
9695
bundle["total"] += 1
9796
bundle["entry"].append(
98-
# TODO-NOW - Fix deprecated dict usage
99-
{"resource": document_reference.dict(exclude_none=True)}
97+
{"resource": document_reference.model_dump(exclude_none=True)}
10098
)
10199
logger.log(
102100
LogReference.CONSEARCH004,
@@ -116,8 +114,7 @@ def handler(
116114
diagnostics="An error occurred whilst parsing the document reference search results",
117115
) from exc
118116

119-
# TODO-NOW - Fix deprecated parse_obj
120-
response = Response.from_resource(Bundle.parse_obj(bundle))
117+
response = Response.from_resource(Bundle.model_validate(bundle))
121118
logger.log(LogReference.CONSEARCH999)
122119

123120
return response

api/consumer/searchPostDocumentReference/search_post_document_reference.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ def handler(
9292
nhs_number=body.nhs_number, custodian=custodian_id, pointer_types=pointer_types
9393
):
9494
try:
95-
# TODO-NOW - Fix deprecated parse_raw usage
96-
document_reference = DocumentReference.parse_raw(result.document)
95+
document_reference = DocumentReference.model_validate_json(result.document)
9796
bundle["total"] += 1
9897
bundle["entry"].append(
9998
{"resource": document_reference.dict(exclude_none=True)}
@@ -116,8 +115,7 @@ def handler(
116115
diagnostics="An error occurred whilst parsing the document reference search results",
117116
) from exc
118117

119-
# TODO-NOW - Fix deprecated parse_obj usage
120-
response = Response.from_resource(Bundle.parse_obj(bundle))
118+
response = Response.from_resource(Bundle.model_validate(bundle))
121119
logger.log(LogReference.CONPOSTSEARCH999)
122120

123121
return response

api/producer/readDocumentReference/read_document_reference.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ def handler(
5656
)
5757

5858
try:
59-
# TODO-NOW - Fix deprecated parse_raw usage
60-
document_reference = DocumentReference.parse_raw(result.document)
59+
document_reference = DocumentReference.model_validate_json(result.document)
6160
except ValidationError as exc:
6261
logger.log(
6362
LogReference.PROREAD003,

api/producer/searchDocumentReference/search_document_reference.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ def handler(
7777
pointer_types=pointer_types,
7878
):
7979
try:
80-
# TODO-NOW - Fix deprecated parse_raw usage
81-
document_reference = DocumentReference.parse_raw(result.document)
80+
document_reference = DocumentReference.model_validate_json(result.document)
8281
bundle["total"] += 1
8382
bundle["entry"].append(
8483
{"resource": document_reference.dict(exclude_none=True)}
@@ -101,7 +100,6 @@ def handler(
101100
diagnostics="An error occurred whilst parsing the document reference search results",
102101
)
103102

104-
# TODO-NOW - Fix deprecated parse_obj usage
105-
response = Response.from_resource(Bundle.parse_obj(bundle))
103+
response = Response.from_resource(Bundle.model_validate(bundle))
106104
logger.log(LogReference.PROSEARCH999)
107105
return response

api/producer/searchPostDocumentReference/search_post_document_reference.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ def handler(
7171
pointer_types=pointer_types,
7272
):
7373
try:
74-
# TODO-NOW - Fix deprecated parse_raw usage
75-
document_reference = DocumentReference.parse_raw(result.document)
74+
document_reference = DocumentReference.model_validate_json(result.document)
7675
bundle["total"] += 1
7776
bundle["entry"].append(
7877
{"resource": document_reference.dict(exclude_none=True)}
@@ -96,5 +95,4 @@ def handler(
9695
)
9796

9897
logger.log(LogReference.PROPOSTSEARCH999)
99-
# TODO-NOW - Fix deprecated parse_obj usage
100-
return Response.from_resource(Bundle.parse_obj(bundle))
98+
return Response.from_resource(Bundle.model_validate(bundle))

api/producer/updateDocumentReference/tests/test_update_document_reference.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ def test_update_document_reference_happy_path(repository: DocumentPointerReposit
3131
existing_doc_pointer = repository.get_by_id("Y05868-99999-99999-999999")
3232
assert existing_doc_pointer is not None
3333

34-
existing_doc_ref = DocumentReference.parse_raw(existing_doc_pointer.document)
34+
existing_doc_ref = DocumentReference.model_validate_json(
35+
existing_doc_pointer.document
36+
)
3537
assert existing_doc_ref.docStatus == "final"
3638

3739
doc_ref.docStatus = "entered-in-error"
@@ -75,7 +77,9 @@ def test_update_document_reference_happy_path(repository: DocumentPointerReposit
7577
updated_doc_pointer = repository.get_by_id("Y05868-99999-99999-999999")
7678
assert updated_doc_pointer is not None
7779

78-
updated_doc_ref = DocumentReference.parse_raw(updated_doc_pointer.document)
80+
updated_doc_ref = DocumentReference.model_validate_json(
81+
updated_doc_pointer.document
82+
)
7983
assert updated_doc_ref.docStatus == "entered-in-error"
8084

8185
assert updated_doc_ref.meta.lastUpdated == "2024-03-21T12:34:56.789Z"
@@ -96,7 +100,9 @@ def test_update_document_reference_happy_path_with_ssp(
96100
existing_doc_pointer = repository.get_by_id("Y05868-99999-99999-999999")
97101
assert existing_doc_pointer is not None
98102

99-
existing_doc_ref = DocumentReference.parse_raw(existing_doc_pointer.document)
103+
existing_doc_ref = DocumentReference.model_validate_json(
104+
existing_doc_pointer.document
105+
)
100106
assert existing_doc_ref.docStatus == "final"
101107

102108
doc_ref.docStatus = "entered-in-error"
@@ -140,7 +146,9 @@ def test_update_document_reference_happy_path_with_ssp(
140146
updated_doc_pointer = repository.get_by_id("Y05868-99999-99999-999999")
141147
assert updated_doc_pointer is not None
142148

143-
updated_doc_ref = DocumentReference.parse_raw(updated_doc_pointer.document)
149+
updated_doc_ref = DocumentReference.model_validate_json(
150+
updated_doc_pointer.document
151+
)
144152
assert updated_doc_ref.docStatus == "entered-in-error"
145153

146154
assert updated_doc_ref.meta.lastUpdated == "2024-03-21T12:34:56.789Z"
@@ -681,7 +689,9 @@ def test_update_document_reference_with_meta_lastupdated_ignored(
681689
existing_doc_pointer = repository.get_by_id("Y05868-99999-99999-999999")
682690
assert existing_doc_pointer is not None
683691

684-
existing_doc_ref = DocumentReference.parse_raw(existing_doc_pointer.document)
692+
existing_doc_ref = DocumentReference.model_validate_json(
693+
existing_doc_pointer.document
694+
)
685695
assert existing_doc_ref.docStatus == "final"
686696

687697
doc_ref.docStatus = "entered-in-error"
@@ -724,7 +734,9 @@ def test_update_document_reference_with_meta_lastupdated_ignored(
724734
updated_doc_pointer = repository.get_by_id("Y05868-99999-99999-999999")
725735
assert updated_doc_pointer is not None
726736

727-
updated_doc_ref = DocumentReference.parse_raw(updated_doc_pointer.document)
737+
updated_doc_ref = DocumentReference.model_validate_json(
738+
updated_doc_pointer.document
739+
)
728740
assert updated_doc_ref.docStatus == "entered-in-error"
729741

730742
assert updated_doc_ref.meta.lastUpdated == "2024-03-21T12:34:56.789Z"
@@ -745,7 +757,9 @@ def test_update_document_reference_with_invalid_date_ignored(
745757
existing_doc_pointer = repository.get_by_id("Y05868-99999-99999-999999")
746758
assert existing_doc_pointer is not None
747759

748-
existing_doc_ref = DocumentReference.parse_raw(existing_doc_pointer.document)
760+
existing_doc_ref = DocumentReference.model_validate_json(
761+
existing_doc_pointer.document
762+
)
749763
assert existing_doc_ref.docStatus == "final"
750764

751765
doc_ref.date = "2024-05-04T11:11:10.111Z"
@@ -790,7 +804,9 @@ def test_update_document_reference_with_invalid_date_ignored(
790804
updated_doc_pointer = repository.get_by_id("Y05868-99999-99999-999999")
791805
assert updated_doc_pointer is not None
792806

793-
updated_doc_ref = DocumentReference.parse_raw(updated_doc_pointer.document)
807+
updated_doc_ref = DocumentReference.model_validate_json(
808+
updated_doc_pointer.document
809+
)
794810
assert updated_doc_ref.docStatus == "entered-in-error"
795811
assert updated_doc_ref.date == "2024-03-20T00:00:01.000Z"
796812

api/producer/updateDocumentReference/update_document_reference.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ def handler(
7979
)
8080

8181
try:
82-
# TODO-NOW - Fix deprecated parse_raw usage
83-
existing_resource = DocumentReference.parse_raw(existing_model.document)
82+
existing_resource = DocumentReference.model_validate_json(
83+
existing_model.document
84+
)
8485
except ValidationError as exc:
8586
logger.log(LogReference.PROUPDATE002, error=exc)
8687
raise OperationOutcomeError(

layer/nrlf/core/decorators.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,17 @@ def wrapper(*args, **kwargs) -> Dict[str, Any]:
4242
return wrapped_func(*args, **kwargs)
4343

4444
except OperationOutcomeError as exc:
45-
# TODO-NOW - Fix deprecated dict usage
46-
response = exc.response.dict(exclude_none=True)
45+
response = exc.response.model_dump(exclude_none=True)
4746
logger.log(LogReference.ERROR001, error=str(exc), response=response)
4847
return response
4948

5049
except ParseError as exc:
51-
# TODO-NOW - Fix deprecated dict usage
52-
response = exc.response.dict(exclude_none=True)
50+
response = exc.response.model_dump(exclude_none=True)
5351
logger.log(LogReference.ERROR002, error=str(exc), response=response)
5452
return response
5553

5654
except Exception as exc:
57-
# TODO-NOW - Fix deprecated dict usage
58-
response = Response.from_exception(exc).dict(exclude_none=True)
55+
response = Response.from_exception(exc).model_dump(exclude_none=True)
5956
logger.exception(
6057
"An unhandled exception occurred whilst processing the request",
6158
exc_info=sys.exc_info(),
@@ -213,11 +210,9 @@ def basic_handler(
213210
logger.log(
214211
LogReference.HANDLER999,
215212
status_code=response.statusCode,
216-
# TODO-NOW - Fix deprecated dict usage
217-
response=response.dict(),
213+
response=response.model_dump(),
218214
)
219-
# TODO-NOW - Fix deprecated dict usage
220-
return response.dict()
215+
return response.model_dump()
221216

222217

223218
def request_handler(
@@ -258,8 +253,7 @@ def wrapper(*args, **kwargs):
258253
verify_request_ids(event)
259254

260255
config = Config()
261-
# TODO-NOW - Fix deprecated dict usage
262-
logger.log(LogReference.HANDLER001, config=config.dict())
256+
logger.log(LogReference.HANDLER001, config=config.model_dump())
263257
metadata = load_connection_metadata(event.headers, config)
264258

265259
if metadata.pointer_types == []:
@@ -297,11 +291,9 @@ def wrapper(*args, **kwargs):
297291
logger.log(
298292
LogReference.HANDLER999,
299293
status_code=response.statusCode,
300-
# TODO-NOW - Fix deprecated dict usage
301-
response=response.dict(),
294+
response=response.model_dump(),
302295
)
303-
# TODO-NOW - Fix deprecated dict usage
304-
return response.dict()
296+
return response.model_dump()
305297

306298
decorators = [
307299
functools.wraps(func),

layer/nrlf/core/dynamodb/model.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44
from typing import Any, Dict, Optional
55

66
from nhs_number import is_valid as is_valid_nhs_number
7-
from pydantic import BaseModel, Field, PrivateAttr, root_validator, validator
7+
from pydantic import (
8+
BaseModel,
9+
Field,
10+
PrivateAttr,
11+
ValidationInfo,
12+
field_validator,
13+
model_validator,
14+
)
815

916
from nrlf.core.constants import SYSTEM_SHORT_IDS, VALID_SOURCES
1017
from nrlf.core.logger import LogReference, logger
@@ -134,8 +141,7 @@ def from_document_reference(
134141
category_id=category_id,
135142
source="NRLF",
136143
version=1,
137-
# TODO-NOW - Fix deprecated json usage
138-
document=resource.json(exclude_none=True),
144+
document=resource.model_dump_json(exclude_none=True),
139145
created_on=created_on or create_fhir_instant(),
140146
)
141147

@@ -147,8 +153,7 @@ def from_document_reference(
147153
)
148154
return core_model
149155

150-
# TODO-NOW - Fix deprecated root_validator usage
151-
@root_validator(pre=True)
156+
@model_validator(mode="before")
152157
@classmethod
153158
def extract_custodian_suffix(cls, values: Dict[str, Any]) -> Dict[str, Any]:
154159
"""
@@ -178,8 +183,7 @@ def extract_custodian_suffix(cls, values: Dict[str, Any]) -> Dict[str, Any]:
178183

179184
return values
180185

181-
# TODO-NOW - Fix deprecated root_validator usage
182-
@root_validator(pre=True)
186+
@model_validator(mode="before")
183187
@classmethod
184188
def inject_producer_id(cls, values: Dict[str, Any]) -> Dict[str, Any]:
185189
"""
@@ -209,8 +213,7 @@ def inject_producer_id(cls, values: Dict[str, Any]) -> Dict[str, Any]:
209213
)
210214
return values
211215

212-
# TODO-NOW - Fix deprecated validator usage
213-
@validator("id")
216+
@field_validator("id")
214217
@classmethod
215218
def validate_id(cls, id_: str) -> str:
216219
"""
@@ -224,10 +227,13 @@ def validate_id(cls, id_: str) -> str:
224227

225228
return id_
226229

227-
# TODO-NOW - Fix deprecated validator usage
228-
@validator("producer_id")
230+
@field_validator("producer_id")
229231
@classmethod
230-
def validate_producer_id(cls, producer_id: str, values: Dict[str, Any]) -> str:
232+
def validate_producer_id(
233+
cls, producer_id: str, validation_info: ValidationInfo
234+
) -> str:
235+
values = validation_info.data
236+
231237
id_ = values.get("id")
232238
custodian = values.get("custodian_id")
233239
custodian_suffix = values.get("custodian_suffix")
@@ -247,8 +253,7 @@ def validate_producer_id(cls, producer_id: str, values: Dict[str, Any]) -> str:
247253

248254
return producer_id
249255

250-
# TODO-NOW - Fix deprecated validator usage
251-
@validator("type")
256+
@field_validator("type")
252257
@classmethod
253258
def validate_type(cls, type: str) -> str:
254259
"""
@@ -260,8 +265,7 @@ def validate_type(cls, type: str) -> str:
260265

261266
return type
262267

263-
# TODO-NOW - Fix deprecated validator usage
264-
@validator("nhs_number")
268+
@field_validator("nhs_number")
265269
@classmethod
266270
def validate_nhs_number(cls, nhs_number: str) -> str:
267271
"""
@@ -273,8 +277,7 @@ def validate_nhs_number(cls, nhs_number: str) -> str:
273277

274278
return nhs_number
275279

276-
# TODO-NOW - Fix deprecated validator usage
277-
@validator("source")
280+
@field_validator("source")
278281
@classmethod
279282
def validate_source(cls, source: str) -> str:
280283
"""
@@ -286,8 +289,7 @@ def validate_source(cls, source: str) -> str:
286289

287290
return source
288291

289-
# TODO-NOW - Fix deprecated validator usage
290-
@validator("created_on")
292+
@field_validator("created_on")
291293
@classmethod
292294
def validate_created_on(cls, created_on: str) -> str:
293295
"""
@@ -303,8 +305,7 @@ def validate_created_on(cls, created_on: str) -> str:
303305

304306
return created_on
305307

306-
# TODO-NOW - Fix deprecated validator usage
307-
@validator("updated_on")
308+
@field_validator("updated_on")
308309
@classmethod
309310
def validate_updated_on(cls, updated_on: Optional[str]) -> Optional[str]:
310311
"""

0 commit comments

Comments
 (0)