From bd29eb799dd0fdea6c181465b65fad94a03915c3 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Thu, 3 Aug 2023 03:25:15 -0400 Subject: [PATCH 1/4] 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. --- .../deprecations/26444-ES.rst | 5 +++++ lib/matplotlib/table.py | 19 +++++++++++-------- lib/matplotlib/tests/test_table.py | 15 +++++++++++---- 3 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 doc/api/next_api_changes/deprecations/26444-ES.rst diff --git a/doc/api/next_api_changes/deprecations/26444-ES.rst b/doc/api/next_api_changes/deprecations/26444-ES.rst new file mode 100644 index 000000000000..6e5b48532f2a --- /dev/null +++ b/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. diff --git a/lib/matplotlib/table.py b/lib/matplotlib/table.py index 403c64fdc710..d42cdf878d61 100644 --- a/lib/matplotlib/table.py +++ b/lib/matplotlib/table.py @@ -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 @@ -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 diff --git a/lib/matplotlib/tests/test_table.py b/lib/matplotlib/tests/test_table.py index 9b2cb96ea037..328fd020c1ee 100644 --- a/lib/matplotlib/tests/test_table.py +++ b/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 @@ -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(): From f6793ba4f135a27fd6d03855bfa7b57625dc981f Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Thu, 3 Aug 2023 03:28:46 -0400 Subject: [PATCH 2/4] Remove Fonts.get_used_characters This is never called, is on an internal class, and the attribute it accesses doesn't exist as it was removed in #22204. --- lib/matplotlib/_mathtext.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/lib/matplotlib/_mathtext.py b/lib/matplotlib/_mathtext.py index ed80dd3944a4..25a825b7b0d9 100644 --- a/lib/matplotlib/_mathtext.py +++ b/lib/matplotlib/_mathtext.py @@ -243,14 +243,6 @@ def get_underline_thickness(self, font, fontsize, dpi): """ raise NotImplementedError() - def get_used_characters(self): - """ - Get the set of characters that were used in the math - expression. Used by backends that need to subset fonts so - they know which glyphs to include. - """ - return self.used_characters - def get_sized_alternatives_for_symbol(self, fontname, sym): """ Override if your font provides multiple sizes of the same From f900a842f60cb664d1027f07448fe68625147f56 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Thu, 3 Aug 2023 16:02:04 -0400 Subject: [PATCH 3/4] TST: Fix monkey patch of find_tex_file The normal function returns a `str`, so this patch returning bytes is confusing. --- lib/matplotlib/tests/test_dviread.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/matplotlib/tests/test_dviread.py b/lib/matplotlib/tests/test_dviread.py index a40151fd555f..7b7ff151be18 100644 --- a/lib/matplotlib/tests/test_dviread.py +++ b/lib/matplotlib/tests/test_dviread.py @@ -7,7 +7,7 @@ def test_PsfontsMap(monkeypatch): - monkeypatch.setattr(dr, 'find_tex_file', lambda x: x) + monkeypatch.setattr(dr, 'find_tex_file', lambda x: x.decode()) filename = str(Path(__file__).parent / 'baseline_images/dviread/test.map') fontmap = dr.PsfontsMap(filename) @@ -18,15 +18,15 @@ def test_PsfontsMap(monkeypatch): assert entry.texname == key assert entry.psname == b'PSfont%d' % n if n not in [3, 5]: - assert entry.encoding == b'font%d.enc' % n + assert entry.encoding == 'font%d.enc' % n elif n == 3: - assert entry.encoding == b'enc3.foo' + assert entry.encoding == 'enc3.foo' # We don't care about the encoding of TeXfont5, which specifies # multiple encodings. if n not in [1, 5]: - assert entry.filename == b'font%d.pfa' % n + assert entry.filename == 'font%d.pfa' % n else: - assert entry.filename == b'font%d.pfb' % n + assert entry.filename == 'font%d.pfb' % n if n == 4: assert entry.effects == {'slant': -0.1, 'extend': 1.2} else: @@ -37,13 +37,13 @@ def test_PsfontsMap(monkeypatch): assert entry.encoding is None entry = fontmap[b'TeXfont7'] assert entry.filename is None - assert entry.encoding == b'font7.enc' + assert entry.encoding == 'font7.enc' entry = fontmap[b'TeXfont8'] - assert entry.filename == b'font8.pfb' + assert entry.filename == 'font8.pfb' assert entry.encoding is None entry = fontmap[b'TeXfont9'] assert entry.psname == b'TeXfont9' - assert entry.filename == b'/absolute/font9.pfb' + assert entry.filename == '/absolute/font9.pfb' # First of duplicates only. entry = fontmap[b'TeXfontA'] assert entry.psname == b'PSfontA1' From 960efc32253d01a69defee8ff6d636a9b4b49454 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Thu, 3 Aug 2023 17:10:50 -0400 Subject: [PATCH 4/4] TST: Remove dead code for LogFormatterSciNotation test `LogFormatterSciNotation.sublabel` is not an attribute that exists. There is the private `LogFormatterSciNotation._sublabels`, but this obviously doesn't need to be set as the test is already passing. --- lib/matplotlib/tests/test_ticker.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index 5f3736f2e87e..32def5d9cbc9 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -971,7 +971,6 @@ class TestLogFormatterSciNotation: @pytest.mark.parametrize('base, value, expected', test_data) def test_basic(self, base, value, expected): formatter = mticker.LogFormatterSciNotation(base=base) - formatter.sublabel = {1, 2, 5, 1.2} with mpl.rc_context({'text.usetex': False}): assert formatter(value) == expected