Skip to content

fix: swev-id: matplotlib__matplotlib-24970 prevent Colormap integer warnings#50

Open
casey-brooks wants to merge 1 commit intomatplotlib__matplotlib-24970from
noa/issue-46
Open

fix: swev-id: matplotlib__matplotlib-24970 prevent Colormap integer warnings#50
casey-brooks wants to merge 1 commit intomatplotlib__matplotlib-24970from
noa/issue-46

Conversation

@casey-brooks
Copy link

Summary

  • promote integer inputs in Colormap.__call__ to np.intp before tagging over/under/bad slots so NumPy 1.24 no longer warns
  • add regression coverage for uint8, signed ints, and masked arrays to guard against regressions
  • filter third-party import warnings inside the test module to keep the new test focused on the regression

Reproduction Steps

  1. Install Matplotlib from this branch with NumPy 1.24.x.
  2. Run the snippet:
import matplotlib.pyplot as plt
import numpy as np

plt.get_cmap()(np.empty((0,), dtype=np.uint8))

Observed Failure

/usr/lib/python3.10/site-packages/matplotlib/colors.py:730: DeprecationWarning: NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays.  The conversion of 257 to uint8 will fail in the future.
  xa[xa > self.N - 1] = self._i_over
/usr/lib/python3.10/site-packages/matplotlib/colors.py:731: DeprecationWarning: NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays.  The conversion of 256 to uint8 will fail in the future.
  xa[xa < 0] = self._i_under
/usr/lib/python3.10/site-packages/matplotlib/colors.py:732: DeprecationWarning: NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays.  The conversion of 258 to uint8 will fail in the future.
  xa[mask_bad] = self._i_bad

Fix

  • upcast integer arrays to np.intp immediately after the float-handling branch so over/under/bad indices are always representable
  • new tests assert: (a) uint8 empty input emits no DeprecationWarning, (b) uint8 and int inputs produce identical RGBA output, (c) masked uint8 values map to the bad color, and (d) signed ints respect over/under entries
  • mask out unrelated warnings from setuptools/pyparsing so the regression tests can set DeprecationWarning to error safely

Testing

  • MPLBACKEND=Agg PYTHONPATH=/workspace/matplotlib/lib LD_LIBRARY_PATH=/root/.nix-profile/lib:/nix/store/qipd93x9gjyiygqk673rd2ssnf8y7jj0-gcc-14.3.0-lib/lib:/nix/store/f8w1i7yisixb9hivzbk0l4ixmf67fjqr-gcc-14.3.0-libgcc/lib .venv/bin/pytest lib/matplotlib/tests/test_colormap_integer_inputs.py
  • PYTHONPATH=/workspace/matplotlib/lib .venv/bin/flake8 lib/matplotlib/tests/test_colormap_integer_inputs.py lib/matplotlib/colors.py

Fixes #46

@casey-brooks casey-brooks requested a review from a team December 25, 2025 12:03
@casey-brooks
Copy link
Author

Tests & Lint Summary

  • MPLBACKEND=Agg PYTHONPATH=/workspace/matplotlib/lib LD_LIBRARY_PATH=/root/.nix-profile/lib:/nix/store/qipd93x9gjyiygqk673rd2ssnf8y7jj0-gcc-14.3.0-lib/lib:/nix/store/f8w1i7yisixb9hivzbk0l4ixmf67fjqr-gcc-14.3.0-libgcc/lib .venv/bin/pytest lib/matplotlib/tests/test_colormap_integer_inputs.py (4 passed)
  • PYTHONPATH=/workspace/matplotlib/lib .venv/bin/flake8 lib/matplotlib/tests/test_colormap_integer_inputs.py lib/matplotlib/colors.py (no issues)

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick fix and the thorough regression coverage! I spotted one blocker: the new unconditional promotion of integer arrays to np.intp downcasts wide unsigned dtypes (e.g. uint64), which flips large positive indices to negative before the over/under masking runs. That regresses existing workflows. Please gate the cast on whether the sentinel indices fit in the original dtype (e.g. via np.can_cast).

@casey-brooks
Copy link
Author

Update after reviewer feedback:

  • Conditionally widen integer inputs based on and , promoting only when the existing dtype cannot safely hold the sentinel indices; promotion now targets for consistency.
  • Extended regression coverage () to ensure wide unsigned inputs (uint16/uint32) retain semantics without triggering warnings.

Tests & Lint Summary

  • MPLBACKEND=Agg PYTHONPATH=/workspace/matplotlib/lib LD_LIBRARY_PATH=/root/.nix-profile/lib:/nix/store/qipd93x9gjyiygqk673rd2ssnf8y7jj0-gcc-14.3.0-lib/lib:/nix/store/f8w1i7yisixb9hivzbk0l4ixmf67fjqr-gcc-14.3.0-libgcc/lib .venv/bin/pytest lib/matplotlib/tests/test_colormap_integer_inputs.py (5 passed)
  • PYTHONPATH=/workspace/matplotlib/lib .venv/bin/flake8 lib/matplotlib/tests/test_colormap_integer_inputs.py lib/matplotlib/colors.py (no issues)

@casey-brooks
Copy link
Author

Follow-up details:

  • Conditionally widen integer inputs based on required = np.min_scalar_type(self.N + 2) and np.can_cast(required, xa.dtype, casting="safe").
  • Promotion only happens when needed and uses np.int64.
  • Added test_colormap_wide_unsigned_matches_int to cover uint16/uint32 paths without warnings.

Tests & Lint Summary (re-run)

  • MPLBACKEND=Agg PYTHONPATH=/workspace/matplotlib/lib LD_LIBRARY_PATH=/root/.nix-profile/lib:/nix/store/qipd93x9gjyiygqk673rd2ssnf8y7jj0-gcc-14.3.0-lib/lib:/nix/store/f8w1i7yisixb9hivzbk0l4ixmf67fjqr-gcc-14.3.0-libgcc/lib .venv/bin/pytest lib/matplotlib/tests/test_colormap_integer_inputs.py (5 passed)
  • PYTHONPATH=/workspace/matplotlib/lib .venv/bin/flake8 lib/matplotlib/tests/test_colormap_integer_inputs.py lib/matplotlib/colors.py (no issues)}

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The guard now checks whether the sentinel indices fit in the incoming dtype before widening, so wide unsigned arrays keep their semantics. The new regression test covering uint16/uint32 inputs exercises the scenario we discussed. Thanks for the quick turnaround!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants