diff --git a/.ycm_extra_conf.py b/.ycm_extra_conf.py index d9ea1fb3b7..caf257f054 100644 --- a/.ycm_extra_conf.py +++ b/.ycm_extra_conf.py @@ -92,7 +92,9 @@ def GetCompilationInfoForFile(filename): for extension in SOURCE_EXTENSIONS: replacement_file = basename + extension if os.path.exists(replacement_file): - compilation_info = database.GetCompilationInfoForFile(replacement_file) + compilation_info = database.GetCompilationInfoForFile( + replacement_file + ) if compilation_info.compiler_flags_: return compilation_info return None @@ -108,7 +110,8 @@ def FlagsForFile(filename, **kwargs): return None final_flags = MakeRelativePathsInFlagsAbsolute( - compilation_info.compiler_flags_, compilation_info.compiler_working_dir_ + compilation_info.compiler_flags_, + compilation_info.compiler_working_dir_, ) else: relative_to = DirectoryOfThisScript() diff --git a/bindings/python/google_benchmark/example.py b/bindings/python/google_benchmark/example.py index 1efdfe1ec2..b5b2f88ff3 100644 --- a/bindings/python/google_benchmark/example.py +++ b/bindings/python/google_benchmark/example.py @@ -86,7 +86,9 @@ def custom_counters(state): # Set a counter as a rate. state.counters["foo_rate"] = Counter(num_foo, Counter.kIsRate) # Set a counter as an inverse of rate. - state.counters["foo_inv_rate"] = Counter(num_foo, Counter.kIsRate | Counter.kInvert) + state.counters["foo_inv_rate"] = Counter( + num_foo, Counter.kIsRate | Counter.kInvert + ) # Set a counter as a thread-average quantity. state.counters["foo_avg"] = Counter(num_foo, Counter.kAvgThreads) # There's also a combined flag: diff --git a/setup.py b/setup.py index daa708f337..f4700a025a 100644 --- a/setup.py +++ b/setup.py @@ -4,6 +4,7 @@ import shutil import sysconfig from pathlib import Path +from typing import Generator import setuptools from setuptools.command import build_ext @@ -15,7 +16,7 @@ @contextlib.contextmanager -def temp_fill_include_path(fp: str): +def temp_fill_include_path(fp: str) -> Generator[None, None, None]: """Temporarily set the Python include path in a file.""" with open(fp, "r+") as f: try: @@ -56,7 +57,7 @@ def run(self): # explicitly call `bazel shutdown` for graceful exit self.spawn(["bazel", "shutdown"]) - def bazel_build(self, ext: BazelExtension): + def bazel_build(self, ext: BazelExtension) -> None: """Runs the bazel build to create the package.""" with temp_fill_include_path("WORKSPACE"): temp_path = Path(self.build_temp) @@ -94,7 +95,9 @@ def bazel_build(self, ext: BazelExtension): shared_lib_suffix = ".dll" if IS_WINDOWS else ".so" ext_name = ext.target_name + shared_lib_suffix - ext_bazel_bin_path = temp_path / "bazel-bin" / ext.relpath / ext_name + ext_bazel_bin_path = ( + temp_path / "bazel-bin" / ext.relpath / ext_name + ) ext_dest_path = Path(self.get_ext_fullpath(ext.name)) shutil.copyfile(ext_bazel_bin_path, ext_dest_path) diff --git a/tools/compare.py b/tools/compare.py index 0054094a01..3cc9e5eb4a 100755 --- a/tools/compare.py +++ b/tools/compare.py @@ -1,4 +1,7 @@ #!/usr/bin/env python3 + +# type: ignore + """ compare.py - versatile benchmark output compare tool """ @@ -39,7 +42,8 @@ def check_inputs(in1, in2, flags): # --benchmark_filter= for flag in util.remove_benchmark_flags("--benchmark_filter=", flags): print( - "WARNING: passing %s has no effect since both " "inputs are JSON" % flag + "WARNING: passing %s has no effect since both " + "inputs are JSON" % flag ) if output_type is not None and output_type != "json": print( @@ -53,7 +57,9 @@ def check_inputs(in1, in2, flags): def create_parser(): - parser = ArgumentParser(description="versatile benchmark output compare tool") + parser = ArgumentParser( + description="versatile benchmark output compare tool" + ) parser.add_argument( "-a", @@ -299,7 +305,9 @@ def main(): # Now, filter the benchmarks so that the difference report can work if filter_baseline and filter_contender: replacement = "[%s vs. %s]" % (filter_baseline, filter_contender) - json1 = gbench.report.filter_benchmark(json1_orig, filter_baseline, replacement) + json1 = gbench.report.filter_benchmark( + json1_orig, filter_baseline, replacement + ) json2 = gbench.report.filter_benchmark( json2_orig, filter_contender, replacement ) @@ -428,7 +436,9 @@ def test_filters_basic(self): self.assertFalse(parsed.benchmark_options) def test_filters_with_remainder(self): - parsed = self.parser.parse_args(["filters", self.testInput0, "c", "d", "e"]) + parsed = self.parser.parse_args( + ["filters", self.testInput0, "c", "d", "e"] + ) self.assertFalse(parsed.display_aggregates_only) self.assertTrue(parsed.utest) self.assertEqual(parsed.mode, "filters") @@ -464,7 +474,14 @@ def test_benchmarksfiltered_basic(self): def test_benchmarksfiltered_with_remainder(self): parsed = self.parser.parse_args( - ["benchmarksfiltered", self.testInput0, "c", self.testInput1, "e", "f"] + [ + "benchmarksfiltered", + self.testInput0, + "c", + self.testInput1, + "e", + "f", + ] ) self.assertFalse(parsed.display_aggregates_only) self.assertTrue(parsed.utest) diff --git a/tools/gbench/report.py b/tools/gbench/report.py index 75136c354c..10e6b508f0 100644 --- a/tools/gbench/report.py +++ b/tools/gbench/report.py @@ -1,4 +1,7 @@ -"""report.py - Utilities for reporting statistics about benchmark results +# type: ignore + +""" +report.py - Utilities for reporting statistics about benchmark results """ import copy @@ -58,7 +61,10 @@ def color_format(use_color, fmt_str, *args, **kwargs): """ assert use_color is True or use_color is False if not use_color: - args = [arg if not isinstance(arg, BenchmarkColor) else BC_NONE for arg in args] + args = [ + arg if not isinstance(arg, BenchmarkColor) else BC_NONE + for arg in args + ] kwargs = { key: arg if not isinstance(arg, BenchmarkColor) else BC_NONE for key, arg in kwargs.items() @@ -293,8 +299,12 @@ def get_difference_report(json1, json2, utest=False): "cpu_time": bn["cpu_time"], "real_time_other": other_bench["real_time"], "cpu_time_other": other_bench["cpu_time"], - "time": calculate_change(bn["real_time"], other_bench["real_time"]), - "cpu": calculate_change(bn["cpu_time"], other_bench["cpu_time"]), + "time": calculate_change( + bn["real_time"], other_bench["real_time"] + ), + "cpu": calculate_change( + bn["cpu_time"], other_bench["cpu_time"] + ), } ) @@ -320,11 +330,14 @@ def get_difference_report(json1, json2, utest=False): # benchmark suite. if measurements: run_type = ( - partition[0][0]["run_type"] if "run_type" in partition[0][0] else "" + partition[0][0]["run_type"] + if "run_type" in partition[0][0] + else "" ) aggregate_name = ( partition[0][0]["aggregate_name"] - if run_type == "aggregate" and "aggregate_name" in partition[0][0] + if run_type == "aggregate" + and "aggregate_name" in partition[0][0] else "" ) diff_report.append( @@ -447,7 +460,9 @@ class TestGetUniqueBenchmarkNames(unittest.TestCase): def load_results(self): import json - testInputs = os.path.join(os.path.dirname(os.path.realpath(__file__)), "Inputs") + testInputs = os.path.join( + os.path.dirname(os.path.realpath(__file__)), "Inputs" + ) testOutput = os.path.join(testInputs, "test3_run0.json") with open(testOutput, "r") as f: json = json.load(f) @@ -494,13 +509,69 @@ def test_json_diff_report_pretty_printing(self): ["BM_SameTimes", "+0.0000", "+0.0000", "10", "10", "10", "10"], ["BM_2xFaster", "-0.5000", "-0.5000", "50", "25", "50", "25"], ["BM_2xSlower", "+1.0000", "+1.0000", "50", "100", "50", "100"], - ["BM_1PercentFaster", "-0.0100", "-0.0100", "100", "99", "100", "99"], - ["BM_1PercentSlower", "+0.0100", "+0.0100", "100", "101", "100", "101"], - ["BM_10PercentFaster", "-0.1000", "-0.1000", "100", "90", "100", "90"], - ["BM_10PercentSlower", "+0.1000", "+0.1000", "100", "110", "100", "110"], - ["BM_100xSlower", "+99.0000", "+99.0000", "100", "10000", "100", "10000"], - ["BM_100xFaster", "-0.9900", "-0.9900", "10000", "100", "10000", "100"], - ["BM_10PercentCPUToTime", "+0.1000", "-0.1000", "100", "110", "100", "90"], + [ + "BM_1PercentFaster", + "-0.0100", + "-0.0100", + "100", + "99", + "100", + "99", + ], + [ + "BM_1PercentSlower", + "+0.0100", + "+0.0100", + "100", + "101", + "100", + "101", + ], + [ + "BM_10PercentFaster", + "-0.1000", + "-0.1000", + "100", + "90", + "100", + "90", + ], + [ + "BM_10PercentSlower", + "+0.1000", + "+0.1000", + "100", + "110", + "100", + "110", + ], + [ + "BM_100xSlower", + "+99.0000", + "+99.0000", + "100", + "10000", + "100", + "10000", + ], + [ + "BM_100xFaster", + "-0.9900", + "-0.9900", + "10000", + "100", + "10000", + "100", + ], + [ + "BM_10PercentCPUToTime", + "+0.1000", + "-0.1000", + "100", + "110", + "100", + "90", + ], ["BM_ThirdFaster", "-0.3333", "-0.3334", "100", "67", "100", "67"], ["BM_NotBadTimeUnit", "-0.9000", "+0.2000", "0", "0", "0", "1"], ["BM_hasLabel", "+0.0000", "+0.0000", "1", "1", "1", "1"], @@ -1126,7 +1197,9 @@ def test_json_diff_report(self): assert_measurements(self, out, expected) -class TestReportDifferenceWithUTestWhileDisplayingAggregatesOnly(unittest.TestCase): +class TestReportDifferenceWithUTestWhileDisplayingAggregatesOnly( + unittest.TestCase +): @classmethod def setUpClass(cls): def load_results(): @@ -1409,7 +1482,9 @@ def test_json_diff_report_pretty_printing(self): for n in range(len(self.json["benchmarks"]) ** 2): random.shuffle(self.json["benchmarks"]) - sorted_benchmarks = util.sort_benchmark_results(self.json)["benchmarks"] + sorted_benchmarks = util.sort_benchmark_results(self.json)[ + "benchmarks" + ] self.assertEqual(len(expected_names), len(sorted_benchmarks)) for out, expected in zip(sorted_benchmarks, expected_names): self.assertEqual(out["name"], expected) diff --git a/tools/gbench/util.py b/tools/gbench/util.py index bf70850295..84747d1053 100644 --- a/tools/gbench/util.py +++ b/tools/gbench/util.py @@ -72,7 +72,8 @@ def classify_input_file(filename): ftype = IT_JSON else: err_msg = ( - "'%s' does not name a valid benchmark executable or JSON file" % filename + "'%s' does not name a valid benchmark executable or JSON file" + % filename ) return ftype, err_msg @@ -189,7 +190,9 @@ def run_benchmark(exe_name, benchmark_flags): is_temp_output = True thandle, output_name = tempfile.mkstemp() os.close(thandle) - benchmark_flags = list(benchmark_flags) + ["--benchmark_out=%s" % output_name] + benchmark_flags = list(benchmark_flags) + [ + "--benchmark_out=%s" % output_name + ] cmd = [exe_name] + benchmark_flags print("RUNNING: %s" % " ".join(cmd)) @@ -212,7 +215,9 @@ def run_or_load_benchmark(filename, benchmark_flags): """ ftype = check_input_file(filename) if ftype == IT_JSON: - benchmark_filter = find_benchmark_flag("--benchmark_filter=", benchmark_flags) + benchmark_filter = find_benchmark_flag( + "--benchmark_filter=", benchmark_flags + ) return load_benchmark_results(filename, benchmark_filter) if ftype == IT_Executable: return run_benchmark(filename, benchmark_flags) diff --git a/tools/strip_asm.py b/tools/strip_asm.py index 986a8b936f..bc3a774a79 100755 --- a/tools/strip_asm.py +++ b/tools/strip_asm.py @@ -12,7 +12,7 @@ def find_used_labels(asm): found = set() - label_re = re.compile("\s*j[a-z]+\s+\.L([a-zA-Z0-9][a-zA-Z0-9_]*)") + label_re = re.compile(r"\s*j[a-z]+\s+\.L([a-zA-Z0-9][a-zA-Z0-9_]*)") for line in asm.splitlines(): m = label_re.match(line) if m: @@ -33,7 +33,7 @@ def normalize_labels(asm): if not needs_dot: return asm for ld in decls: - asm = re.sub("(^|\s+)" + ld + "(?=:|\s)", "\\1." + ld, asm) + asm = re.sub(r"(^|\s+)" + ld + r"(?=:|\s)", "\\1." + ld, asm) return asm @@ -41,7 +41,7 @@ def transform_labels(asm): asm = normalize_labels(asm) used_decls = find_used_labels(asm) new_asm = "" - label_decl = re.compile("^\.L([a-zA-Z0-9][a-zA-Z0-9_]*)(?=:)") + label_decl = re.compile(r"^\.L([a-zA-Z0-9][a-zA-Z0-9_]*)(?=:)") for line in asm.splitlines(): m = label_decl.match(line) if not m or m.group(0) in used_decls: @@ -77,7 +77,10 @@ def process_identifiers(line): if tk.startswith("__Z"): tk = tk[1:] elif ( - tk.startswith("_") and len(tk) > 1 and tk[1].isalpha() and tk[1] != "Z" + tk.startswith("_") + and len(tk) > 1 + and tk[1].isalpha() + and tk[1] != "Z" ): tk = tk[1:] new_line += tk @@ -93,15 +96,17 @@ def process_asm(asm): # TODO: Add more things we want to remove discard_regexes = [ - re.compile("\s+\..*$"), # directive - re.compile("\s*#(NO_APP|APP)$"), # inline ASM - re.compile("\s*#.*$"), # comment line - re.compile("\s*\.globa?l\s*([.a-zA-Z_][a-zA-Z0-9$_.]*)"), # global directive + re.compile(r"\s+\..*$"), # directive + re.compile(r"\s*#(NO_APP|APP)$"), # inline ASM + re.compile(r"\s*#.*$"), # comment line re.compile( - "\s*\.(string|asciz|ascii|[1248]?byte|short|word|long|quad|value|zero)" + r"\s*\.globa?l\s*([.a-zA-Z_][a-zA-Z0-9$_.]*)" + ), # global directive + re.compile( + r"\s*\.(string|asciz|ascii|[1248]?byte|short|word|long|quad|value|zero)" ), ] - keep_regexes = [] + keep_regexes: list[re.Pattern] = [] fn_label_def = re.compile("^[a-zA-Z_][a-zA-Z0-9_.]*:") for line in asm.splitlines(): # Remove Mach-O attribute @@ -127,7 +132,11 @@ def process_asm(asm): def main(): parser = ArgumentParser(description="generate a stripped assembly file") parser.add_argument( - "input", metavar="input", type=str, nargs=1, help="An input assembly file" + "input", + metavar="input", + type=str, + nargs=1, + help="An input assembly file", ) parser.add_argument( "out", metavar="output", type=str, nargs=1, help="The output file"