From ce087f6fce40517b8720a0c38eeb8e8f45a44baa Mon Sep 17 00:00:00 2001 From: Andreas Lauser Date: Wed, 14 Aug 2024 09:01:14 +0200 Subject: [PATCH] fix errors raised by recent versions of `ruff` this makes the CI pass again. --- docs/conf.py | 2 +- src/cantools/database/can/database.py | 4 +- tests/test_database.py | 2 +- tests/test_plot.py | 84 +++++++++++++-------------- tests/test_plot_unittests.py | 6 +- tests/test_plot_without_mock.py | 2 +- 6 files changed, 50 insertions(+), 50 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 7e870419..76b7b107 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -55,7 +55,7 @@ # General information about the project. project = "cantools" -copyright = "2015-2023, Erik Moqvist" +project_copyright = "2015-2023, Erik Moqvist" author = "Erik Moqvist" # The version info for the project you're documenting, acts as replacement for diff --git a/src/cantools/database/can/database.py b/src/cantools/database/can/database.py index 9fe9f349..12319d62 100644 --- a/src/cantools/database/can/database.py +++ b/src/cantools/database/can/database.py @@ -158,7 +158,7 @@ def _objects_similar(a: Any, tolerance: float, include_format_specifics: bool) -> bool: - if type(a) != type(b): + if type(a) is not type(b): # the types of the objects do not match return False elif a is None: @@ -221,7 +221,7 @@ def _objects_similar(a: Any, a_attrib = getattr(a, attrib_name) b_attrib = getattr(b, attrib_name) - if type(a_attrib) != type(b_attrib): + if type(a_attrib) is not type(b_attrib): return False elif callable(a_attrib): # ignore callable attributes diff --git a/tests/test_database.py b/tests/test_database.py index 82ca2445..20e8a9c0 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -2451,7 +2451,7 @@ def test_comments_hex_and_motorola_sym(self): for i in range(8): sig = msg2.signals[i] - self.assertEqual(sig.name, 'Test%s' % i) + self.assertEqual(sig.name, f'Test{i}') self.assertEqual(sig.start, i*8 + 7) self.assertEqual(sig.length, 8) self.assertEqual(sig.receivers, []) diff --git a/tests/test_plot.py b/tests/test_plot.py index dbb13cca..7f2a1a32 100755 --- a/tests/test_plot.py +++ b/tests/test_plot.py @@ -32,7 +32,7 @@ def mock_calls(self): return self._mock.mock_calls def __enter__(self): - self._patches = [mock.patch('matplotlib.pyplot.%s' % a, getattr(self,a)) for a in self._attrs] + self._patches = [mock.patch(f'matplotlib.pyplot.{a}', getattr(self,a)) for a in self._attrs] for p in self._patches: p.__enter__() return self @@ -62,13 +62,13 @@ def __init__(self, ignore=True, *, parent=None, **kw): for a in self._ATTRIBUTES_WHICH_CAN_BE_IGNORED: key = key_pattern % a if kw.pop(key, ignore): - setattr(self, a, mock.Mock(name='%s-mock'%a)) + setattr(self, a, mock.Mock(name=f'{a}-mock')) self.__kw[key] = True else: self.__kw[key] = False if kw: - raise TypeError("{}() got unexpected keyword argument(s): {}".format(type(self).__name__, ', '.join('%r'%key for key in kw.keys()))) + raise TypeError("{}() got unexpected keyword argument(s): {}".format(type(self).__name__, ', '.join(key for key in kw.keys()))) if parent: parent.attach_mock(self, 'subplot()') @@ -88,7 +88,7 @@ def __plot(self, xs, ys, fmt, *, label=None): out = [mock.Mock()] if not self.__ignore_color: for i, m in enumerate(out): - self.attach_mock(m, 'plot()._getitem_%s' % i) + self.attach_mock(m, f'plot()._getitem_{i}') return out @@ -99,7 +99,7 @@ def __stem(self, xs, ys, fmt, *, label=None): out = [mock.Mock() for i in range(3)] if not self.__ignore_color: for i, m in enumerate(out): - self.attach_mock(m, 'stem()[%s]' % i) + self.attach_mock(m, f'stem()[{i}]') return out @@ -251,7 +251,7 @@ def test_plot_tz(self): cantools._main() self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") def test_plot_td(self): @@ -490,7 +490,7 @@ def test_wildcards_caseinsensitive(self): cantools._main() self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") def test_subplots(self): argv = ['cantools', 'plot', self.DBC_FILE, 'BREMSE_33.*', '-', 'BREMSE_2.*'] @@ -549,7 +549,7 @@ def test_subplots(self): cantools._main() self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") def test_two_axes_with_auto_ylabels_and_one_legend(self): argv = ['cantools', 'plot', self.DBC_FILE, '*_33.*fl*:b', ',', '*_2.*fl*:r'] @@ -599,7 +599,7 @@ def test_two_axes_with_auto_ylabels_and_one_legend(self): cantools._main() self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") def test_color(self): argv = ['cantools', 'plot', self.DBC_FILE, '--', '--color', 'C0', '--ylabel', 'Bremse 33', '*_33.*fl*', ',', '--color', 'C1', '--ylabel', 'Bremse 2', '*_2.*fl*'] @@ -654,7 +654,7 @@ def test_color(self): cantools._main() self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") def test_autocolor(self): self.maxDiff = None @@ -730,7 +730,7 @@ def test_autocolor(self): cantools._main() self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") def test_format(self): col_33 = "b" @@ -794,7 +794,7 @@ def test_format(self): cantools._main() self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") def test_choices_stem(self): argv = ['cantools', 'plot', self.DBC_FILE_CHOICES, 'Foo:|'] @@ -838,7 +838,7 @@ def test_choices_stem(self): cantools._main() self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") def test_no_decode_choices(self): argv = ['cantools', 'plot', '--no-decode-choices', self.DBC_FILE_CHOICES, 'Foo:|'] @@ -878,7 +878,7 @@ def test_no_decode_choices(self): cantools._main() self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") def test_case_sensitive(self): argv = ['cantools', 'plot', '--case-sensitive', self.DBC_FILE, '*fl*'] @@ -916,7 +916,7 @@ def test_case_sensitive(self): self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") # ------- test error handling ------- @@ -994,7 +994,7 @@ def test_error_messages(self): self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") # --ignore-* @@ -1038,7 +1038,7 @@ def test_ignore_invalid_syntax(self): self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") def test_ignore_unknown_frameid(self): argv = ['cantools', 'plot', '--ignore-unknown-frames', self.DBC_FILE, '*33.*'] @@ -1079,7 +1079,7 @@ def test_ignore_unknown_frameid(self): self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") def test_ignore_invalid_data(self): argv = ['cantools', 'plot', '--ignore-invalid-data', self.DBC_FILE, '*33.*'] @@ -1121,7 +1121,7 @@ def test_ignore_invalid_data(self): self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") def test_quiet(self): argv = ['cantools', 'plot', '-q', self.DBC_FILE, '*33.*'] @@ -1159,7 +1159,7 @@ def test_quiet(self): self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") # --show-* @@ -1208,7 +1208,7 @@ def test_show_invalid_data(self): self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") def test_show_unknown_frames(self): argv = ['cantools', 'plot', '--show-unknown-frames', self.DBC_FILE, '*33.*'] @@ -1256,7 +1256,7 @@ def test_show_unknown_frames(self): self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") def test_show_invalid_syntax(self): argv = ['cantools', 'plot', '--show-invalid-syntax', self.DBC_FILE, '*33.*'] @@ -1304,7 +1304,7 @@ def test_show_invalid_syntax(self): self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") def test_show_all_errors(self): argv = ['cantools', 'plot', '-s', self.DBC_FILE, '*33.*'] @@ -1358,7 +1358,7 @@ def test_show_all_errors(self): self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") # ------- test other command line options ------- @@ -1413,18 +1413,18 @@ def test_break_time(self): n0 = len(whlspeed_0[i]) n1 = len(whlspeed_1[i]) - self.assertEqual(len(actual_xs), len(actual_ys), "numbers of actual x and y values do not match for signal %s" % label) - self.assertEqual(len(actual_xs), len(xs)+1, "number of x values does not match for signal %s" % label) - self.assertEqual(len(actual_ys), n0+n1+1, "number of y values does not match for signal %s" % label) + self.assertEqual(len(actual_xs), len(actual_ys), f"numbers of actual x and y values do not match for signal {label}") + self.assertEqual(len(actual_xs), len(xs)+1, f"number of x values does not match for signal {label}") + self.assertEqual(len(actual_ys), n0+n1+1, f"number of y values does not match for signal {label}") for j in range(1, len(actual_xs)): - self.assertLess(actual_xs[j-1], actual_xs[j], "actual x values are not strictly increasing for signal %s" % label) - self.assertEqual(actual_xs[:n0], xs[:n0], "first half of x values does not match for signal %s" % label) - self.assertEqual(actual_xs[-n1:], xs[-n1:], "second half of x values does not match for signal %s" % label) + self.assertLess(actual_xs[j-1], actual_xs[j], f"actual x values are not strictly increasing for signal {label}") + self.assertEqual(actual_xs[:n0], xs[:n0], f"first half of x values does not match for signal {label}") + self.assertEqual(actual_xs[-n1:], xs[-n1:], f"second half of x values does not match for signal {label}") - self.assertEqual(actual_ys[:n0], whlspeed_0[i], "first half of y values does not match for signal %s" % label) - self.assertEqual(actual_ys[n0], None, "expected separating value is not at it's place for signal %s" % label) - self.assertEqual(actual_ys[-n1:], whlspeed_1[i], "second half of y values does not match for signal %s" % label) + self.assertEqual(actual_ys[:n0], whlspeed_0[i], f"first half of y values does not match for signal {label}") + self.assertEqual(actual_ys[n0], None, f"expected separating value is not at it's place for signal {label}") + self.assertEqual(actual_ys[-n1:], whlspeed_1[i], f"second half of y values does not match for signal {label}") #self.assertEqual(actual_ys[-n1:], actual_ys[n1+1:], "this test is a duplicate of len == n0+n1+1") def test_break_time_none(self): @@ -1476,7 +1476,7 @@ def test_break_time_none(self): cantools._main() self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") def test_output_file(self): @@ -1512,7 +1512,7 @@ def test_output_file(self): ] stdout = StringIO() - expected_output = "Result written to %s\n" % fn + expected_output = f"Result written to {fn}\n" with mock.patch('sys.stdin', StringIO(input_data)): with mock.patch('sys.stdout', stdout): @@ -1724,7 +1724,7 @@ def test_ylabel(self): cantools._main() self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") self.assertEqual(stdout.getvalue(), expected_output) def test_title(self): @@ -1804,7 +1804,7 @@ def test_xlabel(self): cantools._main() self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") self.assertEqual(stdout.getvalue(), expected_output) def test_ymin(self): @@ -1928,7 +1928,7 @@ def test_global_ylabel(self): cantools._main() self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") self.assertEqual(stdout.getvalue(), expected_output) def test_global_title(self): @@ -2008,7 +2008,7 @@ def test_global_xlabel(self): cantools._main() self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") self.assertEqual(stdout.getvalue(), expected_output) def test_global_ymin(self): @@ -2122,7 +2122,7 @@ def test_empty_line(self): cantools._main() self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") def test_do_not_replot(self): argv = ['cantools', 'plot', self.DBC_FILE, '*fl:-o', '*:o'] @@ -2166,7 +2166,7 @@ def test_do_not_replot(self): cantools._main() self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") def test_do_replot(self): argv = ['cantools', 'plot', self.DBC_FILE_CHOICES, "Foo:b-", "Foo:rd"] @@ -2209,7 +2209,7 @@ def test_do_replot(self): cantools._main() self.assertListEqual(plt.mock_calls, expected_calls) for i in range(len(expected_subplot_calls)): - self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg="calls don't match for subplot %s" % i) + self.assertListEqual(subplots[i].mock_calls, expected_subplot_calls[i], msg=f"calls don't match for subplot {i}") # ------- auxiliary functions ------- diff --git a/tests/test_plot_unittests.py b/tests/test_plot_unittests.py index f2833b55..2db7921f 100644 --- a/tests/test_plot_unittests.py +++ b/tests/test_plot_unittests.py @@ -41,7 +41,7 @@ def test_parse_user_input_absolute_time_output_formats(self): ('second', '2021-02-03 04:05:06', '2021-02-03 04:05:42'), ('microsecond', '2021-02-03 04:05:06.000007', '2021-02-03 04:05:06.0042'), ]: - pattern = plot.plt.rcParams["date.autoformatter.%s" % scope] + pattern = plot.plt.rcParams[f"date.autoformatter.{scope}"] first_timestamp = self.parse_time(first_timestamp) expected = self.parse_time(expected) user_input = expected.strftime(pattern) @@ -68,7 +68,7 @@ def test_parse_user_input_relative_time(self): ('3 days, 4:56:01.12', 3*24*60**2 + 4*60**2 + 56*60 + 1.12), ]: actual = sut.parse_user_input_relative_time(user_input, first_timestamp=0) - self.assertEqual(actual, expected, "unexpected result for %r" % user_input) + self.assertEqual(actual, expected, f"unexpected result for {user_input!r}") # ------- auxiliary functions ------- @@ -79,7 +79,7 @@ def parse_time(self, s): return datetime.datetime.strptime(s, pattern) except ValueError: pass - raise ValueError('Failed to parse time %r' % s) + raise ValueError('Failed to parse time {s}') if __name__ == '__main__': diff --git a/tests/test_plot_without_mock.py b/tests/test_plot_without_mock.py index 93379315..ab05b6c7 100755 --- a/tests/test_plot_without_mock.py +++ b/tests/test_plot_without_mock.py @@ -67,7 +67,7 @@ def test_plot_list_styles(self): stdout = StringIO() expected = "available matplotlib styles:" - expected += "".join("\n- %s" % s for s in plt.style.available) + expected += "".join(f"\n- {s}" for s in plt.style.available) expected += "\n" with mock.patch('sys.stdout', stdout):