Skip to content

[python-package] Replace bare Any with specific types in sklearn helpers#12111

Open
PavelGuzenfeld wants to merge 2 commits intodmlc:masterfrom
PavelGuzenfeld:fix/sklearn-remove-any
Open

[python-package] Replace bare Any with specific types in sklearn helpers#12111
PavelGuzenfeld wants to merge 2 commits intodmlc:masterfrom
PavelGuzenfeld:fix/sklearn-remove-any

Conversation

@PavelGuzenfeld
Copy link
Copy Markdown
Contributor

@PavelGuzenfeld PavelGuzenfeld commented Mar 19, 2026

Summary

Replace generic Any annotations with more descriptive types in sklearn.py internal helper functions. Follow-up to #6496.

Changes:

Function Parameter Before After
_wrap_evaluation_matrices X, y Any ArrayLike
_wrap_evaluation_matrices group, qid, sample_weight, base_margin Optional[Any] Optional[ArrayLike]
_wrap_evaluation_matrices eval_set Optional[Sequence[Tuple[Any, Any]]] Optional[Sequence[Tuple[ArrayLike, ArrayLike]]]
_wrap_evaluation_matrices *_eval_set, eval_group, eval_qid Optional[Sequence[Any]] Optional[Sequence[ArrayLike]]
pick_ref_categories X Any ArrayLike
invalid_type m Any object

The return type of _wrap_evaluation_matrices stays Tuple[Any, ...] since it is called with both DMatrix and DaskDMatrix (which does not extend DMatrix).

Remaining Any usages (**kwargs: Any, Dict[str, Any] for params) are intentional — they represent genuinely heterogeneous data or standard sklearn patterns.

Test plan

  • CI pre-commit passes
  • CI mypy type check passes

Replace generic Any annotations with ArrayLike for data parameters
and DMatrix for return types in internal helper functions:

- _wrap_evaluation_matrices: 13 params Any -> ArrayLike, return
  Tuple[Any, ...] -> Tuple[DMatrix, List[Tuple[DMatrix, str]]]
- pick_ref_categories: X param Any -> ArrayLike
- invalid_type: m param Any -> object

Remaining Any usages (**kwargs, Dict[str, Any] for params) are
intentional as they represent genuinely heterogeneous data.

Follow-up to dmlc#6496.
PavelGuzenfeld added a commit to PavelGuzenfeld/xgboost that referenced this pull request Mar 19, 2026
return wrapped

def invalid_type(m: Any) -> None:
def invalid_type(m: object) -> None:
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.

why?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

object is the proper top type in Python — it means "accepts anything" while keeping type safety. Any disables type checking entirely (both co- and contra-variant), so mypy silently skips any misuse. Since invalid_type only calls type(m), which works on every object, object is the accurate annotation.

That said, this is a minor stylistic point — happy to revert if you prefer keeping Any here.

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.

2 participants