From fbb1827d84163b9b98dee0afc6e7b832b9f9469a Mon Sep 17 00:00:00 2001 From: Jiaming Yuan Date: Tue, 7 Feb 2023 02:00:45 +0800 Subject: [PATCH] Cleanup booster param types. --- python-package/xgboost/_typing.py | 2 +- python-package/xgboost/core.py | 33 +++++++++++-------------------- 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/python-package/xgboost/_typing.py b/python-package/xgboost/_typing.py index 11cd6321dac6..0adad9478770 100644 --- a/python-package/xgboost/_typing.py +++ b/python-package/xgboost/_typing.py @@ -25,7 +25,7 @@ FeatureInfo = Sequence[str] FeatureNames = FeatureInfo FeatureTypes = FeatureInfo -BoosterParam = Union[List, Dict] # better be sequence +BoosterParam = Union[List, Dict[str, Any]] # better be sequence ArrayLike = Any PathLike = Union[str, os.PathLike] diff --git a/python-package/xgboost/core.py b/python-package/xgboost/core.py index ff33a684255a..f3b986e93210 100644 --- a/python-package/xgboost/core.py +++ b/python-package/xgboost/core.py @@ -1655,27 +1655,18 @@ def _transform_interaction_constraints( def _configure_constraints(self, params: BoosterParam) -> BoosterParam: if isinstance(params, dict): - value = params.get("monotone_constraints") - if value is not None: - params["monotone_constraints"] = self._transform_monotone_constrains( - value - ) - - value = params.get("interaction_constraints") - if value is not None: - params[ - "interaction_constraints" - ] = self._transform_interaction_constraints(value) - elif isinstance(params, list): - for idx, param in enumerate(params): - name, value = param - if not value: - continue - - if name == "monotone_constraints": - params[idx] = (name, self._transform_monotone_constrains(value)) - elif name == "interaction_constraints": - params[idx] = (name, self._transform_interaction_constraints(value)) + # we must use list in the internal code as there can be multiple metrics + # with the same parameter name `eval_metric` (same key for dictionary). + params = list(params.items()) + for idx, param in enumerate(params): + name, value = param + if value is None: + continue + + if name == "monotone_constraints": + params[idx] = (name, self._transform_monotone_constrains(value)) + elif name == "interaction_constraints": + params[idx] = (name, self._transform_interaction_constraints(value)) return params