Skip to content

Commit f74d1de

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 f714555 commit f74d1de

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

crc.py

+40-17
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

@@ -294,7 +293,7 @@ class TableBasedRegister(BasicRegister):
294293
register like `Register`.
295294
"""
296295

297-
def __init__(self, configuration: Configuration):
296+
def __init__(self, configuration: Configuration) -> None:
298297
"""
299298
Creates a new table based crc register.
300299
@@ -345,7 +344,7 @@ def create_lookup_table(width: int, polynomial: int) -> List[int]:
345344

346345

347346
class Calculator:
348-
def __init__(self, configuration: Configuration, optimized: bool = False):
347+
def __init__(self, configuration: Configuration, optimized: bool = False) -> None:
349348
"""
350349
Creates a new Calculator.
351350
@@ -364,7 +363,15 @@ def __init__(self, configuration: Configuration, optimized: bool = False):
364363
self._crc_register = klass(configuration)
365364

366365
def checksum(
367-
self, data: Union[int, ByteString, BinaryIO, Iterable[ByteString]]
366+
self,
367+
data: Union[
368+
int,
369+
bytes,
370+
bytearray,
371+
memoryview,
372+
BinaryIO,
373+
Iterable[Union[bytes, bytearray, memoryview]],
374+
],
368375
) -> int:
369376
"""
370377
Calculates the checksum for the given data.
@@ -382,7 +389,14 @@ def checksum(
382389

383390
def verify(
384391
self,
385-
data: Union[int, ByteString, BinaryIO, Iterable[ByteString]],
392+
data: Union[
393+
int,
394+
bytes,
395+
bytearray,
396+
memoryview,
397+
BinaryIO,
398+
Iterable[Union[bytes, bytearray, memoryview]],
399+
],
386400
expected: int,
387401
) -> bool:
388402
"""
@@ -400,13 +414,22 @@ def verify(
400414

401415

402416
def _bytes_generator(
403-
data: Union[int, ByteString, BinaryIO, Iterable[ByteString]] # type: ignore
417+
data: Union[
418+
int,
419+
bytes,
420+
bytearray,
421+
memoryview,
422+
BinaryIO,
423+
Iterable[Union[bytes, bytearray, memoryview]],
424+
]
404425
) -> Iterable[bytes]:
405426
if isinstance(data, int):
406427
yield data.to_bytes(1, "big")
407-
elif isinstance(data, ByteString): # type: ignore
408-
yield bytes(data) # type: ignore
409-
elif isinstance(data, (Iterable, BinaryIO)): # type: ignore
428+
elif isinstance(data, bytes):
429+
yield data
430+
elif isinstance(data, (bytearray, memoryview)):
431+
yield bytes(data)
432+
elif isinstance(data, (Iterable, BinaryIO)):
410433
yield from (bytes(e) for e in data)
411434
else:
412435
raise TypeError(f"Unsupported parameter type: {type(data)}")
@@ -598,7 +621,7 @@ def table(args: argparse.Namespace) -> bool:
598621
return True
599622

600623

601-
def main(argv: Optional[List[str]] = None):
624+
def main(argv: Optional[List[str]] = None) -> None:
602625
parser = _argument_parser()
603626
args = parser.parse_args(argv)
604627
if "func" in args:

0 commit comments

Comments
 (0)