From 0303c2f9577dc2a6593ae9d8b80b2ab0787d673a Mon Sep 17 00:00:00 2001 From: Hrithik Saini <49943287+hrithiksaini99@users.noreply.github.com> Date: Sat, 7 Nov 2026 13:28:54 +0530 Subject: [PATCH] [tabulate] Accept pandas.DataFrame in tabulate() Fixes #16708. tabulate() accepts a pandas.DataFrame at runtime (per the PyPI description) and it iterates as column names (Iterable[Hashable]), but the stub only allowed Mapping[Any, Iterable[Any]] | Iterable[Iterable[Any]], so: tabulate(pd.DataFrame({})) was rejected with arg-type. - Widen tabular_data to Mapping[Any, Iterable[Any]] | Iterable[Any] (Iterable[Any] covers DataFrame's Hashable iteration and runtime acceptance; existing Mapping/rows forms still checked) - METADATA: add pandas-stubs dependency for @tests - New @tests/test_cases/check_tabulate.py with assert_type coverage Verified: mypy_test stubs/tabulate success, regr_test tabulate success, ruff + format clean. --- stubs/tabulate/@tests/test_cases/check_tabulate.py | 14 ++++++++++++++ stubs/tabulate/METADATA.toml | 1 + stubs/tabulate/tabulate/__init__.pyi | 5 ++++- 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 stubs/tabulate/@tests/test_cases/check_tabulate.py diff --git a/stubs/tabulate/@tests/test_cases/check_tabulate.py b/stubs/tabulate/@tests/test_cases/check_tabulate.py new file mode 100644 index 000000000000..2e3a85dce21d --- /dev/null +++ b/stubs/tabulate/@tests/test_cases/check_tabulate.py @@ -0,0 +1,14 @@ +from typing_extensions import assert_type + +import pandas as pd +import tabulate + +# Regression for #16708: tabulate() accepts a pandas.DataFrame at +# runtime (the PyPI description says so), and it iterates as column +# names, not rows. +df = pd.DataFrame({"a": [1, 2], "b": [3, 4]}) +assert_type(tabulate.tabulate(df), str) + +# Existing tabular forms keep working. +assert_type(tabulate.tabulate([["a", "b"], ["c", "d"]]), str) +assert_type(tabulate.tabulate({"a": [1, 2], "b": [3, 4]}), str) diff --git a/stubs/tabulate/METADATA.toml b/stubs/tabulate/METADATA.toml index 7deacccbf1f4..a81256fe5d69 100644 --- a/stubs/tabulate/METADATA.toml +++ b/stubs/tabulate/METADATA.toml @@ -1,2 +1,3 @@ version = "0.10.*" upstream-repository = "https://github.com/astanin/python-tabulate" +dependencies = ["pandas-stubs"] diff --git a/stubs/tabulate/tabulate/__init__.pyi b/stubs/tabulate/tabulate/__init__.pyi index 9a312a37db14..b1308613d332 100644 --- a/stubs/tabulate/tabulate/__init__.pyi +++ b/stubs/tabulate/tabulate/__init__.pyi @@ -44,7 +44,10 @@ multiline_formats: dict[str, str] def simple_separated_format(separator: str) -> TableFormat: ... def tabulate( # The key is converted using str(). - tabular_data: Mapping[Any, Iterable[Any]] | Iterable[Iterable[Any]], + # Iterable[Any] also covers pandas.DataFrame, which tabulate accepts + # at runtime and which iterates as column names (Iterable[Hashable]), + # not as rows; see #16708. + tabular_data: Mapping[Any, Iterable[Any]] | Iterable[Any], headers: str | dict[str, str] | Sequence[str] = (), tablefmt: str | TableFormat = "simple", floatfmt: str | Iterable[str] = "g",