Repository navigation
TYP: enable mypy check_untyped_defs for shap/maskers/_image - #4761
Open
SarthakB11 wants to merge 1 commit into
Open
SarthakB11 wants to merge 1 commit into
SarthakB11 wants to merge 1 commit into
Conversation
Add type annotations to all functions and classes in shap/maskers/_image.py and remove the module from the mypy check_untyped_defs exclusion list. - Add from __future__ import annotations and numpy.typing (npt) import - Add class-level variable declarations so _blur_value_cache is known as npt.NDArray[Any] | None (not inferred as None from __init__ alone) - Annotate __init__, __call__, inpaint, build_partition_tree, save, load - Add assert self._blur_value_cache is not None before the index access to narrow the type after the lazy-init guard (logically safe: the if-block above always assigns it before this line) - Add # type: ignore[union-attr] for issubclass(type(mask_value), ndarray) guard (mypy cannot narrow on issubclass; isinstance would work but changing the guard is a behavior change) - Add # type: ignore[misc] on @njit decorator (project-wide convention; same suppression used in shap/utils/_clustering.py) - Annotate _jit_build_partition_tree with precise int / npt.NDArray types - Comment out shap.maskers._image from pyproject.toml override Only remaining mypy output is the pre-existing numba njit attr-defined error (present in _clustering.py and _masked_model.py, both already type-checked; disappears in CI where numba is not installed). Closes part of shap#3730
SarthakB11
force-pushed
the
typ/3730-image-masker-type-hints
branch
from
April 22, 2026 22:25
fddde9a to
e59023f
Compare
This was referenced Apr 23, 2026
Contributor
Author
|
@CloseChoice bumping this too, same #3730 series as #4445/#4760/#4772. CI green, no review yet. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
shap/maskers/_image.pyshap.maskers._imagefrom thecheck_untyped_defs = falseoverride inpyproject.tomlmypy shap/maskers/_image.pypasses with zero new errors (only the pre-existingnumba.njitattr-defined error shared with_clustering.pyand_masked_model.py, which disappears in CI where numba is not installed)Changes
shap/maskers/_image.pyfrom __future__ import annotations+numpy.typing as nptadded_blur_value_cacheis typed asnpt.NDArray[Any] | Nonerather than inferred asNone__init__annotated;int(np.prod(...))wrap added (returnsnp.intp, base class expectsint); targeted# type: ignoreforissubclass-based guard (mypy cannot narrow onissubclass, changing toisinstancewould be a behavior change) and fortuple(map(int, ...))returning variable-length tupleassert self._blur_value_cache is not Noneadded before index access — logically safe (theif self.last_xid != id(x)block above always assigns it), narrows the type for mypy__call__annotated withx: Any(acceptstorch.Tensor+ ndarray),mask: npt.NDArray[Any] | bool | None,# type: ignore[override]since base class does not allowNonemaskinpaint,build_partition_tree,save,loadannotated_jit_build_partition_treeannotated with# type: ignore[misc]on@njit(project convention, same as_clustering.py)pyproject.toml"shap.maskers._image"commented out following the existing# NOW TYPE CHECKEDconventionTest
AI Disclosure
Type annotations were drafted with AI assistance (Claude) and verified manually against the source code and mypy output.
Part of #3730.