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

Code before:
        - Returns an empty :class:`~sklearn.utils.metadata_routing.MetadataRequest`
            otherwise.

    Returns
    -------
    obj : MetadataRequest or MetadataRouter
        A ``MetadataRequest`` or a ``MetadataRouter`` taken or created from
        the given object.

    Examples
    --------
    >>> from sklearn.datasets import make_classification
    >>> from sklearn.pipeline import Pipeline
    >>> from sklearn.preprocessing import StandardScaler
    >>> from sklearn.linear_model import LogisticRegressionCV
    >>> from sklearn.utils.metadata_routing import get_routing_for_object
    >>> X, y = make_classification()
    >>> pipe = Pipeline([('scaler', StandardScaler()), ('lg', LogisticRegressionCV())])
    >>> pipe.fit(X, y) # doctest: +SKIP
    Pipeline(steps=[('scaler', StandardScaler()), ('lg', LogisticRegressionCV())])
    >>> type(get_routing_for_object(pipe))
    <class 'sklearn.utils._metadata_requests.MetadataRouter'>
    >>> type(get_routing_for_object(pipe.named_steps.scaler))
    <class 'sklearn.utils._metadata_requests.MetadataRequest'>
    >>> type(get_routing_for_object(pipe.named_steps.lg))
    <class 'sklearn.utils._metadata_requests.MetadataRouter'>
    """
    # doing this instead of a try/except since an AttributeError could be raised
    # for other reasons.
    if hasattr(obj, "get_metadata_routing"):
        return deepcopy(obj.get_metadata_routing())

    elif getattr(obj, "_type", None) in ["metadata_request", "metadata_router"]:
        return deepcopy(obj)

    return MetadataRequest(owner=None)


# Request method
# ==============
# This section includes what's needed for the `RequestMethod` descriptor and
# the dynamic generation of `set_{method}_request` methods in the `_MetadataRequester`
# mixin class.

# These strings are used to dynamically generate the docstrings for the methods.
REQUESTER_DOC = """        Configure whether metadata should be requested to be \
passed to the ``{method}`` method.

        Note that this method is only relevant when this estimator is used as a
        sub-estimator within a :term:`meta-estimator` and metadata routing is enabled
        with ``enable_metadata_routing=True`` (see :func:`sklearn.set_config`).

Review comment (suggestion):
```suggestion
    >>> pipe = Pipeline([('scaler', StandardScaler()), ('lr_cv', LogisticRegressionCV())])
    >>> pipe.fit(X, y) # doctest: +SKIP
    Pipeline(steps=[('scaler', StandardScaler()), ('lr_cv', LogisticRegressionCV())])
    >>> type(get_routing_for_object(pipe))
    <class 'sklearn.utils._metadata_requests.MetadataRouter'>
    >>> type(get_routing_for_object(pipe.named_steps.scaler))
    <class 'sklearn.utils._metadata_requests.MetadataRequest'>
    >>> type(get_routing_for_object(pipe.named_steps.lr_cv))
    <class 'sklearn.utils._metadata_requests.MetadataRouter'>
```

Code after:
        - Returns an empty :class:`~sklearn.utils.metadata_routing.MetadataRequest`
            otherwise.

    Returns
    -------
    obj : MetadataRequest or MetadataRouter
        A ``MetadataRequest`` or a ``MetadataRouter`` taken or created from
        the given object.

    Examples
    --------
    >>> from sklearn.datasets import make_classification
    >>> from sklearn.pipeline import Pipeline
    >>> from sklearn.preprocessing import StandardScaler
    >>> from sklearn.linear_model import LogisticRegressionCV
    >>> from sklearn.utils.metadata_routing import get_routing_for_object
    >>> X, y = make_classification()
    >>> pipe = Pipeline(
    ...       [("scaler", StandardScaler()), ("lr_cv", LogisticRegressionCV())]
    ... )
    >>> pipe.fit(X, y) # doctest: +SKIP
    Pipeline(steps=[('scaler', StandardScaler()), ('lr_cv', LogisticRegressionCV())])
    >>> type(get_routing_for_object(pipe))
    <class 'sklearn.utils._metadata_requests.MetadataRouter'>
    >>> type(get_routing_for_object(pipe.named_steps.scaler))
    <class 'sklearn.utils._metadata_requests.MetadataRequest'>
    >>> type(get_routing_for_object(pipe.named_steps.lr_cv))
    <class 'sklearn.utils._metadata_requests.MetadataRouter'>
    """
    # doing this instead of a try/except since an AttributeError could be raised
    # for other reasons.
    if hasattr(obj, "get_metadata_routing"):
        return deepcopy(obj.get_metadata_routing())

    elif getattr(obj, "_type", None) in ["metadata_request", "metadata_router"]:
        return deepcopy(obj)

    return MetadataRequest(owner=None)


# Request method
# ==============
# This section includes what's needed for the `RequestMethod` descriptor and
# the dynamic generation of `set_{method}_request` methods in the `_MetadataRequester`
# mixin class.

# These strings are used to dynamically generate the docstrings for the methods.
REQUESTER_DOC = """        Configure whether metadata should be requested to be \
passed to the ``{method}`` method.

        Note that this method is only relevant when this estimator is used as a