[py] Use generic in xgboost model doc decorator #11071
Merged
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.
Context
XGBoost classes are wrapped with
xboost_model_doc
decorator.Problem
xgboost_model_doc
is typed as returningCallable[[Type], [Type]
, which ends up obfuscating the real type of the class. For example, if you try to define a union type with other classes, it ends up asUnknown
:Both
ModelType
andModelType2
are treated as variables instead of types by mypy.Solution
Use a generic type so that the type checker can infer that the returned class is the same as the decorated one.
With this fix, the above issue is solved.
Caveat
This is my first time contributing to this repo. I'm pretty sure this won't break anything (it's just a typing change), but please let me know if I missed anything,