[python-package] Fix python -OO crash by guarding __doc__ assignments#12094
Merged
RAMitchell merged 1 commit intodmlc:masterfrom Mar 18, 2026
Merged
[python-package] Fix python -OO crash by guarding __doc__ assignments#12094RAMitchell merged 1 commit intodmlc:masterfrom
RAMitchell merged 1 commit intodmlc:masterfrom
Conversation
Under `python -OO`, docstrings are stripped and `__doc__` is None. Code that reads `__doc__` from one object to copy or mutate it for another would crash with an AttributeError (e.g. calling `.replace()` on None). Wrap each `__doc__` assignment with a `if source.__doc__ is not None` guard so the assignment is simply skipped under -OO, while preserving full docstring behavior under normal Python.
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes an import-time crash when running XGBoost under python -OO / PYTHONOPTIMIZE=2 by ensuring runtime __doc__ reassignments are skipped when the source docstring is None (as happens when docstrings are stripped).
Changes:
- Guard
save_model/load_modeldocstring copying fromBoosteragainstNone. - Guard
XGBClassifier.fit.__doc__rewriting (previously relied on anassertand.replace()). - Guard docstring copying for Dask wrappers (
predict_proba,fit) againstNone.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| python-package/xgboost/sklearn.py | Adds None checks around Booster.*.__doc__ copies and XGBModel.fit.__doc__-based rewrite to prevent -OO import crashes. |
| python-package/xgboost/dask/init.py | Adds None checks for docstring copying from sklearn wrappers to avoid -OO issues and no-op assignments. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+1125
to
+1126
| if Booster.save_model.__doc__ is not None: | ||
| save_model.__doc__ = f"""{Booster.save_model.__doc__}""" |
Contributor
Author
|
Added a regression test in |
RAMitchell
approved these changes
Mar 18, 2026
PavelGuzenfeld
added a commit
to PavelGuzenfeld/xgboost
that referenced
this pull request
Mar 19, 2026
Add section documenting 3 merged PRs (dmlc#12087, dmlc#12089, dmlc#12094), 1 open PR (dmlc#12086), and 3 closed/superseded PRs.
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
Fixes an import crash when running
python -OO(orPYTHONOPTIMIZE=2) by guarding all runtime__doc__assignments againstNone.Under
-OO, Python strips all docstrings so__doc__isNone. The existing code:Booster.save_model.__doc__/Booster.load_model.__doc__(producesNonein output)XGBModel.fit.__doc__ is not Nonethen calls.replace()on it (crashes withAssertionError)__doc__from sklearn wrappers in dask (assignsNone— harmless but pointless)This PR wraps each
__doc__assignment withif source.__doc__ is not None:so:-OO: assignments are silently skipped, no crashCloses #8537
Supersedes #12085 (reworked based on review feedback — previous approach removed docstrings entirely, this one preserves them)
Test plan
python -OO -c "import xgboost"no longer crashespython -c "import xgboost; help(xgboost.XGBClassifier.fit)"still shows full docstrings