Repository navigation
FIX estimator_html_repr fails on Windows locales with non-UTF-8 default encoding - #35093
Conversation
Add UTF-8 encoding when opening estimator.js file.
|
Thank you for opening your first pull request to scikit-learn! 🎉 To help get your contribution reviewed, please make sure that:
|
AnneBeyer
left a comment
There was a problem hiding this comment.
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.
|
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 Happy to add a regression test if you think it's warranted; otherwise I'll leave |
|
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
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.
|
Hi, @DeaMariaLeon. |
|
I left some comments in the test, thanks for adding it.
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. |
|
Hi @DeaMariaLeon , all comments addressed. |
Removed duplicate test for estimator_html_repr under non-UTF-8 locale.
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 readsthe bundled
estimator.jsasset without specifying an encoding:When the system default is GBK, decoding the UTF-8-encoded JS file fails:
Note that the error is raised inside
_repr_mimebundle_, i.e. afterfit()hasalready completed successfully. Users are therefore misled into thinking their
model training failed.
Changes:
sklearn/utils/_repr_html/estimator.py: passencoding="utf-8"toopen()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=1orsklearn.set_config(display='text')exist but should notbe 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.pyline 563. This is my firstcontribution to scikit-learn; happy to adjust as needed.
AI usage disclosure
I used AI assistance for:
open()call and PEP 597 context)The fix and tests were reviewed and run locally by me.