Skip to content

FIX: restore the previous style when a style_context block raises - #5184

Open
kratos0718 wants to merge 13 commits into
shap:masterfrom
kratos0718:style-context-restore-on-error
Open

kratos0718 wants to merge 13 commits into
shap:masterfrom
kratos0718:style-context-restore-on-error

Conversation

@kratos0718

Copy link
Copy Markdown

Overview

Description of the changes proposed in this pull request:

style_context restored the previous style after a bare yield, so if anything inside the block raises, for example a plot call that fails, the temporary style stays applied to every plot made afterwards:

from shap.plots import _style

_style.get_style().text_color          # 'white'
try:
    with _style.style_context(text_color="black"):
        raise ValueError("plot failed")
except ValueError:
    pass
_style.get_style().text_color          # 'black', should be 'white'

This moves the restore into a finally. The same leak can carry a style from one plot test into later tests, since test_waterfall.py uses style_context too.

Checklist

  • All pre-commit checks pass.
  • Unit tests added (if fixing a bug or adding a new feature)

CloseChoice and others added 13 commits September 7, 2026 23:54
updates:
- [github.com/astral-sh/ruff-pre-commit: v0.16.1 → v0.16.6](astral-sh/ruff-pre-commit@v0.16.1...v0.16.6)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Bumps [soupsieve](https://github.com/facelessuser/soupsieve) from 2.8.4 to 2.9.
- [Release notes](https://github.com/facelessuser/soupsieve/releases)
- [Commits](facelessuser/soupsieve@2.8.4...2.9)

---
updated-dependencies:
- dependency-name: soupsieve
  dependency-version: '2.9'
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
…5160)

Bumps [js-yaml](https://github.com/nodeca/js-yaml) from 3.15.0 to 3.15.2.
- [Changelog](https://github.com/nodeca/js-yaml/blob/3.15.2/CHANGELOG.md)
- [Commits](nodeca/js-yaml@3.15.0...3.15.2)

---
updated-dependencies:
- dependency-name: js-yaml
  dependency-version: 3.15.2
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Tobias Pitters <[email protected]>
Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from 4 to 7.
- [Release notes](https://github.com/codecov/codecov-action/releases)
- [Changelog](https://github.com/codecov/codecov-action/blob/main/CHANGELOG.md)
- [Commits](codecov/codecov-action@v4...v7)

---
updated-dependencies:
- dependency-name: codecov/codecov-action
  dependency-version: '7'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
* ENH: Add Pyodide/JupyterLite support via PyEmscripten wheel (PEP 783)

Signed-off-by: Mohit Yadav <[email protected]>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: add isinstance checks to resolve mypy union-attr error in test_pyodide

Signed-off-by: Mohit Yadav <[email protected]>

* fix unnecessary pr things

* add run pyodide tests

* build matplotlib from source so that tests pass

Co-authored-by: Codex <[email protected]>

---------

Signed-off-by: Mohit Yadav <[email protected]>
Co-authored-by: Mohit Yadav <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Codex <[email protected]>
…y name (shap#5164)

* FIX: match KernelExplainer DataFrame columns to the background by name

shap_values read DataFrame input by position, so reordered columns scored the model on the wrong features and attributed the result to the wrong names. A DataFrame whose columns reorder the background features is now explained in background order and the values are mapped back to its own column order. A DataFrame missing some background features raises a ValueError instead of an IndexError.

* FIX: take the column order from the background, and select by position

A background DataFrame carries its own column order, and feature_names passed next to it can list the same names in another order; the background order now wins, so input that already matches it is left alone. The reordered frame is selected by position rather than by label, which keeps labels pandas reads as a row mask out of the recursion, and input narrower than the background raises the missing feature error even when no name matches.

* simplify kernelexplainer reordering

---------

Co-authored-by: Tobias Pitters <[email protected]>
…ith 2 updates (shap#5156)

Bumps the github-actions-minor-patch group with 2 updates in the / directory: [pypa/cibuildwheel](https://github.com/pypa/cibuildwheel) and [scientific-python/upload-nightly-action](https://github.com/scientific-python/upload-nightly-action).


Updates `pypa/cibuildwheel` from 4.2.0 to 4.2.1
- [Release notes](https://github.com/pypa/cibuildwheel/releases)
- [Changelog](https://github.com/pypa/cibuildwheel/blob/main/docs/changelog.md)
- [Commits](pypa/cibuildwheel@v4.2...v4.2.1)

Updates `scientific-python/upload-nightly-action` from 0.6.4 to 0.6.5
- [Release notes](https://github.com/scientific-python/upload-nightly-action/releases)
- [Commits](scientific-python/upload-nightly-action@e76cfec...16fa02e)

---
updated-dependencies:
- dependency-name: pypa/cibuildwheel
  dependency-version: 4.2.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: github-actions-minor-patch
- dependency-name: scientific-python/upload-nightly-action
  dependency-version: 0.6.5
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: github-actions-minor-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
)

Bumps [aws-actions/configure-aws-credentials](https://github.com/aws-actions/configure-aws-credentials) from 4 to 6.
- [Release notes](https://github.com/aws-actions/configure-aws-credentials/releases)
- [Changelog](https://github.com/aws-actions/configure-aws-credentials/blob/main/CHANGELOG.md)
- [Commits](aws-actions/configure-aws-credentials@v4...v6)

---
updated-dependencies:
- dependency-name: aws-actions/configure-aws-credentials
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Tobias Pitters <[email protected]>
Bumps [actions/download-artifact](https://github.com/actions/download-artifact) from 4 to 8.
- [Release notes](https://github.com/actions/download-artifact/releases)
- [Commits](actions/download-artifact@v4...v8)

---
updated-dependencies:
- dependency-name: actions/download-artifact
  dependency-version: '8'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Tobias Pitters <[email protected]>
…hap#5163)

tree_shap_indep() and the Node struct in tree_shap.h stored tree node
indices (children/parent/stack pointers) in 16-bit shorts. Any single
tree with more than 2**15 - 1 nodes silently overflowed these fields,
corrupting heap memory and segfaulting TreeExplainer.shap_values() in
interventional mode.

Widen the affected fields and local variables to 32-bit int, and add a
regression test that builds a >2**15-node tree via the dict model format.
style_context restored the old style after a bare yield, so an
exception inside the block (for example a plot that fails) left the
temporary style applied to every later plot.

This branch has not been deployed

No deployments
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.

4 participants