From ca3a83d1c922bb2b7be2eab302ffa26061793ad6 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Thu, 5 Mar 2026 10:56:30 +0100 Subject: [PATCH 1/5] Apply ruff/flake8-comprehensions C416 Unnecessary list comprehension (rewrite using `list()`) --- tabulate/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tabulate/__init__.py b/tabulate/__init__.py index b0810a9..090cfce 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -2381,7 +2381,7 @@ def tabulate( assert isinstance(colalign, Iterable) if isinstance(colalign, str): warnings.warn( - f"As a string, `colalign` is interpreted as {[c for c in colalign]}. " + f"As a string, `colalign` is interpreted as {list(colalign)}. " f'Did you mean `colglobalalign = "{colalign}"` or `colalign = ("{colalign}",)`?', stacklevel=2, ) @@ -2423,7 +2423,7 @@ def tabulate( assert isinstance(headersalign, Iterable) if isinstance(headersalign, str): warnings.warn( - f"As a string, `headersalign` is interpreted as {[c for c in headersalign]}. " + f"As a string, `headersalign` is interpreted as {list(headersalign)}. " f'Did you mean `headersglobalalign = "{headersalign}"` ' f'or `headersalign = ("{headersalign}",)`?', stacklevel=2, @@ -2702,7 +2702,7 @@ def _update_lines(self, lines, new_line): as add any colors from previous lines order to preserve the same formatting as a single unwrapped string. """ - code_matches = [x for x in _ansi_codes.finditer(new_line)] + code_matches = list(_ansi_codes.finditer(new_line)) color_codes = [code.string[code.span()[0] : code.span()[1]] for code in code_matches] # Add color codes from earlier in the unwrapped line, and then track any new ones we add. From 8880764aba6ae5f1c5c59e206f8141af44548870 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Thu, 5 Mar 2026 10:41:07 +0100 Subject: [PATCH 2/5] Apply ruff/flake8-comprehensions rule C417 Unnecessary `map()` usage (rewrite using a list comprehension) --- benchmark/benchmark.py | 2 +- tabulate/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/benchmark/benchmark.py b/benchmark/benchmark.py index 2fd1e3a..d405f5c 100644 --- a/benchmark/benchmark.py +++ b/benchmark/benchmark.py @@ -75,7 +75,7 @@ def benchmark(n): methods = [m for m in methods if m[0].startswith("tabulate")] results = [(desc, timeit(code, setup_code, number=n) / n * 1e6) for desc, code in methods] - mintime = min(map(lambda x: x[1], results)) + mintime = min(x[1] for x in results) results = [(desc, t, t / mintime) for desc, t in sorted(results, key=lambda x: x[1])] table = tabulate.tabulate( results, ["Table formatter", "time, μs", "rel. time"], "rst", floatfmt=".1f" diff --git a/tabulate/__init__.py b/tabulate/__init__.py index 090cfce..38e1708 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -1592,7 +1592,7 @@ def _normalize_tabular_data(tabular_data, headers, showindex="default"): headers = list(map(str, headers)) # rows = list(map(list, rows)) - rows = list(map(lambda r: r if _is_separating_line(r) else list(r), rows)) + rows = [r if _is_separating_line(r) else list(r) for r in rows] # add or remove an index column showindex_is_a_str = type(showindex) in [str, bytes] From 10e17373d5fe1e47a159a9e3c1489944b9dfcba7 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Thu, 5 Mar 2026 10:41:53 +0100 Subject: [PATCH 3/5] Apply ruff/flake8-comprehensions rule C419 Unnecessary list comprehension --- test/common.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/common.py b/test/common.py index b557b85..5f00852 100644 --- a/test/common.py +++ b/test/common.py @@ -41,6 +41,6 @@ def check_warnings(func_args_kwargs, *, num=None, category=None, contain=None): if num is not None: assert len(W) == num if category is not None: - assert all([issubclass(w.category, category) for w in W]) + assert all(issubclass(w.category, category) for w in W) if contain is not None: - assert all([contain in str(w.message) for w in W]) + assert all(contain in str(w.message) for w in W) From 1f02978d54b61b4fb93e7850f3fc50b40f7aae0e Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Thu, 5 Mar 2026 10:57:48 +0100 Subject: [PATCH 4/5] Apply ruff/flake8-comprehensions rule C420 Unnecessary dict comprehension for iterable; use `dict.fromkeys` instead --- test/test_regression.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_regression.py b/test/test_regression.py index c23d34d..6172180 100644 --- a/test/test_regression.py +++ b/test/test_regression.py @@ -171,7 +171,7 @@ def test_numeric_column_headers(): expected = " 42\n----\n 1\n 2" assert_equal(expected, result) - lod = [{p: i for p in range(5)} for i in range(5)] + lod = [dict.fromkeys(range(5), i) for i in range(5)] result = tabulate(lod, "keys") expected = "\n".join( [ From d147f70705b0d4dccb9eb17676e64933679900c0 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Thu, 5 Mar 2026 10:45:28 +0100 Subject: [PATCH 5/5] Enforce ruff/flake8-comprehensions rules (C4) --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 2a80daa..11397bb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -53,7 +53,7 @@ line-length = 99 exclude = ["tabulate/_version.py"] [tool.ruff.lint] -extend-select = ["W", "ISC", "I", "C90"] +extend-select = ["W", "C4", "ISC", "I", "C90"] ignore = ["E721", "C901"] [tool.ruff.lint.mccabe]