From 768f9bcbf4b5acd09dda85ab32c0ea30d8826136 Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Mon, 28 Feb 2022 10:54:43 -0500 Subject: [PATCH] Use Black stable to format python code The first stable release of the Black code formatter, 22.1.0 [1] is now available with a promise of code style stability for all versions sharing the same major version number [2]. Set the requirements to 'black ~= 22.0' to get bug fix releases while maintaining a stable code style. [1] https://github.com/psf/black/releases/tag/22.1.0 [2] https://black.readthedocs.io/en/latest/the_black_code_style/index.html#stability-policy Change-Id: I8b7843deff87464237a9214d4ea3bf18e4d987a1 Signed-off-by: Michael Jeanson Reviewed-on: https://review.lttng.org/c/babeltrace/+/7406 Reviewed-by: Philippe Proulx Tested-by: jenkins --- dev-requirements.txt | 2 +- src/bindings/python/bt2/bt2/field.py | 2 +- src/bindings/python/bt2/bt2/utils.py | 4 +- tests/bindings/python/bt2/test_clock_class.py | 6 +-- tests/bindings/python/bt2/test_field.py | 6 +-- tests/bindings/python/bt2/test_value.py | 8 ++-- tests/utils/python/tap/adapter.py | 1 + tests/utils/python/tap/directive.py | 6 ++- tests/utils/python/tap/line.py | 9 ++-- tests/utils/python/tap/main.py | 21 +++++++--- tests/utils/python/tap/parser.py | 26 ++++++++---- tests/utils/python/tap/rules.py | 28 +++++++------ tests/utils/python/tap/runner.py | 40 ++++++++++-------- tests/utils/python/tap/tracker.py | 42 +++++++++++-------- 14 files changed, 122 insertions(+), 79 deletions(-) diff --git a/dev-requirements.txt b/dev-requirements.txt index 8a6bc811..452c0de6 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,2 +1,2 @@ -black == 20.8b1 +black ~= 22.0 flake8 >= 3.8 diff --git a/src/bindings/python/bt2/bt2/field.py b/src/bindings/python/bt2/bt2/field.py index 4eb08e6b..8af7b769 100644 --- a/src/bindings/python/bt2/bt2/field.py +++ b/src/bindings/python/bt2/bt2/field.py @@ -354,7 +354,7 @@ class _UnsignedIntegerField(_UnsignedIntegerFieldConst, _IntegerField, _Field): @property def _upper_bound(self): - return (2 ** self.cls.field_value_range) - 1 + return (2**self.cls.field_value_range) - 1 class _SignedIntegerFieldConst(_IntegerFieldConst, _FieldConst): diff --git a/src/bindings/python/bt2/bt2/utils.py b/src/bindings/python/bt2/bt2/utils.py index b53032d9..d7e99ca6 100644 --- a/src/bindings/python/bt2/bt2/utils.py +++ b/src/bindings/python/bt2/bt2/utils.py @@ -36,7 +36,7 @@ def _check_type(o, expected_type): def _is_in_int64_range(v): assert isinstance(v, int) - return v >= -(2 ** 63) and v <= (2 ** 63 - 1) + return v >= -(2**63) and v <= (2**63 - 1) def _is_int64(v): @@ -48,7 +48,7 @@ def _is_int64(v): def _is_in_uint64_range(v): assert isinstance(v, int) - return v >= 0 and v <= (2 ** 64 - 1) + return v >= 0 and v <= (2**64 - 1) def _is_uint64(v): diff --git a/tests/bindings/python/bt2/test_clock_class.py b/tests/bindings/python/bt2/test_clock_class.py index 8a32af8d..04df66ad 100644 --- a/tests/bindings/python/bt2/test_clock_class.py +++ b/tests/bindings/python/bt2/test_clock_class.py @@ -160,7 +160,7 @@ class ClockClassTestCase(unittest.TestCase): def test_cycles_to_ns_from_origin(self): def f(comp_self): return comp_self._create_clock_class( - frequency=10 ** 8, origin_is_unix_epoch=True + frequency=10**8, origin_is_unix_epoch=True ) cc = run_in_component_init(f) @@ -172,7 +172,7 @@ class ClockClassTestCase(unittest.TestCase): cc = run_in_component_init(f) with self.assertRaises(bt2._OverflowError): - cc.cycles_to_ns_from_origin(2 ** 63) + cc.cycles_to_ns_from_origin(2**63) def test_create_uuid(self): def f(comp_self): @@ -243,7 +243,7 @@ class ClockSnapshotTestCase(unittest.TestCase): elif self._at == 1: notif = self._create_event_message(_ec, _stream, 123) elif self._at == 2: - notif = self._create_event_message(_ec, _stream, 2 ** 63) + notif = self._create_event_message(_ec, _stream, 2**63) elif self._at == 3: notif = self._create_stream_end_message(_stream) else: diff --git a/tests/bindings/python/bt2/test_field.py b/tests/bindings/python/bt2/test_field.py index 37afffb5..6815715e 100644 --- a/tests/bindings/python/bt2/test_field.py +++ b/tests/bindings/python/bt2/test_field.py @@ -1231,8 +1231,8 @@ class _TestIntegerFieldCommon(_TestNumericField): field = _create_field(self._tc, uint_fc) # Larger than the IEEE 754 double-precision exact representation of # integers. - raw = (2 ** 53) + 1 - field.value = (2 ** 53) + 1 + raw = (2**53) + 1 + field.value = (2**53) + 1 self.assertEqual(field, raw) def test_assign_uint_out_of_range(self): @@ -1304,7 +1304,7 @@ class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase) fc.add_mapping('speaker', bt2.SignedIntegerRangeSet([(12, 16)])) fc.add_mapping('can', bt2.SignedIntegerRangeSet([(18, 2540)])) fc.add_mapping( - 'whole range', bt2.SignedIntegerRangeSet([(-(2 ** 31), (2 ** 31) - 1)]) + 'whole range', bt2.SignedIntegerRangeSet([(-(2**31), (2**31) - 1)]) ) fc.add_mapping('zip', bt2.SignedIntegerRangeSet([(-45, 1001)])) return fc diff --git a/tests/bindings/python/bt2/test_value.py b/tests/bindings/python/bt2/test_value.py index 8a30c87f..e7f2ba2a 100644 --- a/tests/bindings/python/bt2/test_value.py +++ b/tests/bindings/python/bt2/test_value.py @@ -1292,11 +1292,11 @@ class SignedIntegerValueTestCase(_TestIntegerValue, unittest.TestCase): def test_create_pos_too_big(self): with self._assert_expecting_int64(): - self._CLS(2 ** 63) + self._CLS(2**63) def test_create_neg_too_big(self): with self._assert_expecting_int64(): - self._CLS(-(2 ** 63) - 1) + self._CLS(-(2**63) - 1) def test_assign_neg_int(self): raw = -13 @@ -1306,7 +1306,7 @@ class SignedIntegerValueTestCase(_TestIntegerValue, unittest.TestCase): def test_compare_big_int(self): # Larger than the IEEE 754 double-precision exact representation of # integers. - raw = (2 ** 53) + 1 + raw = (2**53) + 1 v = bt2.create_value(raw) self.assertEqual(v, raw) @@ -1319,7 +1319,7 @@ class UnsignedIntegerValueTestCase(_TestIntegerValue, unittest.TestCase): def test_create_pos_too_big(self): with self._assert_expecting_uint64(): - self._CLS(2 ** 64) + self._CLS(2**64) def test_create_neg(self): with self._assert_expecting_uint64(): diff --git a/tests/utils/python/tap/adapter.py b/tests/utils/python/tap/adapter.py index 6dd5d446..3b8431de 100644 --- a/tests/utils/python/tap/adapter.py +++ b/tests/utils/python/tap/adapter.py @@ -8,6 +8,7 @@ class Adapter(object): It is an alternative to TestCase to collect TAP results. """ + failureException = AssertionError def __init__(self, filename, line): diff --git a/tests/utils/python/tap/directive.py b/tests/utils/python/tap/directive.py index 27e8bb2e..6be71bbb 100644 --- a/tests/utils/python/tap/directive.py +++ b/tests/utils/python/tap/directive.py @@ -12,12 +12,14 @@ class Directive(object): r"""^SKIP\S* (?P\s*) # Optional whitespace. (?P.*) # Slurp up the rest.""", - re.IGNORECASE | re.VERBOSE) + re.IGNORECASE | re.VERBOSE, + ) todo_pattern = re.compile( r"""^TODO\b # The directive name (?P\s*) # Immediately following must be whitespace. (?P.*) # Slurp up the rest.""", - re.IGNORECASE | re.VERBOSE) + re.IGNORECASE | re.VERBOSE, + ) def __init__(self, text): """Initialize the directive by parsing the text. diff --git a/tests/utils/python/tap/line.py b/tests/utils/python/tap/line.py index e0ed3581..31829582 100644 --- a/tests/utils/python/tap/line.py +++ b/tests/utils/python/tap/line.py @@ -8,6 +8,7 @@ class Line(object): TAP is a line based protocol. Thus, the most primitive type is a line. """ + @property def category(self): raise NotImplementedError @@ -17,8 +18,8 @@ class Result(Line): """Information about an individual test line.""" def __init__( - self, ok, number=None, description='', directive=None, - diagnostics=None): + self, ok, number=None, description='', directive=None, diagnostics=None + ): self._ok = ok if number: self._number = int(number) @@ -82,7 +83,8 @@ class Result(Line): if self.diagnostics is not None: diagnostics = '\n' + self.diagnostics.rstrip() return "{0}ok {1} - {2}{3}{4}".format( - is_not, self.number, self.description, directive, diagnostics) + is_not, self.number, self.description, directive, diagnostics + ) class Plan(Line): @@ -173,6 +175,7 @@ class Unknown(Line): This exists for the purpose of a Null Object pattern. """ + @property def category(self): """:returns: ``unknown``""" diff --git a/tests/utils/python/tap/main.py b/tests/utils/python/tap/main.py index 784b6466..04937f5c 100644 --- a/tests/utils/python/tap/main.py +++ b/tests/utils/python/tap/main.py @@ -34,15 +34,26 @@ def parse_args(argv): description = _('A TAP consumer for Python') epilog = _( 'When no files are given or a dash (-) is used for the file name, ' - 'tappy will read a TAP stream from STDIN.') + 'tappy will read a TAP stream from STDIN.' + ) parser = argparse.ArgumentParser(description=description, epilog=epilog) parser.add_argument( - 'files', metavar='FILE', nargs='*', help=_( + 'files', + metavar='FILE', + nargs='*', + help=_( 'A file containing TAP output. Any directories listed will be ' - 'scanned for files to include as TAP files.')) + 'scanned for files to include as TAP files.' + ), + ) parser.add_argument( - '-v', '--verbose', action='store_const', default=1, const=2, - help=_('use verbose messages')) + '-v', + '--verbose', + action='store_const', + default=1, + const=2, + help=_('use verbose messages'), + ) # argparse expects the executable to be removed from argv. args = parser.parse_args(argv[1:]) diff --git a/tests/utils/python/tap/parser.py b/tests/utils/python/tap/parser.py index 67f72a0e..bb89fbf2 100644 --- a/tests/utils/python/tap/parser.py +++ b/tests/utils/python/tap/parser.py @@ -26,20 +26,26 @@ class Parser(object): """ ok = re.compile(r'^ok' + result_base, re.VERBOSE) not_ok = re.compile(r'^not\ ok' + result_base, re.VERBOSE) - plan = re.compile(r""" + plan = re.compile( + r""" ^1..(?P\d+) # Match the plan details. [^#]* # Consume any non-hash character to confirm only # directives appear with the plan details. \#? # Optional directive marker. \s* # Optional whitespace. (?P.*) # Optional directive text. - """, re.VERBOSE) + """, + re.VERBOSE, + ) diagnostic = re.compile(r'^#') - bail = re.compile(r""" + bail = re.compile( + r""" ^Bail\ out! \s* # Optional whitespace. (?P.*) # Optional reason. - """, re.VERBOSE) + """, + re.VERBOSE, + ) version = re.compile(r'^TAP version (?P\d+)$') TAP_MINIMUM_DECLARED_VERSION = 13 @@ -119,12 +125,16 @@ class Parser(object): def _parse_result(self, ok, match): """Parse a matching result line into a result instance.""" return Result( - ok, match.group('number'), match.group('description').strip(), - Directive(match.group('directive'))) + ok, + match.group('number'), + match.group('description').strip(), + Directive(match.group('directive')), + ) def _parse_version(self, match): version = int(match.group('version')) if version < self.TAP_MINIMUM_DECLARED_VERSION: - raise ValueError(_('It is an error to explicitly specify ' - 'any version lower than 13.')) + raise ValueError( + _('It is an error to explicitly specify ' 'any version lower than 13.') + ) return Version(version) diff --git a/tests/utils/python/tap/rules.py b/tests/utils/python/tap/rules.py index 56f672d3..99b33e7f 100644 --- a/tests/utils/python/tap/rules.py +++ b/tests/utils/python/tap/rules.py @@ -9,7 +9,6 @@ from tap.line import Result class Rules(object): - def __init__(self, filename, suite): self._filename = filename self._suite = suite @@ -41,15 +40,19 @@ class Rules(object): plan, at_line = self._lines_seen['plan'][0] if not self._plan_on_valid_line(at_line, final_line_count): self._add_error( - _('A plan must appear at the beginning or end of the file.')) + _('A plan must appear at the beginning or end of the file.') + ) return if plan.expected_tests != self._lines_seen['test']: - self._add_error(_( - 'Expected {expected_count} tests ' - 'but only {seen_count} ran.').format( + self._add_error( + _( + 'Expected {expected_count} tests ' 'but only {seen_count} ran.' + ).format( expected_count=plan.expected_tests, - seen_count=self._lines_seen['test'])) + seen_count=self._lines_seen['test'], + ) + ) def _plan_on_valid_line(self, at_line, final_line_count): """Check if a plan is on a valid line.""" @@ -59,9 +62,10 @@ class Rules(object): # The plan may only appear on line 2 if the version is at line 1. after_version = ( - self._lines_seen['version'] and - self._lines_seen['version'][0] == 1 and - at_line == 2) + self._lines_seen['version'] + and self._lines_seen['version'][0] == 1 + and at_line == 2 + ) if after_version: return True @@ -73,13 +77,11 @@ class Rules(object): def handle_file_does_not_exist(self): """Handle a test file that does not exist.""" - self._add_error(_('{filename} does not exist.').format( - filename=self._filename)) + self._add_error(_('{filename} does not exist.').format(filename=self._filename)) def handle_skipping_plan(self, skip_plan): """Handle a plan that contains a SKIP directive.""" - skip_line = Result( - True, None, skip_plan.directive.text, Directive('SKIP')) + skip_line = Result(True, None, skip_plan.directive.text, Directive('SKIP')) self._suite.addTest(Adapter(self._filename, skip_line)) def saw_plan(self, plan, at_line): diff --git a/tests/utils/python/tap/runner.py b/tests/utils/python/tap/runner.py index 6f1a1a66..47da69cd 100644 --- a/tests/utils/python/tap/runner.py +++ b/tests/utils/python/tap/runner.py @@ -28,15 +28,15 @@ class TAPTestResult(TextTestResult): super(TAPTestResult, self).addError(test, err) diagnostics = formatter.format_exception(err) self.tracker.add_not_ok( - self._cls_name(test), self._description(test), - diagnostics=diagnostics) + self._cls_name(test), self._description(test), diagnostics=diagnostics + ) def addFailure(self, test, err): super(TAPTestResult, self).addFailure(test, err) diagnostics = formatter.format_exception(err) self.tracker.add_not_ok( - self._cls_name(test), self._description(test), - diagnostics=diagnostics) + self._cls_name(test), self._description(test), diagnostics=diagnostics + ) def addSuccess(self, test): super(TAPTestResult, self).addSuccess(test) @@ -44,20 +44,23 @@ class TAPTestResult(TextTestResult): def addSkip(self, test, reason): super(TAPTestResult, self).addSkip(test, reason) - self.tracker.add_skip( - self._cls_name(test), self._description(test), reason) + self.tracker.add_skip(self._cls_name(test), self._description(test), reason) def addExpectedFailure(self, test, err): super(TAPTestResult, self).addExpectedFailure(test, err) diagnostics = formatter.format_exception(err) self.tracker.add_not_ok( - self._cls_name(test), self._description(test), - _('(expected failure)'), diagnostics=diagnostics) + self._cls_name(test), + self._description(test), + _('(expected failure)'), + diagnostics=diagnostics, + ) def addUnexpectedSuccess(self, test): super(TAPTestResult, self).addUnexpectedSuccess(test) - self.tracker.add_ok(self._cls_name(test), self._description(test), - _('(unexpected success)')) + self.tracker.add_ok( + self._cls_name(test), self._description(test), _('(unexpected success)') + ) def _cls_name(self, test): return test.__class__.__name__ @@ -67,12 +70,16 @@ class TAPTestResult(TextTestResult): try: return self.FORMAT.format( method_name=str(test), - short_description=test.shortDescription() or '') + short_description=test.shortDescription() or '', + ) except KeyError: - sys.exit(_( - 'Bad format string: {format}\n' - 'Replacement options are: {{short_description}} and ' - '{{method_name}}').format(format=self.FORMAT)) + sys.exit( + _( + 'Bad format string: {format}\n' + 'Replacement options are: {{short_description}} and ' + '{{method_name}}' + ).format(format=self.FORMAT) + ) return test.shortDescription() or str(test) @@ -103,8 +110,7 @@ class TAPTestRunner(TextTestRunner): _tracker.stream = sys.stdout def _makeResult(self): - result = self.resultclass( - self.stream, self.descriptions, self.verbosity) + result = self.resultclass(self.stream, self.descriptions, self.verbosity) result.tracker = _tracker return result diff --git a/tests/utils/python/tap/tracker.py b/tests/utils/python/tap/tracker.py index e9c7e1d4..3f6826e3 100644 --- a/tests/utils/python/tap/tracker.py +++ b/tests/utils/python/tap/tracker.py @@ -13,10 +13,9 @@ from tap.line import Result class Tracker(object): - def __init__( - self, outdir=None, combined=False, streaming=False, stream=None, - header=True): + self, outdir=None, combined=False, streaming=False, stream=None, header=True + ): self.outdir = outdir # Combine all the test results into one file. @@ -66,23 +65,31 @@ class Tracker(object): def add_ok(self, class_name, description, directive=''): result = Result( - ok=True, number=self._get_next_line_number(class_name), - description=description, directive=Directive(directive)) + ok=True, + number=self._get_next_line_number(class_name), + description=description, + directive=Directive(directive), + ) self._add_line(class_name, result) - def add_not_ok( - self, class_name, description, directive='', diagnostics=None): + def add_not_ok(self, class_name, description, directive='', diagnostics=None): result = Result( - ok=False, number=self._get_next_line_number(class_name), - description=description, diagnostics=diagnostics, - directive=Directive(directive)) + ok=False, + number=self._get_next_line_number(class_name), + description=description, + diagnostics=diagnostics, + directive=Directive(directive), + ) self._add_line(class_name, result) def add_skip(self, class_name, description, reason): directive = 'SKIP {0}'.format(reason) result = Result( - ok=True, number=self._get_next_line_number(class_name), - description=description, directive=Directive(directive)) + ok=True, + number=self._get_next_line_number(class_name), + description=description, + directive=Directive(directive), + ) self._add_line(class_name, result) def _add_line(self, class_name, result): @@ -122,9 +129,9 @@ class Tracker(object): with open(combined_file, 'w') as out_file: for test_case in self.combined_test_cases_seen: self.generate_tap_report( - test_case, self._test_cases[test_case], out_file) - print( - '1..{0}'.format(self.combined_line_number), file=out_file) + test_case, self._test_cases[test_case], out_file + ) + print('1..{0}'.format(self.combined_line_number), file=out_file) else: for test_case, tap_lines in self._test_cases.items(): with open(self._get_tap_file_path(test_case), 'w') as out_file: @@ -142,8 +149,9 @@ class Tracker(object): print('1..{0}'.format(len(tap_lines)), file=out_file) def _write_test_case_header(self, test_case, stream): - print(_('# TAP results for {test_case}').format( - test_case=test_case), file=stream) + print( + _('# TAP results for {test_case}').format(test_case=test_case), file=stream + ) def _get_tap_file_path(self, test_case): """Get the TAP output file path for the test case.""" -- 2.34.1