Skip to content

FIX estimator_html_repr fails on Windows locales with non-UTF-8 default encoding - #35093

Merged
ogrisel merged 17 commits into
scikit-learn:mainfrom
Vinc3ntCyb3rpunk:patch-1
Oct 13, 2026
Merged

ogrisel merged 17 commits into
scikit-learn:mainfrom
Vinc3ntCyb3rpunk:patch-1

Conversation

@Vinc3ntCyb3rpunk

Copy link
Copy Markdown
Contributor

FIX estimator_html_repr fails on Windows locales with non-UTF-8 default encoding

What does this implement/fix? Explain your changes.

On Windows systems where the default locale encoding is not UTF-8 (e.g. Simplified
Chinese Windows with GBK/cp936), displaying an estimator in Jupyter or VS Code
raises a UnicodeDecodeError. This happens because the HTML repr machinery reads
the bundled estimator.js asset without specifying an encoding:

with open(str(Path(__file__).parent / "estimator.js"), "r") as f:
    script = f.read()

When the system default is GBK, decoding the UTF-8-encoded JS file fails:

UnicodeDecodeError: 'gbk' codec can't decode byte 0x94 in position 2952: illegal multibyte sequence

Note that the error is raised inside _repr_mimebundle_, i.e. after fit() has
already completed successfully. Users are therefore misled into thinking their
model training failed.

Changes:

  • sklearn/utils/_repr_html/estimator.py: pass encoding="utf-8" to open()
    when reading estimator.js (and other text assets in the same directory).

Rationale: explicit encodings are consistent with PEP 597's direction, and cost
nothing at runtime since the assets are small and read once per repr. Workarounds
like PYTHONUTF8=1 or sklearn.set_config(display='text') exist but should not
be required for a core display function.

Introduce yourself

I'm a quant researcher from China using scikit-learn for machine
learning / time-series projects. I ran into this while doing feature
selection exercises in VS Code's interactive window on Simplified Chinese Windows:
RFE(...).fit(X, y) succeeded but the cell output crashed with a UnicodeDecodeError.
That led me to trace it down to estimator.py line 563. This is my first
contribution to scikit-learn; happy to adjust as needed.

AI usage disclosure

I used AI assistance for:

  • Research and understanding (tracing the error to the open() call and PEP 597 context)
  • Drafting this PR description and the regression test
  • Code generation for the fix itself

The fix and tests were reviewed and run locally by me.

Add UTF-8 encoding when opening estimator.js file.
@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown

Thank you for opening your first pull request to scikit-learn! 🎉

To help get your contribution reviewed, please make sure that:

  • You have filled out the pull request template.

  • The pull request addresses an existing issue that is ready for contribution (e.g. not tagged as 'Needs Triage', 'Needs Decision', ...). If you are proposing a new feature, please open an issue to discuss it first.

  • There are no other open pull requests already targeting the same issue.

  • You have followed the pull request checklist. In particular, linting and tests should pass.

@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Oct 7, 2026
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Oct 7, 2026

@AnneBeyer AnneBeyer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi @Vinc3ntCyb3rpunk, thank you for the fix. I can confirm that we actually open this file with the explicit encoding in the tests, but not here, so this seems to be an oversight. LGTM.

@AnneBeyer

Copy link
Copy Markdown
Member

Just out of curiosity: Your PR description mentions regression tests and that you ran them locally, but there are no tests included. Could you clarify this?

@AnneBeyer AnneBeyer added Quick Review For PRs that are quick to review No Changelog Needed labels Oct 8, 2026
@Vinc3ntCyb3rpunk

Copy link
Copy Markdown
Contributor Author

Just out of curiosity: Your PR description mentions regression tests and that you ran them locally, but there are no tests included. Could you clarify this?

Hi @AnneBeyer, thanks for the review!

To be honest: I didn't run the test suite at all. I only verified manually on my
machine (Windows, zh-CN/gbk, Python 3.13) that RFE(...).fit(X, y) no longer
crashes the cell output. The word "tests" in my disclosure was imprecise — it
referred to that manual check plus some draft regression tests I never committed.
Sorry for the confusion.

Happy to add a regression test if you think it's warranted; otherwise I'll leave
it as a minimal fix. Let me know.

@DeaMariaLeon

Copy link
Copy Markdown
Member

Thanks for the fix @Vinc3ntCyb3rpunk!. I wonder if you could add a regression test.

Added a regression test for estimator_html_repr to ensure it works under non-UTF-8 locale.
scikit-learn#35093
@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Oct 10, 2026
Vinc3ntCyb3rpunk and others added 8 commits October 10, 2026 21:46
Split long docstring into two lines for readability.
Fix formatting of docstring in non-UTF-8 locale test.
Remove unnecessary blank line before test function.
Remove unnecessary blank line before test_function_transformer_show_caption.
Add a test for FunctionTransformer caption display.
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Oct 10, 2026
@Vinc3ntCyb3rpunk

Copy link
Copy Markdown
Contributor Author

Hi, @DeaMariaLeon.
Fixed GH-35093: estimator_html_repr now works under non-UTF-8 locales.
Regression test added, all CI checks green. (The formatting commit is just
a pre-existing lint issue in that file.)

@DeaMariaLeon

DeaMariaLeon commented Oct 10, 2026 •

Copy link
Copy Markdown
Member

I left some comments in the test, thanks for adding it.

The formatting commit is just a pre-existing lint issue in that file.

I think that in your commits there was a line that was too long. You could install pre-commit so you don't have to wait for the CI to complain, because you see these failures locally: https://scikit-learn.org/stable/developers/development_setup.html#set-up-pre-commit

I would also add a changelog, since it's fixing a visible crash for some users. If the final reviewer (the person who can merge this PR) thinks it's fine without it, I'm fine. But it would be good to add it in the next release as a milestone... just my thought.

Comment thread sklearn/utils/_repr_html/estimator.py Resolved
@Vinc3ntCyb3rpunk

Copy link
Copy Markdown
Contributor Author

Hi @DeaMariaLeon , all comments addressed.

Removed duplicate test for estimator_html_repr under non-UTF-8 locale.

@DeaMariaLeon DeaMariaLeon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thank you

@ogrisel
ogrisel enabled auto-merge (squash) October 13, 2026 09:19
@ogrisel
ogrisel merged commit 700e8a8 into scikit-learn:main Oct 13, 2026
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants