Repository navigation
TYP: enable mypy check_untyped_defs for shap/plots/_force - #4760
Open
SarthakB11 wants to merge 3 commits into
Open
SarthakB11 wants to merge 3 commits into
SarthakB11 wants to merge 3 commits into
Conversation
Add type annotations to all functions and classes in shap/plots/_force.py and remove the module from the mypy check_untyped_defs exclusion list. - Add from __future__ import annotations to resolve the matplotlib parameter/module name conflict in force() and visualize() - Annotate force(), visualize(), save_html(), getjs(), initjs(), id_generator(), ensure_not_numpy(), verify_valid_cmap() - Annotate all Visualizer __init__, html(), _repr_html_(), matplotlib() methods across SimpleListVisualizer, AdditiveForceVisualizer, AdditiveForceArrayVisualizer - Annotate AdditiveExplanation.__init__ with precise legacy types - Add html() stub to BaseVisualizer so save_html() type-checks - Convert filter(lambda...) to generator expressions for mypy compatibility - Use TYPE_CHECKING guard for matplotlib.figure.Figure import (no runtime cost) - Comment out shap.plots._force from pyproject.toml override Closes part of shap#3730
SimpleListVisualizer: annotation was AdditiveExplanation (contradicting the isinstance(e, Explanation) runtime guard and the call site in visualize()). Fix: annotate e as Explanation to match the guard, then cast to AdditiveExplanation internally for attribute access. Remove the now-unnecessary type: ignore on the SimpleListVisualizer(e) call in visualize(). ordering_keys: was silently changed from hasattr(ordering_keys, '__iter__') to 'is not None', which is a behavioral difference for untyped callers passing non-iterable non-None values. Restore the original guard and scope the type: ignore to the list() call only.
This was referenced Apr 23, 2026
Contributor
Author
|
@CloseChoice bumping this, part of the #3730 type-annotation migration series alongside #4445/#4761/#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/plots/_force.pyshap.plots._forcefrom thecheck_untyped_defs = falseoverride inpyproject.tomlmypy shap/plots/_force.pynow passes with zero errorsChanges
shap/plots/_force.pyfrom __future__ import annotationsadded to resolve thematplotlib: boolparameter /matplotlib.figure.Figurereturn-type name conflictFigureimported only underTYPE_CHECKING(no runtime cost)force,visualize,save_html,getjs,initjs,id_generator,ensure_not_numpy,verify_valid_cmapExplanation.__init__andAdditiveExplanation.__init__annotated with precise legacy types (DenseData,Link,Model,Instance)SimpleListVisualizer,AdditiveForceVisualizer,AdditiveForceArrayVisualizer(all__init__,html,_repr_html_,matplotlibmethods)BaseVisualizer.html()stub added sosave_html()type-checks correctlyfilter(lambda ...)patterns converted to generator expressions for mypy compatibilitypyproject.toml"shap.plots._force"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, runtime behaviour, and mypy output.
Part of #3730.