Skip to content

Commit c17fbf1

Browse files
Fix MyPy issues
1 parent db73371 commit c17fbf1

10 files changed

Lines changed: 40 additions & 35 deletions

File tree

pynetdicom/_handlers.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import logging
44
from struct import unpack, calcsize
5-
from typing import TYPE_CHECKING, cast, Any
5+
from typing import TYPE_CHECKING, cast, Any, TypeAlias
66
from collections.abc import Sequence, Iterator
77

88
from pydicom.dataset import Dataset
@@ -2493,13 +2493,13 @@ def _recv_n_delete_rsp(event: "Event") -> list[str]:
24932493
return s
24942494

24952495

2496-
StatusType = int | Dataset
2497-
DatasetType = Dataset | None
2498-
UserReturnType = tuple[StatusType, DatasetType]
2499-
DestinationType = tuple[str, int] | tuple[str, int, dict[str, Any]]
2500-
CFindType = Iterator[UserReturnType]
2501-
CGetType = Iterator[int | UserReturnType]
2502-
CMoveType = Iterator[DestinationType | int | UserReturnType]
2496+
StatusType: TypeAlias = int | Dataset
2497+
DatasetType: TypeAlias = Dataset | None
2498+
UserReturnType: TypeAlias = tuple[StatusType, DatasetType]
2499+
DestinationType: TypeAlias = tuple[str, int] | tuple[str, int, dict[str, Any]]
2500+
CFindType: TypeAlias = Iterator[UserReturnType]
2501+
CGetType: TypeAlias = Iterator[int | UserReturnType]
2502+
CMoveType: TypeAlias = Iterator[DestinationType | int | UserReturnType]
25032503

25042504

25052505
# Example handlers used for the documentation

pynetdicom/ae.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
import threading
1111
from typing import (
1212
cast,
13-
TypeVar,
1413
Any,
14+
TypeAlias,
15+
TypeVar,
1516
)
1617
from collections.abc import Sequence
1718
import warnings
@@ -40,8 +41,8 @@
4041

4142

4243
_T = TypeVar("_T")
43-
ListCXType = list[PresentationContext]
44-
TSyntaxType = None | str | UID | Sequence[str] | Sequence[UID]
44+
ListCXType: TypeAlias = list[PresentationContext]
45+
TSyntaxType: TypeAlias = None | str | UID | Sequence[str] | Sequence[UID]
4546

4647

4748
class ApplicationEntity:
@@ -412,7 +413,7 @@ def add_supported_context(
412413
else:
413414
context = PresentationContext()
414415
context.abstract_syntax = abstract_syntax
415-
context.transfer_syntax = transfer_syntax # type: ignore
416+
context.transfer_syntax = transfer_syntax
416417
context.scu_role = None or scu_role
417418
context.scp_role = None or scp_role
418419

pynetdicom/association.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1845,7 +1845,7 @@ def send_c_store(
18451845
if not _config.STORE_SEND_CHUNKED_DATASET:
18461846
dataset = dcmread(os.fspath(fpath))
18471847
else:
1848-
dataset = None # type: ignore[assignment]
1848+
dataset = None
18491849
allow_conversion = False
18501850
file_meta, offset = split_dataset(fpath)
18511851
req._dataset_path = (fpath, offset)

pynetdicom/dimse_messages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ def decode_msg(self, primitive: P_DATA, assoc: "Association | None" = None) -> b
474474
sop_class = cast(UID, cs.AffectedSOPClassUID)
475475
sop_instance = cast(UID, cs.AffectedSOPInstanceUID)
476476
write_file_meta_info(
477-
self._data_set_file, # type: ignore
477+
self._data_set_file,
478478
create_file_meta(
479479
sop_class_uid=sop_class,
480480
sop_instance_uid=sop_instance,

pynetdicom/dsutils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def encode(
155155
fp.close()
156156
return None
157157

158-
bytestring: bytes = fp.parent.getvalue() # type: ignore
158+
bytestring: bytes = fp.parent.getvalue()
159159
fp.close()
160160

161161
if deflated:
@@ -181,7 +181,7 @@ def encode_file_meta(file_meta: FileMetaDataset) -> bytes:
181181
buffer.is_little_endian = True
182182
buffer.is_implicit_VR = False
183183
write_file_meta_info(buffer, file_meta)
184-
return buffer.getvalue()
184+
return buffer.getvalue() # type: ignore[no-any-return] # FIXME
185185

186186

187187
def pretty_dataset(ds: Dataset, indent: int = 0, indent_char: str = " ") -> list[str]:
@@ -288,7 +288,7 @@ def split_dataset(path: Path) -> tuple[Dataset, int]:
288288

289289
def _not_group_0002(tag: BaseTag, VR: str | None, length: int) -> bool:
290290
"""Return True if the tag is not in group 0x0002, False otherwise."""
291-
return tag.group != 2
291+
return tag.group != 2 # type: ignore[no-any-return] # FIXME
292292

293293
with open(path, "rb") as fp:
294294
read_preamble(fp, False)

pynetdicom/events.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@
5757

5858

5959
EventType: TypeAlias = "NotificationEvent | InterventionEvent"
60-
EventHandlerType = tuple[EventType, Callable] | tuple[EventType, Callable, list[Any]]
61-
_BasicReturnType = Dataset | int
62-
_DatasetReturnType = tuple[_BasicReturnType, Dataset | None]
63-
_IteratorType = Iterator[tuple[_BasicReturnType, Dataset | None]]
60+
EventHandlerType: TypeAlias = tuple[EventType, Callable] | tuple[EventType, Callable, list[Any]]
61+
_BasicReturnType: TypeAlias = Dataset | int
62+
_DatasetReturnType: TypeAlias = tuple[_BasicReturnType, Dataset | None]
63+
_IteratorType: TypeAlias = Iterator[tuple[_BasicReturnType, Dataset | None]]
6464

6565

6666
# Notification events

pynetdicom/pdu_items.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2637,7 +2637,7 @@ def from_primitive(self, primitive: "SOPClassCommonExtendedNegotiation") -> None
26372637
self.sop_class_uid = primitive.sop_class_uid
26382638
self.service_class_uid = primitive.service_class_uid
26392639
self.related_general_sop_class_identification = (
2640-
primitive.related_general_sop_class_identification # type: ignore[assignment]
2640+
primitive.related_general_sop_class_identification
26412641
)
26422642

26432643
def to_primitive(self) -> "SOPClassCommonExtendedNegotiation":
@@ -2655,7 +2655,7 @@ def to_primitive(self) -> "SOPClassCommonExtendedNegotiation":
26552655
primitive.sop_class_uid = self.sop_class_uid
26562656
primitive.service_class_uid = self.service_class_uid
26572657
primitive.related_general_sop_class_identification = (
2658-
self.related_general_sop_class_identification # type: ignore[assignment]
2658+
self.related_general_sop_class_identification
26592659
)
26602660

26612661
return primitive
@@ -2732,7 +2732,7 @@ def _encoders(self) -> Any:
27322732
]
27332733

27342734
@staticmethod
2735-
def _generate_items(bytestream: bytes) -> Iterator[UID]: # type: ignore
2735+
def _generate_items(bytestream: bytes) -> Iterator[UID]:
27362736
"""Yield Related General SOP Class UIDs from `bytestream`.
27372737
27382738
Parameters
@@ -2905,7 +2905,7 @@ def __str__(self) -> str:
29052905

29062906
return "\n".join(s)
29072907

2908-
def _wrap_generate_items(self, b: bytes) -> list[UID]: # type: ignore
2908+
def _wrap_generate_items(self, b: bytes) -> list[UID]:
29092909
"""Return a list of UID items generated from `bytestream`."""
29102910
return list(self._generate_items(b))
29112911

pynetdicom/presentation.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ def add_transfer_syntax(self, syntax: None | str | bytes | UID) -> None:
250250
if syntax is None:
251251
return
252252

253-
if isinstance(syntax, str): # includes UID
253+
if isinstance(syntax, UID):
254+
pass
255+
elif isinstance(syntax, str):
254256
syntax = UID(syntax)
255257
elif isinstance(syntax, bytes):
256258
syntax = UID(syntax.decode("ascii"))
@@ -264,15 +266,16 @@ def add_transfer_syntax(self, syntax: None | str | bytes | UID) -> None:
264266

265267
# If the transfer syntax is rejected we may receive an empty str
266268
if syntax not in self._transfer_syntax and syntax != "":
267-
if not syntax.is_valid:
269+
if not syntax.is_valid: # type: ignore[union-attr] # FIXME
268270
LOGGER.warning(
269-
f"A non-conformant UID has been added to 'transfer_syntax': '{syntax}'"
271+
"A non-conformant UID has been added to 'transfer_syntax': "
272+
f"'{syntax}'" # type: ignore[str-bytes-safe] # FIXME
270273
)
271274

272-
if not syntax.is_private and not syntax.is_transfer_syntax:
275+
if not syntax.is_private and not syntax.is_transfer_syntax: # type: ignore[union-attr] # FIXME
273276
LOGGER.warning(
274277
"A UID has been added to 'transfer_syntax' that is not a known "
275-
f"public transfer syntax: '{syntax}'"
278+
f"public transfer syntax: '{syntax}'" # type: ignore[str-bytes-safe] # FIXME
276279
)
277280

278281
self._transfer_syntax.append(syntax)
@@ -365,7 +368,7 @@ def __ne__(self, other: object) -> bool:
365368

366369
def __repr__(self) -> str:
367370
"""Representation of the Presentation Context."""
368-
return cast(UID, self.abstract_syntax).name
371+
return cast(UID, self.abstract_syntax).name # type: ignore[no-any-return] # FIXME
369372

370373
@property
371374
def scp_role(self) -> None | bool:

pynetdicom/service_class.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
TYPE_CHECKING,
1111
cast,
1212
Any,
13+
TypeAlias,
1314
TypeVar,
1415
)
1516
from collections.abc import Iterator, Sequence
@@ -64,9 +65,9 @@
6465
_QR = C_FIND | C_MOVE | C_GET
6566

6667

67-
StatusType = int | Dataset
68-
DatasetType = Dataset | None
69-
UserReturnType = tuple[StatusType, DatasetType]
68+
StatusType: TypeAlias = int | Dataset
69+
DatasetType: TypeAlias = Dataset | None
70+
UserReturnType: TypeAlias = tuple[StatusType, DatasetType]
7071
_T = TypeVar("_T", bound=DIMSEPrimitive)
7172
_ExcInfoType = (
7273
tuple[None, None, None] | tuple[type[BaseException], BaseException, TracebackType]

pynetdicom/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,6 @@ def validate_uid(uid: UID) -> bool:
335335
``True`` if the value is considered valid, ``False`` otherwise.
336336
"""
337337
if _config.ENFORCE_UID_CONFORMANCE:
338-
return uid.is_valid
338+
return uid.is_valid # type: ignore[no-any-return] # FIXME
339339

340340
return 0 < len(uid) < 65

0 commit comments

Comments
 (0)