Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bd29eb7

Browse files
committed
Deprecate passing non-int to Table.auto_set_column_width
When iterables were added in #6047, the test added a string. Typing correctly points out that that is not accepted, and in fact it does not do anything (as shown in the test image) because column keys are ints, not strings.
1 parent 54f1612 commit bd29eb7

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Passing non-int or sequence of non-int to ``Table.auto_set_column_width``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Column numbers are ints, and formerly passing any other type was effectively
5+
ignored. This will become an error in the future.

lib/matplotlib/table.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
Thanks to John Gill for providing the class and table.
2525
"""
2626

27+
import numpy as np
28+
2729
from . import _api, _docstring
2830
from .artist import Artist, allow_rasterization
2931
from .patches import Rectangle
@@ -494,14 +496,15 @@ def auto_set_column_width(self, col):
494496
col : int or sequence of ints
495497
The indices of the columns to auto-scale.
496498
"""
497-
# check for col possibility on iteration
498-
try:
499-
iter(col)
500-
except (TypeError, AttributeError):
501-
self._autoColumns.append(col)
502-
else:
503-
for cell in col:
504-
self._autoColumns.append(cell)
499+
col1d = np.atleast_1d(col)
500+
if not np.issubdtype(col1d.dtype, np.integer):
501+
_api.warn_deprecated("3.8", name="col",
502+
message="%(name)r must be an int or sequence of ints. "
503+
"Passing other types is deprecated since %(since)s "
504+
"and will be removed %(removal)s.")
505+
return
506+
for cell in col1d:
507+
self._autoColumns.append(cell)
505508

506509
self.stale = True
507510

lib/matplotlib/tests/test_table.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import matplotlib.pyplot as plt
21
import numpy as np
3-
from matplotlib.testing.decorators import image_comparison, check_figures_equal
2+
import pytest
43

5-
from matplotlib.table import CustomCell, Table
4+
import matplotlib.pyplot as plt
5+
import matplotlib as mpl
66
from matplotlib.path import Path
7+
from matplotlib.table import CustomCell, Table
8+
from matplotlib.testing.decorators import image_comparison, check_figures_equal
79
from matplotlib.transforms import Bbox
810

911

@@ -176,7 +178,12 @@ def test_auto_column():
176178
loc="center")
177179
tb4.auto_set_font_size(False)
178180
tb4.set_fontsize(12)
179-
tb4.auto_set_column_width("-101")
181+
with pytest.warns(mpl.MatplotlibDeprecationWarning,
182+
match="'col' must be an int or sequence of ints"):
183+
tb4.auto_set_column_width("-101") # type: ignore [arg-type]
184+
with pytest.warns(mpl.MatplotlibDeprecationWarning,
185+
match="'col' must be an int or sequence of ints"):
186+
tb4.auto_set_column_width(["-101"]) # type: ignore [list-item]
180187

181188

182189
def test_table_cells():

0 commit comments

Comments
 (0)