Skip to content

Commit f1808ed

Browse files
authored
Address pylint issues (#1239)
* Adress pylint issues * Format code with black * Fix line-too-long * Fix abstract-method * Fix line-too-long Co-authored-by: felixdivo <[email protected]>
1 parent 28513eb commit f1808ed

File tree

6 files changed

+22
-23
lines changed

6 files changed

+22
-23
lines changed

can/broadcastmanager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import threading
2020
import time
2121

22-
# try to import win32event for event-based cyclic send task(needs pywin32 package)
22+
# try to import win32event for event-based cyclic send task (needs the pywin32 package)
2323
try:
2424
import win32event
2525

@@ -260,7 +260,7 @@ def stop(self) -> None:
260260
def start(self) -> None:
261261
self.stopped = False
262262
if self.thread is None or not self.thread.is_alive():
263-
name = "Cyclic send task for 0x%X" % (self.messages[0].arbitration_id)
263+
name = f"Cyclic send task for 0x{self.messages[0].arbitration_id:X}"
264264
self.thread = threading.Thread(target=self._run, name=name)
265265
self.thread.daemon = True
266266

can/exceptions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
For example, validating typical arguments and parameters might result in a
1515
:class:`ValueError`. This should always be documented for the function at hand.
1616
"""
17+
1718
import sys
1819
from contextlib import contextmanager
1920

@@ -114,7 +115,7 @@ def error_check(
114115
"""Catches any exceptions and turns them into the new type while preserving the stack trace."""
115116
try:
116117
yield
117-
except Exception as error:
118+
except Exception as error: # pylint: disable=broad-except
118119
if error_message is None:
119120
raise exception_type(str(error)) from error
120121
else:

can/interface.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,24 @@ def _get_class_for_interface(interface: str) -> Type[BusABC]:
3232
module_name, class_name = BACKENDS[interface]
3333
except KeyError:
3434
raise NotImplementedError(
35-
"CAN interface '{}' not supported".format(interface)
35+
f"CAN interface '{interface}' not supported"
3636
) from None
3737

3838
# Import the correct interface module
3939
try:
4040
module = importlib.import_module(module_name)
4141
except Exception as e:
4242
raise CanInterfaceNotImplementedError(
43-
"Cannot import module {} for CAN interface '{}': {}".format(
44-
module_name, interface, e
45-
)
43+
f"Cannot import module {module_name} for CAN interface '{interface}': {e}"
4644
) from None
4745

4846
# Get the correct class
4947
try:
5048
bus_class = getattr(module, class_name)
5149
except Exception as e:
5250
raise CanInterfaceNotImplementedError(
53-
"Cannot import class {} from module {} for CAN interface '{}': {}".format(
54-
class_name, module_name, interface, e
55-
)
51+
f"Cannot import class {class_name} from module {module_name} for CAN interface "
52+
f"'{interface}': {e}"
5653
) from None
5754

5855
return cast(Type[BusABC], bus_class)

can/listener.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def stop(self) -> None:
5858
"""
5959

6060

61-
class RedirectReader(Listener):
61+
class RedirectReader(Listener): # pylint: disable=abstract-method
6262
"""
6363
A RedirectReader sends all received messages to another Bus.
6464
"""
@@ -71,7 +71,7 @@ def on_message_received(self, msg: Message) -> None:
7171
self.bus.send(msg)
7272

7373

74-
class BufferedReader(Listener):
74+
class BufferedReader(Listener): # pylint: disable=abstract-method
7575
"""
7676
A BufferedReader is a subclass of :class:`~can.Listener` which implements a
7777
**message buffer**: that is, when the :class:`can.BufferedReader` instance is
@@ -126,7 +126,7 @@ def stop(self) -> None:
126126
self.is_stopped = True
127127

128128

129-
class AsyncBufferedReader(Listener):
129+
class AsyncBufferedReader(Listener): # pylint: disable=abstract-method
130130
"""A message buffer for use with :mod:`asyncio`.
131131
132132
See :ref:`asyncio` for how to use with :class:`can.Notifier`.

can/logconvert.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class ArgumentParser(argparse.ArgumentParser):
1313
def error(self, message):
1414
self.print_help(sys.stderr)
15-
self.exit(errno.EINVAL, "%s: error: %s\n" % (self.prog, message))
15+
self.exit(errno.EINVAL, f"{self.prog}: error: {message}\n")
1616

1717

1818
def main():

can/viewer.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import time
3030
from typing import Dict, List, Tuple, Union
3131

32-
import can
3332
from can import __version__
3433
from .logger import (
3534
_create_bus,
@@ -53,7 +52,7 @@
5352
curses = None # type: ignore
5453

5554

56-
class CanViewer:
55+
class CanViewer: # pylint: disable=too-many-instance-attributes
5756
def __init__(self, stdscr, bus, data_structs, testing=False):
5857
self.stdscr = stdscr
5958
self.bus = bus
@@ -89,7 +88,7 @@ def run(self):
8988
# Clear the terminal and draw the header
9089
self.draw_header()
9190

92-
while 1:
91+
while True:
9392
# Do not read the CAN-Bus when in paused mode
9493
if not self.paused:
9594
# Read the CAN-Bus and draw it in the terminal window
@@ -237,8 +236,11 @@ def draw_can_bus_message(self, msg, sorting=False):
237236
self.ids[key]["count"] += 1
238237

239238
# Format the CAN-Bus ID as a hex value
240-
arbitration_id_string = "0x{0:0{1}X}".format(
241-
msg.arbitration_id, 8 if msg.is_extended_id else 3
239+
arbitration_id_string = (
240+
"0x{0:0{1}X}".format( # pylint: disable=consider-using-f-string
241+
msg.arbitration_id,
242+
8 if msg.is_extended_id else 3,
243+
)
242244
)
243245

244246
# Use red for error frames
@@ -263,7 +265,7 @@ def draw_can_bus_message(self, msg, sorting=False):
263265
previous_byte_values = self.previous_values[key]
264266
except KeyError: # no row of previous values exists for the current message ID
265267
# initialise a row to store the values for comparison next time
266-
self.previous_values[key] = dict()
268+
self.previous_values[key] = {}
267269
previous_byte_values = self.previous_values[key]
268270
for i, b in enumerate(msg.data):
269271
col = 52 + i * 3
@@ -279,7 +281,7 @@ def draw_can_bus_message(self, msg, sorting=False):
279281
else:
280282
data_color = color
281283
except KeyError:
282-
# previous entry for byte didnt exist - default to rest of line colour
284+
# previous entry for byte didn't exist - default to rest of line colour
283285
data_color = color
284286
finally:
285287
# write the new value to the previous values dict for next time
@@ -336,7 +338,7 @@ def draw_header(self):
336338
def redraw_screen(self):
337339
# Trigger a complete redraw
338340
self.draw_header()
339-
for key, ids in self.ids.items():
341+
for ids in self.ids.values():
340342
self.draw_can_bus_message(ids["msg"])
341343

342344

@@ -545,7 +547,6 @@ def main() -> None:
545547
if can_filters:
546548
additional_config.update({"can_filters": can_filters})
547549
bus = _create_bus(parsed_args, **additional_config)
548-
# print(f"Connected to {bus.__class__.__name__}: {bus.channel_info}")
549550

550551
curses.wrapper(CanViewer, bus, data_structs)
551552

0 commit comments

Comments
 (0)