Repository: scikit-learn/scikit-learn
File: sklearn/utils/_metadata_requests.py (Python)

Code before:
        The object names are those defined in `obj.get_metadata_routing()`.

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn import set_config
    >>> from sklearn.datasets import make_classification
    >>> from sklearn.base import BaseEstimator, ClassifierMixin
    >>> from sklearn.utils.metadata_routing import (MetadataRouter, MethodMapping,
    ...             process_routing)
    >>> from sklearn.linear_model import LinearRegression
    >>> set_config(enable_metadata_routing=True)
    >>> X, y = make_classification(random_state=0)
    >>> sample_weight = np.ones_like(y)
    >>> class MetaClassifier(ClassifierMixin, BaseEstimator):
    ...     def __init__(self, estimator):
    ...         self.estimator = estimator
    ...     def get_metadata_routing(self):
    ...         router = MetadataRouter(owner=self).add(
    ...             estimator=self.estimator,
    ...             method_mapping=MethodMapping()
    ...             .add(caller="fit", callee="fit")
    ...         )
    ...         return router
    ...     def fit(self, X, y, **fit_params):
    ...         routed_params = process_routing(self, "fit", **fit_params)
    ...         return self
    >>> MetaClassifier(estimator=LinearRegression().set_fit_request(
    ...             sample_weight=True)).fit(X, y, sample_weight=sample_weight)
    MetaClassifier(estimator=LinearRegression())
    >>> set_config(enable_metadata_routing=False)
    """
    if not kwargs:
        # If routing is not enabled and kwargs are empty, then we don't have to
        # try doing any routing, we can simply return a structure which returns
        # an empty dict on routed_params.ANYTHING.ANY_METHOD.
        class EmptyRequest:
            def get(self, name, default=None):
                return Bunch(**{method: dict() for method in METHODS})

            def __getitem__(self, name):
                return Bunch(**{method: dict() for method in METHODS})

            def __getattr__(self, name):
                return Bunch(**{method: dict() for method in METHODS})

        return EmptyRequest()

    if not (hasattr(_obj, "get_metadata_routing") or isinstance(_obj, MetadataRouter)):
        raise AttributeError(
            f"The given object ({_routing_repr(_obj)}) needs to either"

Review comment (suggestion):
I feel that examples of what `process_routing` outputs would be more appropriate, eg 
```python
>>> process_routing(meta_clf, "fit", some_metadata)
... <routed params>
```
 Here it seems more like a developer guide example (how to use process_routing) instead of a direct example (what process_routing does).

Code after:
    **kwargs : dict
        Metadata to be routed.

    Returns
    -------
    routed_params : Bunch
        A :class:`~sklearn.utils.Bunch` of the form ``{"object_name":
        {"method_name": {metadata: value}}}`` which can be used to pass the
        required metadata to corresponding methods or corresponding child objects.
        The object names are those defined in `obj.get_metadata_routing()`.

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn import set_config
    >>> from sklearn.utils.metadata_routing import process_routing
    >>> from sklearn.linear_model import Ridge
    >>> from sklearn.feature_selection import SelectFromModel
    >>> set_config(enable_metadata_routing=True)
    >>> process_routing(
    ...     SelectFromModel(Ridge().set_fit_request(sample_weight=True)),
    ...     "fit",
    ...     sample_weight=np.array([1, 1, 2]),
    ... )
    {'estimator': {'fit': {'sample_weight': array([1, 1, 2])}}}
    >>> set_config(enable_metadata_routing=False)
    """
    if not kwargs:
        # If routing is not enabled and kwargs are empty, then we don't have to
        # try doing any routing, we can simply return a structure which returns
        # an empty dict on routed_params.ANYTHING.ANY_METHOD.
        class EmptyRequest:
            def get(self, name, default=None):
                return Bunch(**{method: dict() for method in METHODS})

            def __getitem__(self, name):
                return Bunch(**{method: dict() for method in METHODS})

            def __getattr__(self, name):
                return Bunch(**{method: dict() for method in METHODS})

        return EmptyRequest()

    if not (hasattr(_obj, "get_metadata_routing") or isinstance(_obj, MetadataRouter)):
        raise AttributeError(
            f"The given object ({_routing_repr(_obj)}) needs to either"
            " implement the routing method `get_metadata_routing` or be a"
            " `MetadataRouter` instance."
        )
    if _method not in METHODS:
        raise TypeError(