Skip to content

Commit c9e3de6

Browse files
author
Gert van Dijk
committed
mypy: comply with strict mode and resolve all 'type: ignore' cases
Tested with mypy 1.5.1 on Python 3.11. This changes `ByteString` into `Union[bytes, bytearray, memoryview]`. See python/cpython#91896. Python 3.11 documentation on `typing.ByteString`: > Deprecated since version 3.9, will be removed in version 3.14: Prefer > typing_extensions.Buffer, or a union like > `bytes | bytearray | memoryview`. Python 3.8 documentation on `typing.ByteString` [2]: > This type represents the types `bytes`, `bytearray`, and `memoryview` > of byte sequences. > > As a shorthand for this type, `bytes` can be used to annotate > arguments of any of the types mentioned above. [1]: https://docs.python.org/3.11/library/typing.html#typing.ByteString [2]: https://docs.python.org/3.8/library/typing.html#typing.ByteString
1 parent e69eba0 commit c9e3de6

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

crc.py

+36-13
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from dataclasses import dataclass
1212
from typing import (
1313
BinaryIO,
14-
ByteString,
1514
Iterable,
1615
Iterator,
1716
List,
@@ -62,15 +61,15 @@ def __getitem__(self, index: int) -> int:
6261
def __iter__(self) -> Iterator[int]:
6362
return (self[i] for i in range(0, len(self)))
6463

65-
def __int__(self):
64+
def __int__(self) -> int:
6665
return self.value
6766

6867
@property
6968
def value(self) -> int:
7069
return self._value & Byte.BIT_MASK
7170

7271
@value.setter
73-
def value(self, value) -> None:
72+
def value(self, value: int) -> None:
7473
self._value = value & Byte.BIT_MASK
7574

7675
def reversed(self) -> "Byte":
@@ -91,7 +90,7 @@ class AbstractRegister(metaclass=abc.ABCMeta):
9190
"""
9291

9392
@abc.abstractmethod
94-
def init(self):
93+
def init(self) -> None:
9594
"""
9695
Initializes the crc register.
9796
"""
@@ -175,7 +174,7 @@ def __init__(self, configuration: Configuration):
175174
if isinstance(configuration, enum.Enum):
176175
configuration = configuration.value
177176
self._topbit = 1 << (configuration.width - 1)
178-
self._bitmask = 2**configuration.width - 1
177+
self._bitmask = int(2**configuration.width - 1)
179178
self._config = configuration
180179
self._register = configuration.init_value & self._bitmask
181180

@@ -260,7 +259,7 @@ def register(self) -> int:
260259
return self._register & self._bitmask
261260

262261
@register.setter
263-
def register(self, value) -> None:
262+
def register(self, value: int) -> None:
264263
self._register = value & self._bitmask
265264

266265

@@ -368,7 +367,15 @@ def __init__(self, configuration: Configuration, optimized: bool = False):
368367
self._crc_register = klass(configuration)
369368

370369
def checksum(
371-
self, data: Union[int, ByteString, BinaryIO, Iterable[ByteString]]
370+
self,
371+
data: Union[
372+
int,
373+
bytes,
374+
bytearray,
375+
memoryview,
376+
BinaryIO,
377+
Iterable[Union[bytes, bytearray, memoryview]],
378+
],
372379
) -> int:
373380
"""
374381
Calculates the checksum for the given data.
@@ -386,7 +393,14 @@ def checksum(
386393

387394
def verify(
388395
self,
389-
data: Union[int, ByteString, BinaryIO, Iterable[ByteString]],
396+
data: Union[
397+
int,
398+
bytes,
399+
bytearray,
400+
memoryview,
401+
BinaryIO,
402+
Iterable[Union[bytes, bytearray, memoryview]],
403+
],
390404
expected: int,
391405
) -> bool:
392406
"""
@@ -404,13 +418,22 @@ def verify(
404418

405419

406420
def _bytes_generator(
407-
data: Union[int, ByteString, BinaryIO, Iterable[ByteString]] # type: ignore
421+
data: Union[
422+
int,
423+
bytes,
424+
bytearray,
425+
memoryview,
426+
BinaryIO,
427+
Iterable[Union[bytes, bytearray, memoryview]],
428+
]
408429
) -> Iterable[bytes]:
409430
if isinstance(data, int):
410431
yield data.to_bytes(1, "big")
411-
elif isinstance(data, ByteString): # type: ignore
412-
yield bytes(data) # type: ignore
413-
elif isinstance(data, (Iterable, BinaryIO)): # type: ignore
432+
elif isinstance(data, bytes):
433+
yield data
434+
elif isinstance(data, (bytearray, memoryview)):
435+
yield bytes(data)
436+
elif isinstance(data, (Iterable, BinaryIO)):
414437
yield from (bytes(e) for e in data)
415438
else:
416439
raise TypeError(f"Unsupported parameter type: {type(data)}")
@@ -602,7 +625,7 @@ def table(args: argparse.Namespace) -> bool:
602625
return True
603626

604627

605-
def main(argv: Optional[List[str]] = None):
628+
def main(argv: Optional[List[str]] = None) -> None:
606629
parser = _argument_parser()
607630
args = parser.parse_args(argv)
608631
if "func" in args:

0 commit comments

Comments
 (0)