Skip to content

Commit

Permalink
Deprecate passing non-int to Table.auto_set_column_width
Browse files Browse the repository at this point in the history
When iterables were added in matplotlib#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.
  • Loading branch information
QuLogic committed Aug 3, 2023
1 parent 54f1612 commit bd29eb7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
5 changes: 5 additions & 0 deletions doc/api/next_api_changes/deprecations/26444-ES.rst
@@ -0,0 +1,5 @@
Passing non-int or sequence of non-int to ``Table.auto_set_column_width``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Column numbers are ints, and formerly passing any other type was effectively
ignored. This will become an error in the future.
19 changes: 11 additions & 8 deletions lib/matplotlib/table.py
Expand Up @@ -24,6 +24,8 @@
Thanks to John Gill for providing the class and table.
"""

import numpy as np

from . import _api, _docstring
from .artist import Artist, allow_rasterization
from .patches import Rectangle
Expand Down Expand Up @@ -494,14 +496,15 @@ def auto_set_column_width(self, col):
col : int or sequence of ints
The indices of the columns to auto-scale.
"""
# check for col possibility on iteration
try:
iter(col)
except (TypeError, AttributeError):
self._autoColumns.append(col)
else:
for cell in col:
self._autoColumns.append(cell)
col1d = np.atleast_1d(col)
if not np.issubdtype(col1d.dtype, np.integer):
_api.warn_deprecated("3.8", name="col",
message="%(name)r must be an int or sequence of ints. "
"Passing other types is deprecated since %(since)s "
"and will be removed %(removal)s.")
return
for cell in col1d:
self._autoColumns.append(cell)

self.stale = True

Expand Down
15 changes: 11 additions & 4 deletions lib/matplotlib/tests/test_table.py
@@ -1,9 +1,11 @@
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.testing.decorators import image_comparison, check_figures_equal
import pytest

from matplotlib.table import CustomCell, Table
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.path import Path
from matplotlib.table import CustomCell, Table
from matplotlib.testing.decorators import image_comparison, check_figures_equal
from matplotlib.transforms import Bbox


Expand Down Expand Up @@ -176,7 +178,12 @@ def test_auto_column():
loc="center")
tb4.auto_set_font_size(False)
tb4.set_fontsize(12)
tb4.auto_set_column_width("-101")
with pytest.warns(mpl.MatplotlibDeprecationWarning,
match="'col' must be an int or sequence of ints"):
tb4.auto_set_column_width("-101") # type: ignore [arg-type]
with pytest.warns(mpl.MatplotlibDeprecationWarning,
match="'col' must be an int or sequence of ints"):
tb4.auto_set_column_width(["-101"]) # type: ignore [list-item]


def test_table_cells():
Expand Down

0 comments on commit bd29eb7

Please sign in to comment.