Skip to content

Commit 3679a53

Browse files
migrated pydantic v1 code to v2 code for compliance #62
1 parent bedfc40 commit 3679a53

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed

dicomtrolley/core.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
TypeVar,
1414
)
1515

16-
from pydantic import Field, ValidationError, field_validator
16+
from pydantic import ConfigDict, Field, ValidationError, field_validator
1717
from pydantic.main import BaseModel
1818
from pydicom.datadict import tag_for_keyword
1919
from pydicom.dataset import Dataset
@@ -187,8 +187,8 @@ class DICOMObject(BaseModel, DICOMDownloadable):
187187
a DICOM dataset
188188
"""
189189

190-
class Config:
191-
arbitrary_types_allowed = True # allows the use of Dataset type below
190+
# allows the use of Dataset type below
191+
model_config = ConfigDict(arbitrary_types_allowed=True)
192192

193193
uid: str
194194
data: Dataset
@@ -614,9 +614,8 @@ class Query(BaseModel):
614614
max_study_date: Optional[datetime] = None
615615
min_study_date: Optional[datetime] = None
616616
include_fields: Optional[List[str]] = Field(default_factory=list)
617-
618-
class Config:
619-
extra = "forbid" # raise ValueError when passing an unknown keyword to init
617+
# raise ValueError when passing an unknown keyword to init
618+
model_config = ConfigDict(extra="forbid")
620619

621620
@classmethod
622621
def init_from_query(
@@ -643,7 +642,7 @@ def init_from_query(
643642
this class
644643
"""
645644
# remove empty, None and 0 values
646-
params = {key: val for key, val in query.dict().items() if val}
645+
params = {key: val for key, val in query.model_dump().items() if val}
647646
try:
648647
return cls(**params)
649648
except ValidationError as e:
@@ -672,7 +671,7 @@ def include_fields_check(cls, include_fields, values): # noqa: B902, N805
672671

673672
def to_short_string(self):
674673
"""A more information-dense str repr. For human reading"""
675-
filled_fields = {key: val for key, val in self.dict().items() if val}
674+
filled_fields = {key: val for key, val in self.model_dump().items() if val}
676675
filled_fields["query_level"] = filled_fields["query_level"].value
677676
return f"{type(self).__name__}: {filled_fields}"
678677

dicomtrolley/dicom_qr.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
)
2020
from dicomtrolley.exceptions import DICOMTrolleyError
2121
from dicomtrolley.parsing import DICOMParseTree
22+
from pydantic import ConfigDict
2223

2324

2425
class QueryRetrieveLevels:
@@ -59,9 +60,8 @@ class DICOMQuery(ExtendedQuery):
5960
Modality: str = ""
6061
ProtocolName: str = ""
6162
StudyID: str = ""
62-
63-
class Config:
64-
extra = "forbid" # raise ValueError when passing an unknown keyword to init
63+
# raise ValueError when passing an unknown keyword to init
64+
model_config = ConfigDict(extra="forbid")
6565

6666
@staticmethod
6767
def get_default_include_fields(query_level):
@@ -95,7 +95,7 @@ def as_parameters(self) -> Dict[str, str]:
9595

9696
# remove non-DICOM parameters and replace with DICOM tags based on them
9797
parameters = {
98-
x: y for x, y in self.dict().items()
98+
x: y for x, y in self.model_dump().items()
9999
} # all params for query
100100
parameters["StudyDate"] = self.get_study_date(
101101
parameters.pop("min_study_date"), parameters.pop("max_study_date")

dicomtrolley/mint.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def __str__(self):
204204

205205
def as_parameters(self):
206206
"""All non-empty query parameters. For use as url parameters"""
207-
parameters = {x: y for x, y in self.dict().items() if y}
207+
parameters = {x: y for x, y in self.model_dump().items() if y}
208208

209209
if "min_study_date" in parameters:
210210
parameters["min_study_date"] = parameters[
@@ -332,6 +332,6 @@ def parse_attribs(element):
332332
return dataset
333333

334334

335-
MintInstance.update_forward_refs() # enables pydantic validation
336-
MintSeries.update_forward_refs()
337-
MintStudy.update_forward_refs()
335+
MintInstance.model_rebuild() # enables pydantic validation
336+
MintSeries.model_rebuild()
337+
MintStudy.model_rebuild()

dicomtrolley/qido_rs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def uri_search_params(self) -> Dict[str, Union[str, List[str]]]:
132132
}
133133
other_search_params = {
134134
key: val
135-
for key, val in self.dict().items()
135+
for key, val in self.model_dump().items()
136136
if key not in exclude_fields
137137
}
138138

@@ -182,7 +182,7 @@ def assert_parents_filled(a_hierarchy, value_dict):
182182
else:
183183
return assert_parents_filled(a_hierarchy, value_dict)
184184

185-
assert_parents_filled(order, self.dict())
185+
assert_parents_filled(order, self.model_dump())
186186
return self
187187

188188
@model_validator(mode="after")
@@ -198,7 +198,7 @@ def assert_key_exists(values_in, query_level_in, missing_key_in):
198198
f"a QIDO-RS relational query"
199199
)
200200

201-
values = self.dict()
201+
values = self.model_dump()
202202
if query_level == QueryLevels.STUDY:
203203
pass # Fine. you can always look for some studies
204204
elif query_level == QueryLevels.SERIES:

0 commit comments

Comments
 (0)