Skip to content

Commit f967017

Browse files
Robust test for callable objects
- If the object implements a custom `__getattr__`, or if its `__call__` is itself not callable, you may get misleading results. - Instead use the built-in `callable` function. Not only is it more robust, but it makes the intent clear and the code more readable.
1 parent 74885be commit f967017

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

tabulate/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,7 +1480,7 @@ def _normalize_tabular_data(tabular_data, headers, showindex="default"):
14801480
index = None
14811481
if hasattr(tabular_data, "keys") and hasattr(tabular_data, "values"):
14821482
# dict-like and pandas.DataFrame?
1483-
if hasattr(tabular_data.values, "__call__"):
1483+
if callable(tabular_data.values):
14841484
# likely a conventional dict
14851485
keys = tabular_data.keys()
14861486
try:
@@ -2515,7 +2515,7 @@ def _build_row(padded_cells, colwidths, colaligns, rowfmt):
25152515
"Return a string which represents a row of data cells."
25162516
if not rowfmt:
25172517
return None
2518-
if hasattr(rowfmt, "__call__"):
2518+
if callable(rowfmt):
25192519
return rowfmt(padded_cells, colwidths, colaligns)
25202520
else:
25212521
return _build_simple_row(padded_cells, rowfmt)
@@ -2566,7 +2566,7 @@ def _build_line(colwidths, colaligns, linefmt):
25662566
"Return a string which represents a horizontal line."
25672567
if not linefmt:
25682568
return None
2569-
if hasattr(linefmt, "__call__"):
2569+
if callable(linefmt):
25702570
return linefmt(colwidths, colaligns)
25712571
else:
25722572
begin, fill, sep, end = linefmt

0 commit comments

Comments
 (0)