Skip to content

Commit 6deca6e

Browse files
authored
Update black, ruff & mypy (#1929)
* update black and ruff * update mypy * fix pylint complaints --------- Co-authored-by: zariiii9003 <[email protected]>
1 parent 1a200c9 commit 6deca6e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+212
-218
lines changed

can/__init__.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,20 @@
1111
from typing import Any, Dict
1212

1313
__all__ = [
14+
"VALID_INTERFACES",
1415
"ASCReader",
1516
"ASCWriter",
1617
"AsyncBufferedReader",
17-
"BitTiming",
18-
"BitTimingFd",
1918
"BLFReader",
2019
"BLFWriter",
20+
"BitTiming",
21+
"BitTimingFd",
2122
"BufferedReader",
2223
"Bus",
2324
"BusABC",
2425
"BusState",
26+
"CSVReader",
27+
"CSVWriter",
2528
"CanError",
2629
"CanInitializationError",
2730
"CanInterfaceNotImplementedError",
@@ -30,30 +33,27 @@
3033
"CanTimeoutError",
3134
"CanutilsLogReader",
3235
"CanutilsLogWriter",
33-
"CSVReader",
34-
"CSVWriter",
3536
"CyclicSendTaskABC",
3637
"LimitedDurationCyclicSendTaskABC",
3738
"Listener",
38-
"Logger",
3939
"LogReader",
40-
"ModifiableCyclicTaskABC",
41-
"Message",
42-
"MessageSync",
40+
"Logger",
4341
"MF4Reader",
4442
"MF4Writer",
43+
"Message",
44+
"MessageSync",
45+
"ModifiableCyclicTaskABC",
4546
"Notifier",
4647
"Printer",
4748
"RedirectReader",
4849
"RestartableCyclicTaskABC",
4950
"SizedRotatingLogger",
5051
"SqliteReader",
5152
"SqliteWriter",
52-
"ThreadSafeBus",
5353
"TRCFileVersion",
5454
"TRCReader",
5555
"TRCWriter",
56-
"VALID_INTERFACES",
56+
"ThreadSafeBus",
5757
"bit_timing",
5858
"broadcastmanager",
5959
"bus",
@@ -64,8 +64,8 @@
6464
"interfaces",
6565
"io",
6666
"listener",
67-
"logconvert",
6867
"log",
68+
"logconvert",
6969
"logger",
7070
"message",
7171
"notifier",

can/bit_timing.py

+19-21
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def from_bitrate_and_segments(
155155
if the arguments are invalid.
156156
"""
157157
try:
158-
brp = int(round(f_clock / (bitrate * (1 + tseg1 + tseg2))))
158+
brp = round(f_clock / (bitrate * (1 + tseg1 + tseg2)))
159159
except ZeroDivisionError:
160160
raise ValueError("Invalid inputs") from None
161161

@@ -232,15 +232,15 @@ def iterate_from_sample_point(
232232
raise ValueError(f"sample_point (={sample_point}) must not be below 50%.")
233233

234234
for brp in range(1, 65):
235-
nbt = round(int(f_clock / (bitrate * brp)))
235+
nbt = int(f_clock / (bitrate * brp))
236236
if nbt < 8:
237237
break
238238

239239
effective_bitrate = f_clock / (nbt * brp)
240240
if abs(effective_bitrate - bitrate) > bitrate / 256:
241241
continue
242242

243-
tseg1 = int(round(sample_point / 100 * nbt)) - 1
243+
tseg1 = round(sample_point / 100 * nbt) - 1
244244
# limit tseg1, so tseg2 is at least 1 TQ
245245
tseg1 = min(tseg1, nbt - 2)
246246

@@ -312,7 +312,7 @@ def f_clock(self) -> int:
312312
@property
313313
def bitrate(self) -> int:
314314
"""Bitrate in bits/s."""
315-
return int(round(self.f_clock / (self.nbt * self.brp)))
315+
return round(self.f_clock / (self.nbt * self.brp))
316316

317317
@property
318318
def brp(self) -> int:
@@ -322,7 +322,7 @@ def brp(self) -> int:
322322
@property
323323
def tq(self) -> int:
324324
"""Time quantum in nanoseconds"""
325-
return int(round(self.brp / self.f_clock * 1e9))
325+
return round(self.brp / self.f_clock * 1e9)
326326

327327
@property
328328
def nbt(self) -> int:
@@ -433,7 +433,7 @@ def recreate_with_f_clock(self, f_clock: int) -> "BitTiming":
433433
"f_clock change failed because of sample point discrepancy."
434434
)
435435
# adapt synchronization jump width, so it has the same size relative to bit time as self
436-
sjw = int(round(self.sjw / self.nbt * bt.nbt))
436+
sjw = round(self.sjw / self.nbt * bt.nbt)
437437
sjw = max(1, min(4, bt.tseg2, sjw))
438438
bt._data["sjw"] = sjw # pylint: disable=protected-access
439439
bt._data["nof_samples"] = self.nof_samples # pylint: disable=protected-access
@@ -458,7 +458,7 @@ def __repr__(self) -> str:
458458
return f"can.{self.__class__.__name__}({args})"
459459

460460
def __getitem__(self, key: str) -> int:
461-
return cast(int, self._data.__getitem__(key))
461+
return cast("int", self._data.__getitem__(key))
462462

463463
def __len__(self) -> int:
464464
return self._data.__len__()
@@ -716,10 +716,8 @@ def from_bitrate_and_segments( # pylint: disable=too-many-arguments
716716
if the arguments are invalid.
717717
"""
718718
try:
719-
nom_brp = int(round(f_clock / (nom_bitrate * (1 + nom_tseg1 + nom_tseg2))))
720-
data_brp = int(
721-
round(f_clock / (data_bitrate * (1 + data_tseg1 + data_tseg2)))
722-
)
719+
nom_brp = round(f_clock / (nom_bitrate * (1 + nom_tseg1 + nom_tseg2)))
720+
data_brp = round(f_clock / (data_bitrate * (1 + data_tseg1 + data_tseg2)))
723721
except ZeroDivisionError:
724722
raise ValueError("Invalid inputs.") from None
725723

@@ -787,15 +785,15 @@ def iterate_from_sample_point(
787785
sync_seg = 1
788786

789787
for nom_brp in range(1, 257):
790-
nbt = round(int(f_clock / (nom_bitrate * nom_brp)))
788+
nbt = int(f_clock / (nom_bitrate * nom_brp))
791789
if nbt < 1:
792790
break
793791

794792
effective_nom_bitrate = f_clock / (nbt * nom_brp)
795793
if abs(effective_nom_bitrate - nom_bitrate) > nom_bitrate / 256:
796794
continue
797795

798-
nom_tseg1 = int(round(nom_sample_point / 100 * nbt)) - 1
796+
nom_tseg1 = round(nom_sample_point / 100 * nbt) - 1
799797
# limit tseg1, so tseg2 is at least 2 TQ
800798
nom_tseg1 = min(nom_tseg1, nbt - sync_seg - 2)
801799
nom_tseg2 = nbt - nom_tseg1 - 1
@@ -811,7 +809,7 @@ def iterate_from_sample_point(
811809
if abs(effective_data_bitrate - data_bitrate) > data_bitrate / 256:
812810
continue
813811

814-
data_tseg1 = int(round(data_sample_point / 100 * dbt)) - 1
812+
data_tseg1 = round(data_sample_point / 100 * dbt) - 1
815813
# limit tseg1, so tseg2 is at least 2 TQ
816814
data_tseg1 = min(data_tseg1, dbt - sync_seg - 2)
817815
data_tseg2 = dbt - data_tseg1 - 1
@@ -923,7 +921,7 @@ def f_clock(self) -> int:
923921
@property
924922
def nom_bitrate(self) -> int:
925923
"""Nominal (arbitration phase) bitrate."""
926-
return int(round(self.f_clock / (self.nbt * self.nom_brp)))
924+
return round(self.f_clock / (self.nbt * self.nom_brp))
927925

928926
@property
929927
def nom_brp(self) -> int:
@@ -933,7 +931,7 @@ def nom_brp(self) -> int:
933931
@property
934932
def nom_tq(self) -> int:
935933
"""Nominal time quantum in nanoseconds"""
936-
return int(round(self.nom_brp / self.f_clock * 1e9))
934+
return round(self.nom_brp / self.f_clock * 1e9)
937935

938936
@property
939937
def nbt(self) -> int:
@@ -969,7 +967,7 @@ def nom_sample_point(self) -> float:
969967
@property
970968
def data_bitrate(self) -> int:
971969
"""Bitrate of the data phase in bit/s."""
972-
return int(round(self.f_clock / (self.dbt * self.data_brp)))
970+
return round(self.f_clock / (self.dbt * self.data_brp))
973971

974972
@property
975973
def data_brp(self) -> int:
@@ -979,7 +977,7 @@ def data_brp(self) -> int:
979977
@property
980978
def data_tq(self) -> int:
981979
"""Data time quantum in nanoseconds"""
982-
return int(round(self.data_brp / self.f_clock * 1e9))
980+
return round(self.data_brp / self.f_clock * 1e9)
983981

984982
@property
985983
def dbt(self) -> int:
@@ -1106,10 +1104,10 @@ def recreate_with_f_clock(self, f_clock: int) -> "BitTimingFd":
11061104
"f_clock change failed because of sample point discrepancy."
11071105
)
11081106
# adapt synchronization jump width, so it has the same size relative to bit time as self
1109-
nom_sjw = int(round(self.nom_sjw / self.nbt * bt.nbt))
1107+
nom_sjw = round(self.nom_sjw / self.nbt * bt.nbt)
11101108
nom_sjw = max(1, min(bt.nom_tseg2, nom_sjw))
11111109
bt._data["nom_sjw"] = nom_sjw # pylint: disable=protected-access
1112-
data_sjw = int(round(self.data_sjw / self.dbt * bt.dbt))
1110+
data_sjw = round(self.data_sjw / self.dbt * bt.dbt)
11131111
data_sjw = max(1, min(bt.data_tseg2, data_sjw))
11141112
bt._data["data_sjw"] = data_sjw # pylint: disable=protected-access
11151113
bt._validate() # pylint: disable=protected-access
@@ -1138,7 +1136,7 @@ def __repr__(self) -> str:
11381136
return f"can.{self.__class__.__name__}({args})"
11391137

11401138
def __getitem__(self, key: str) -> int:
1141-
return cast(int, self._data.__getitem__(key))
1139+
return cast("int", self._data.__getitem__(key))
11421140

11431141
def __len__(self) -> int:
11441142
return self._data.__len__()

can/broadcastmanager.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def create_timer(self) -> _Pywin32Event:
6161
):
6262
event = self.win32event.CreateWaitableTimer(None, False, None)
6363

64-
return cast(_Pywin32Event, event)
64+
return cast("_Pywin32Event", event)
6565

6666
def set_timer(self, event: _Pywin32Event, period_ms: int) -> None:
6767
self.win32event.SetWaitableTimer(event.handle, 0, period_ms, None, None, False)
@@ -121,12 +121,12 @@ def __init__(
121121
# Take the Arbitration ID of the first element
122122
self.arbitration_id = messages[0].arbitration_id
123123
self.period = period
124-
self.period_ns = int(round(period * 1e9))
124+
self.period_ns = round(period * 1e9)
125125
self.messages = messages
126126

127127
@staticmethod
128128
def _check_and_convert_messages(
129-
messages: Union[Sequence[Message], Message]
129+
messages: Union[Sequence[Message], Message],
130130
) -> Tuple[Message, ...]:
131131
"""Helper function to convert a Message or Sequence of messages into a
132132
tuple, and raises an error when the given value is invalid.

can/bus.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def send_periodic(
276276

277277
# Create a backend specific task; will be patched to a _SelfRemovingCyclicTask later
278278
task = cast(
279-
_SelfRemovingCyclicTask,
279+
"_SelfRemovingCyclicTask",
280280
self._send_periodic_internal(
281281
msgs, period, duration, autostart, modifier_callback
282282
),
@@ -452,7 +452,7 @@ def _matches_filters(self, msg: Message) -> bool:
452452
for _filter in self._filters:
453453
# check if this filter even applies to the message
454454
if "extended" in _filter:
455-
_filter = cast(can.typechecking.CanFilterExtended, _filter)
455+
_filter = cast("can.typechecking.CanFilterExtended", _filter)
456456
if _filter["extended"] != msg.is_extended_id:
457457
continue
458458

can/ctypesutil.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
log = logging.getLogger("can.ctypesutil")
1111

12-
__all__ = ["CLibrary", "HANDLE", "PHANDLE", "HRESULT"]
12+
__all__ = ["HANDLE", "HRESULT", "PHANDLE", "CLibrary"]
1313

1414
if sys.platform == "win32":
1515
_LibBase = ctypes.WinDLL

can/interface.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def _get_class_for_interface(interface: str) -> Type[BusABC]:
5252
f"'{interface}': {e}"
5353
) from None
5454

55-
return cast(Type[BusABC], bus_class)
55+
return cast("Type[BusABC]", bus_class)
5656

5757

5858
@util.deprecated_args_alias(
@@ -138,7 +138,7 @@ def Bus( # noqa: N802
138138

139139

140140
def detect_available_configs(
141-
interfaces: Union[None, str, Iterable[str]] = None
141+
interfaces: Union[None, str, Iterable[str]] = None,
142142
) -> List[AutoDetectedConfig]:
143143
"""Detect all configurations/channels that the interfaces could
144144
currently connect with.

can/interfaces/ics_neovi/__init__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""
2-
"""
1+
""" """
32

43
__all__ = [
54
"ICSApiError",

can/interfaces/ixxat/canlib_vcinpl.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@
3535
from .exceptions import *
3636

3737
__all__ = [
38-
"VCITimeout",
39-
"VCIError",
38+
"IXXATBus",
4039
"VCIBusOffError",
4140
"VCIDeviceNotFoundError",
42-
"IXXATBus",
41+
"VCIError",
42+
"VCITimeout",
4343
"vciFormatError",
4444
]
4545

@@ -890,7 +890,7 @@ def __init__(
890890
self._count = int(duration / period) if duration else 0
891891

892892
self._msg = structures.CANCYCLICTXMSG()
893-
self._msg.wCycleTime = int(round(period * resolution))
893+
self._msg.wCycleTime = round(period * resolution)
894894
self._msg.dwMsgId = self.messages[0].arbitration_id
895895
self._msg.uMsgInfo.Bits.type = constants.CAN_MSGTYPE_DATA
896896
self._msg.uMsgInfo.Bits.ext = 1 if self.messages[0].is_extended_id else 0

can/interfaces/ixxat/canlib_vcinpl2.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
from .exceptions import *
3535

3636
__all__ = [
37-
"VCITimeout",
38-
"VCIError",
37+
"IXXATBus",
3938
"VCIBusOffError",
4039
"VCIDeviceNotFoundError",
41-
"IXXATBus",
40+
"VCIError",
41+
"VCITimeout",
4242
"vciFormatError",
4343
]
4444

@@ -1010,7 +1010,7 @@ def __init__(
10101010
self._count = int(duration / period) if duration else 0
10111011

10121012
self._msg = structures.CANCYCLICTXMSG2()
1013-
self._msg.wCycleTime = int(round(period * resolution))
1013+
self._msg.wCycleTime = round(period * resolution)
10141014
self._msg.dwMsgId = self.messages[0].arbitration_id
10151015
self._msg.uMsgInfo.Bits.type = constants.CAN_MSGTYPE_DATA
10161016
self._msg.uMsgInfo.Bits.ext = 1 if self.messages[0].is_extended_id else 0

can/interfaces/ixxat/exceptions.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
)
1313

1414
__all__ = [
15-
"VCITimeout",
16-
"VCIError",
17-
"VCIRxQueueEmptyError",
1815
"VCIBusOffError",
1916
"VCIDeviceNotFoundError",
17+
"VCIError",
18+
"VCIRxQueueEmptyError",
19+
"VCITimeout",
2020
]
2121

2222

can/interfaces/ixxat/structures.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,13 @@ class CANBTP(ctypes.Structure):
201201
]
202202

203203
def __str__(self):
204-
return "dwMode=%d, dwBPS=%d, wTS1=%d, wTS2=%d, wSJW=%d, wTDO=%d" % (
205-
self.dwMode,
206-
self.dwBPS,
207-
self.wTS1,
208-
self.wTS2,
209-
self.wSJW,
210-
self.wTDO,
204+
return (
205+
f"dwMode={self.dwMode:d}, "
206+
f"dwBPS={self.dwBPS:d}, "
207+
f"wTS1={self.wTS1:d}, "
208+
f"wTS2={self.wTS2:d}, "
209+
f"wSJW={self.wSJW:d}, "
210+
f"wTDO={self.wTDO:d}"
211211
)
212212

213213

can/interfaces/kvaser/__init__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""
2-
"""
1+
""" """
32

43
__all__ = [
54
"CANLIBInitializationError",

0 commit comments

Comments
 (0)