Skip to content

Commit 3ed7556

Browse files
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. While at it, also add the return types for special methods which are still optional in mypy strict mode (Ruff's ANN024 rule [3]). [1]: https://docs.python.org/3.11/library/typing.html#typing.ByteString [2]: https://docs.python.org/3.8/library/typing.html#typing.ByteString [3]: https://docs.astral.sh/ruff/rules/missing-return-type-special-method/
1 parent 8f31640 commit 3ed7556

1 file changed

Lines changed: 40 additions & 17 deletions

File tree

‎crc.py‎

Lines changed: 40 additions & 17 deletions
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,
@@ -27,7 +26,7 @@ class Byte(numbers.Number):
2726
BIT_LENGTH: int = 8
2827
BIT_MASK: int = 0xFF
2928

30-
def __init__(self, value: int = 0x00):
29+
def __init__(self, value: int = 0x00) -> None:
3130
self._value = value & Byte.BIT_MASK
3231

3332
def __add__(self, other: "Byte") -> "Byte":
@@ -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
"""
@@ -164,7 +163,7 @@ class BasicRegister(AbstractRegister):
164163
an overwrite for the _process_byte method.
165164
"""
166165

167-
def __init__(self, configuration: Configuration):
166+
def __init__(self, configuration: Configuration) -> None:
168167
"""
169168
Create a new BasicRegister.
170169
@@ -174,7 +173,7 @@ def __init__(self, configuration: Configuration):
174173
if isinstance(configuration, enum.Enum):
175174
configuration = configuration.value
176175
self._topbit = 1 << (configuration.width - 1)
177-
self._bitmask = 2**configuration.width - 1
176+
self._bitmask = int(2**configuration.width - 1)
178177
self._config = configuration
179178
self._register = configuration.init_value & self._bitmask
180179

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

261260
@register.setter
262-
def register(self, value) -> None:
261+
def register(self, value: int) -> None:
263262
self._register = value & self._bitmask
264263

265264

@@ -296,7 +295,7 @@ class TableBasedRegister(BasicRegister):
296295
register like `Register`.
297296
"""
298297

299-
def __init__(self, configuration: Configuration):
298+
def __init__(self, configuration: Configuration) -> None:
300299
"""
301300
Creates a new table based crc register.
302301
@@ -348,7 +347,7 @@ def create_lookup_table(width: int, polynomial: int) -> List[int]:
348347

349348

350349
class Calculator:
351-
def __init__(self, configuration: Configuration, optimized: bool = False):
350+
def __init__(self, configuration: Configuration, optimized: bool = False) -> None:
352351
"""
353352
Creates a new Calculator.
354353
@@ -367,7 +366,15 @@ def __init__(self, configuration: Configuration, optimized: bool = False):
367366
self._crc_register = klass(configuration)
368367

369368
def checksum(
370-
self, data: Union[int, ByteString, BinaryIO, Iterable[ByteString]]
369+
self,
370+
data: Union[
371+
int,
372+
bytes,
373+
bytearray,
374+
memoryview,
375+
BinaryIO,
376+
Iterable[Union[bytes, bytearray, memoryview]],
377+
],
371378
) -> int:
372379
"""
373380
Calculates the checksum for the given data.
@@ -385,7 +392,14 @@ def checksum(
385392

386393
def verify(
387394
self,
388-
data: Union[int, ByteString, BinaryIO, Iterable[ByteString]],
395+
data: Union[
396+
int,
397+
bytes,
398+
bytearray,
399+
memoryview,
400+
BinaryIO,
401+
Iterable[Union[bytes, bytearray, memoryview]],
402+
],
389403
expected: int,
390404
) -> bool:
391405
"""
@@ -403,13 +417,22 @@ def verify(
403417

404418

405419
def _bytes_generator(
406-
data: Union[int, ByteString, BinaryIO, Iterable[ByteString]] # type: ignore
420+
data: Union[
421+
int,
422+
bytes,
423+
bytearray,
424+
memoryview,
425+
BinaryIO,
426+
Iterable[Union[bytes, bytearray, memoryview]],
427+
]
407428
) -> Iterable[bytes]:
408429
if isinstance(data, int):
409430
yield data.to_bytes(1, "big")
410-
elif isinstance(data, ByteString): # type: ignore
411-
yield bytes(data) # type: ignore
412-
elif isinstance(data, (Iterable, BinaryIO)): # type: ignore
431+
elif isinstance(data, bytes):
432+
yield data
433+
elif isinstance(data, (bytearray, memoryview)):
434+
yield bytes(data)
435+
elif isinstance(data, (Iterable, BinaryIO)):
413436
yield from (bytes(e) for e in data)
414437
else:
415438
raise TypeError(f"Unsupported parameter type: {type(data)}")
@@ -601,7 +624,7 @@ def table(args: argparse.Namespace) -> bool:
601624
return True
602625

603626

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

0 commit comments

Comments
 (0)