Expand source code
# This is just a simple wrapper around sklearn decisiontree
# https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html
from sklearn.tree import DecisionTreeClassifier, export_text, DecisionTreeRegressor
from imodels.util.arguments import check_fit_arguments
from imodels.util.tree import compute_tree_complexity
class GreedyTreeClassifier(DecisionTreeClassifier):
"""Wrapper around sklearn greedy tree classifier
"""
def fit(self, X, y, feature_names=None, sample_weight=None, check_input=True):
"""Build a decision tree classifier from the training set (X, y).
Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
The training input samples. Internally, it will be converted to
``dtype=np.float32`` and if a sparse matrix is provided
to a sparse ``csc_matrix``.
y : array-like of shape (n_samples,) or (n_samples, n_outputs)
The target values (class labels) as integers or strings.
feature_names : array-like of shape (n_features)
The names of the features
sample_weight : array-like of shape (n_samples,), default=None
Sample weights. If None, then samples are equally weighted. Splits
that would create child nodes with net zero or negative weight are
ignored while searching for a split in each node. Splits are also
ignored if they would result in any single class carrying a
negative weight in either child node.
check_input : bool, default=True
Allow to bypass several input checking.
Don't use this parameter unless you know what you do.
Returns
-------
self : DecisionTreeClassifier
Fitted estimator.
"""
X, y, feature_names = check_fit_arguments(self, X, y, feature_names)
self.feature_names = list(feature_names)
classes = self.classes_ # super().fit overwrites this with the encoded labels
names_in = getattr(self, 'feature_names_in_', None)
super().fit(X, y, sample_weight=sample_weight, check_input=check_input)
self.classes_ = classes
if names_in is not None: # super().fit strips this when passed a plain array
self.feature_names_in_ = names_in
self._set_complexity()
return self
def get_rules(self, feature_names=None):
"""Return this model's rules as a DataFrame (see imodels.get_rules)."""
from imodels.util.get_rules import get_rules
return get_rules(self, feature_names=feature_names)
def _set_complexity(self):
"""Set complexity as number of non-leaf nodes
"""
self.complexity_ = compute_tree_complexity(self.tree_)
def __str__(self):
s = '> ------------------------------\n'
s += '> Greedy CART Tree:\n'
s += '> \tPrediction is made by looking at the value in the appropriate leaf of the tree\n'
s += '> ------------------------------' + '\n'
if hasattr(self, 'feature_names') and self.feature_names is not None:
return s + export_text(self, feature_names=self.feature_names, show_weights=True)
else:
return s + export_text(self, show_weights=True)
class GreedyTreeRegressor(DecisionTreeRegressor):
"""Wrapper around sklearn greedy tree regressor
"""
def fit(self, X, y, feature_names=None, sample_weight=None, check_input=True):
"""Build a decision tree regressor from the training set (X, y).
Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
The training input samples. Internally, it will be converted to
``dtype=np.float32`` and if a sparse matrix is provided
to a sparse ``csc_matrix``.
y : array-like of shape (n_samples,) or (n_samples, n_outputs)
The target values (real numbers). Use ``dtype=np.float64`` and
``order='C'`` for maximum efficiency.
sample_weight : array-like of shape (n_samples,), default=None
Sample weights. If None, then samples are equally weighted. Splits
that would create child nodes with net zero or negative weight are
ignored while searching for a split in each node.
check_input : bool, default=True
Allow to bypass several input checking.
Don't use this parameter unless you know what you do.
Returns
-------
self : DecisionTreeRegressor
Fitted estimator.
"""
X, y, feature_names = check_fit_arguments(self, X, y, feature_names)
self.feature_names = list(feature_names)
names_in = getattr(self, 'feature_names_in_', None)
super().fit(X, y, sample_weight=sample_weight, check_input=check_input)
if names_in is not None: # super().fit strips this when passed a plain array
self.feature_names_in_ = names_in
self._set_complexity()
return self
def get_rules(self, feature_names=None):
"""Return this model's rules as a DataFrame (see imodels.get_rules)."""
from imodels.util.get_rules import get_rules
return get_rules(self, feature_names=feature_names)
def _set_complexity(self):
"""Set complexity as number of non-leaf nodes
"""
self.complexity_ = compute_tree_complexity(self.tree_)
def __str__(self):
if hasattr(self, 'feature_names') and self.feature_names is not None:
return 'GreedyTree:\n' + export_text(self, feature_names=self.feature_names, show_weights=True)
else:
return 'GreedyTree:\n' + export_text(self, show_weights=True)
Classes
class GreedyTreeClassifier (*, criterion='gini', splitter='best', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, random_state=None, max_leaf_nodes=None, min_impurity_decrease=0.0, class_weight=None, ccp_alpha=0.0, monotonic_cst=None)-
Wrapper around sklearn greedy tree classifier
Expand source code
class GreedyTreeClassifier(DecisionTreeClassifier): """Wrapper around sklearn greedy tree classifier """ def fit(self, X, y, feature_names=None, sample_weight=None, check_input=True): """Build a decision tree classifier from the training set (X, y). Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features) The training input samples. Internally, it will be converted to ``dtype=np.float32`` and if a sparse matrix is provided to a sparse ``csc_matrix``. y : array-like of shape (n_samples,) or (n_samples, n_outputs) The target values (class labels) as integers or strings. feature_names : array-like of shape (n_features) The names of the features sample_weight : array-like of shape (n_samples,), default=None Sample weights. If None, then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node. Splits are also ignored if they would result in any single class carrying a negative weight in either child node. check_input : bool, default=True Allow to bypass several input checking. Don't use this parameter unless you know what you do. Returns ------- self : DecisionTreeClassifier Fitted estimator. """ X, y, feature_names = check_fit_arguments(self, X, y, feature_names) self.feature_names = list(feature_names) classes = self.classes_ # super().fit overwrites this with the encoded labels names_in = getattr(self, 'feature_names_in_', None) super().fit(X, y, sample_weight=sample_weight, check_input=check_input) self.classes_ = classes if names_in is not None: # super().fit strips this when passed a plain array self.feature_names_in_ = names_in self._set_complexity() return self def get_rules(self, feature_names=None): """Return this model's rules as a DataFrame (see imodels.get_rules).""" from imodels.util.get_rules import get_rules return get_rules(self, feature_names=feature_names) def _set_complexity(self): """Set complexity as number of non-leaf nodes """ self.complexity_ = compute_tree_complexity(self.tree_) def __str__(self): s = '> ------------------------------\n' s += '> Greedy CART Tree:\n' s += '> \tPrediction is made by looking at the value in the appropriate leaf of the tree\n' s += '> ------------------------------' + '\n' if hasattr(self, 'feature_names') and self.feature_names is not None: return s + export_text(self, feature_names=self.feature_names, show_weights=True) else: return s + export_text(self, show_weights=True)Ancestors
- sklearn.tree._classes.DecisionTreeClassifier
- sklearn.base.ClassifierMixin
- sklearn.tree._classes.BaseDecisionTree
- sklearn.base.MultiOutputMixin
- sklearn.base.BaseEstimator
- sklearn.utils._repr_html.base.ReprHTMLMixin
- sklearn.utils._repr_html.base._HTMLDocumentationLinkMixin
- sklearn.utils._metadata_requests._MetadataRequester
Methods
def fit(self, X, y, feature_names=None, sample_weight=None, check_input=True)-
Build a decision tree classifier from the training set (X, y). Parameters
X:{array-like, sparse matrix}ofshape (n_samples, n_features)- The training input samples. Internally, it will be converted to
dtype=np.float32and if a sparse matrix is provided to a sparsecsc_matrix. y:array-likeofshape (n_samples,)or(n_samples, n_outputs)- The target values (class labels) as integers or strings.
feature_names:array-likeofshape (n_features)- The names of the features
sample_weight:array-likeofshape (n_samples,), default=None- Sample weights. If None, then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node. Splits are also ignored if they would result in any single class carrying a negative weight in either child node.
check_input:bool, default=True- Allow to bypass several input checking. Don't use this parameter unless you know what you do.
Returns
self:DecisionTreeClassifier- Fitted estimator.
Expand source code
def fit(self, X, y, feature_names=None, sample_weight=None, check_input=True): """Build a decision tree classifier from the training set (X, y). Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features) The training input samples. Internally, it will be converted to ``dtype=np.float32`` and if a sparse matrix is provided to a sparse ``csc_matrix``. y : array-like of shape (n_samples,) or (n_samples, n_outputs) The target values (class labels) as integers or strings. feature_names : array-like of shape (n_features) The names of the features sample_weight : array-like of shape (n_samples,), default=None Sample weights. If None, then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node. Splits are also ignored if they would result in any single class carrying a negative weight in either child node. check_input : bool, default=True Allow to bypass several input checking. Don't use this parameter unless you know what you do. Returns ------- self : DecisionTreeClassifier Fitted estimator. """ X, y, feature_names = check_fit_arguments(self, X, y, feature_names) self.feature_names = list(feature_names) classes = self.classes_ # super().fit overwrites this with the encoded labels names_in = getattr(self, 'feature_names_in_', None) super().fit(X, y, sample_weight=sample_weight, check_input=check_input) self.classes_ = classes if names_in is not None: # super().fit strips this when passed a plain array self.feature_names_in_ = names_in self._set_complexity() return self def get_rules(self, feature_names=None)-
Return this model's rules as a DataFrame (see imodels.get_rules).
Expand source code
def get_rules(self, feature_names=None): """Return this model's rules as a DataFrame (see imodels.get_rules).""" from imodels.util.get_rules import get_rules return get_rules(self, feature_names=feature_names) def set_fit_request(self: GreedyTreeClassifier, *, feature_names: bool | str | None = '$UNCHANGED$', sample_weight: bool | str | None = '$UNCHANGED$') ‑> GreedyTreeClassifier-
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a :term:
meta-estimatorand metadata routing is enabled withenable_metadata_routing=True(see :func:sklearn.set_config). Please check the :ref:User Guide <metadata_routing>on how the routing mechanism works.The options for each parameter are:
-
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided. -
False: metadata is not requested and the meta-estimator will not pass it tofit. -
None: metadata is not requested, and the meta-estimator will raise an error if the user provides it. -
str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version: 1.3
Parameters
feature_names:str, True, False,orNone, default=sklearn.utils.metadata_routing.UNCHANGED- Metadata routing for
feature_namesparameter infit. sample_weight:str, True, False,orNone, default=sklearn.utils.metadata_routing.UNCHANGED- Metadata routing for
sample_weightparameter infit.
Returns
self:object- The updated object.
Expand source code
def func(*args, **kw): """Updates the `_metadata_request` attribute of the consumer (`instance`) for the parameters provided as `**kw`. This docstring is overwritten below. See REQUESTER_DOC for expected functionality. """ if not _routing_enabled(): raise RuntimeError( "This method is only available when metadata routing is enabled." " You can enable it using" " sklearn.set_config(enable_metadata_routing=True)." ) if self.validate_keys and (set(kw) - set(self.keys)): raise TypeError( f"Unexpected args: {set(kw) - set(self.keys)} in {self.name}. " f"Accepted arguments are: {set(self.keys)}" ) # This makes it possible to use the decorated method as an unbound method, # for instance when monkeypatching. # https://github.com/scikit-learn/scikit-learn/issues/28632 if instance is None: _instance = args[0] args = args[1:] else: _instance = instance # Replicating python's behavior when positional args are given other than # `self`, and `self` is only allowed if this method is unbound. if args: raise TypeError( f"set_{self.name}_request() takes 0 positional argument but" f" {len(args)} were given" ) requests = _instance._get_metadata_request() method_metadata_request = getattr(requests, self.name) for prop, alias in kw.items(): if alias is not UNCHANGED: method_metadata_request.add_request(param=prop, alias=alias) _instance._metadata_request = requests return _instance -
def set_score_request(self: GreedyTreeClassifier, *, sample_weight: bool | str | None = '$UNCHANGED$') ‑> GreedyTreeClassifier-
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a :term:
meta-estimatorand metadata routing is enabled withenable_metadata_routing=True(see :func:sklearn.set_config). Please check the :ref:User Guide <metadata_routing>on how the routing mechanism works.The options for each parameter are:
-
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided. -
False: metadata is not requested and the meta-estimator will not pass it toscore. -
None: metadata is not requested, and the meta-estimator will raise an error if the user provides it. -
str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version: 1.3
Parameters
sample_weight:str, True, False,orNone, default=sklearn.utils.metadata_routing.UNCHANGED- Metadata routing for
sample_weightparameter inscore.
Returns
self:object- The updated object.
Expand source code
def func(*args, **kw): """Updates the `_metadata_request` attribute of the consumer (`instance`) for the parameters provided as `**kw`. This docstring is overwritten below. See REQUESTER_DOC for expected functionality. """ if not _routing_enabled(): raise RuntimeError( "This method is only available when metadata routing is enabled." " You can enable it using" " sklearn.set_config(enable_metadata_routing=True)." ) if self.validate_keys and (set(kw) - set(self.keys)): raise TypeError( f"Unexpected args: {set(kw) - set(self.keys)} in {self.name}. " f"Accepted arguments are: {set(self.keys)}" ) # This makes it possible to use the decorated method as an unbound method, # for instance when monkeypatching. # https://github.com/scikit-learn/scikit-learn/issues/28632 if instance is None: _instance = args[0] args = args[1:] else: _instance = instance # Replicating python's behavior when positional args are given other than # `self`, and `self` is only allowed if this method is unbound. if args: raise TypeError( f"set_{self.name}_request() takes 0 positional argument but" f" {len(args)} were given" ) requests = _instance._get_metadata_request() method_metadata_request = getattr(requests, self.name) for prop, alias in kw.items(): if alias is not UNCHANGED: method_metadata_request.add_request(param=prop, alias=alias) _instance._metadata_request = requests return _instance -
class GreedyTreeRegressor (*, criterion='squared_error', splitter='best', max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, random_state=None, max_leaf_nodes=None, min_impurity_decrease=0.0, ccp_alpha=0.0, monotonic_cst=None)-
Wrapper around sklearn greedy tree regressor
Expand source code
class GreedyTreeRegressor(DecisionTreeRegressor): """Wrapper around sklearn greedy tree regressor """ def fit(self, X, y, feature_names=None, sample_weight=None, check_input=True): """Build a decision tree regressor from the training set (X, y). Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features) The training input samples. Internally, it will be converted to ``dtype=np.float32`` and if a sparse matrix is provided to a sparse ``csc_matrix``. y : array-like of shape (n_samples,) or (n_samples, n_outputs) The target values (real numbers). Use ``dtype=np.float64`` and ``order='C'`` for maximum efficiency. sample_weight : array-like of shape (n_samples,), default=None Sample weights. If None, then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node. check_input : bool, default=True Allow to bypass several input checking. Don't use this parameter unless you know what you do. Returns ------- self : DecisionTreeRegressor Fitted estimator. """ X, y, feature_names = check_fit_arguments(self, X, y, feature_names) self.feature_names = list(feature_names) names_in = getattr(self, 'feature_names_in_', None) super().fit(X, y, sample_weight=sample_weight, check_input=check_input) if names_in is not None: # super().fit strips this when passed a plain array self.feature_names_in_ = names_in self._set_complexity() return self def get_rules(self, feature_names=None): """Return this model's rules as a DataFrame (see imodels.get_rules).""" from imodels.util.get_rules import get_rules return get_rules(self, feature_names=feature_names) def _set_complexity(self): """Set complexity as number of non-leaf nodes """ self.complexity_ = compute_tree_complexity(self.tree_) def __str__(self): if hasattr(self, 'feature_names') and self.feature_names is not None: return 'GreedyTree:\n' + export_text(self, feature_names=self.feature_names, show_weights=True) else: return 'GreedyTree:\n' + export_text(self, show_weights=True)Ancestors
- sklearn.tree._classes.DecisionTreeRegressor
- sklearn.base.RegressorMixin
- sklearn.tree._classes.BaseDecisionTree
- sklearn.base.MultiOutputMixin
- sklearn.base.BaseEstimator
- sklearn.utils._repr_html.base.ReprHTMLMixin
- sklearn.utils._repr_html.base._HTMLDocumentationLinkMixin
- sklearn.utils._metadata_requests._MetadataRequester
Methods
def fit(self, X, y, feature_names=None, sample_weight=None, check_input=True)-
Build a decision tree regressor from the training set (X, y). Parameters
X:{array-like, sparse matrix}ofshape (n_samples, n_features)- The training input samples. Internally, it will be converted to
dtype=np.float32and if a sparse matrix is provided to a sparsecsc_matrix. y:array-likeofshape (n_samples,)or(n_samples, n_outputs)- The target values (real numbers). Use
dtype=np.float64andorder='C'for maximum efficiency. sample_weight:array-likeofshape (n_samples,), default=None- Sample weights. If None, then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node.
check_input:bool, default=True- Allow to bypass several input checking. Don't use this parameter unless you know what you do.
Returns
self:DecisionTreeRegressor- Fitted estimator.
Expand source code
def fit(self, X, y, feature_names=None, sample_weight=None, check_input=True): """Build a decision tree regressor from the training set (X, y). Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features) The training input samples. Internally, it will be converted to ``dtype=np.float32`` and if a sparse matrix is provided to a sparse ``csc_matrix``. y : array-like of shape (n_samples,) or (n_samples, n_outputs) The target values (real numbers). Use ``dtype=np.float64`` and ``order='C'`` for maximum efficiency. sample_weight : array-like of shape (n_samples,), default=None Sample weights. If None, then samples are equally weighted. Splits that would create child nodes with net zero or negative weight are ignored while searching for a split in each node. check_input : bool, default=True Allow to bypass several input checking. Don't use this parameter unless you know what you do. Returns ------- self : DecisionTreeRegressor Fitted estimator. """ X, y, feature_names = check_fit_arguments(self, X, y, feature_names) self.feature_names = list(feature_names) names_in = getattr(self, 'feature_names_in_', None) super().fit(X, y, sample_weight=sample_weight, check_input=check_input) if names_in is not None: # super().fit strips this when passed a plain array self.feature_names_in_ = names_in self._set_complexity() return self def get_rules(self, feature_names=None)-
Return this model's rules as a DataFrame (see imodels.get_rules).
Expand source code
def get_rules(self, feature_names=None): """Return this model's rules as a DataFrame (see imodels.get_rules).""" from imodels.util.get_rules import get_rules return get_rules(self, feature_names=feature_names) def set_fit_request(self: GreedyTreeRegressor, *, feature_names: bool | str | None = '$UNCHANGED$', sample_weight: bool | str | None = '$UNCHANGED$') ‑> GreedyTreeRegressor-
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a :term:
meta-estimatorand metadata routing is enabled withenable_metadata_routing=True(see :func:sklearn.set_config). Please check the :ref:User Guide <metadata_routing>on how the routing mechanism works.The options for each parameter are:
-
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided. -
False: metadata is not requested and the meta-estimator will not pass it tofit. -
None: metadata is not requested, and the meta-estimator will raise an error if the user provides it. -
str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version: 1.3
Parameters
feature_names:str, True, False,orNone, default=sklearn.utils.metadata_routing.UNCHANGED- Metadata routing for
feature_namesparameter infit. sample_weight:str, True, False,orNone, default=sklearn.utils.metadata_routing.UNCHANGED- Metadata routing for
sample_weightparameter infit.
Returns
self:object- The updated object.
Expand source code
def func(*args, **kw): """Updates the `_metadata_request` attribute of the consumer (`instance`) for the parameters provided as `**kw`. This docstring is overwritten below. See REQUESTER_DOC for expected functionality. """ if not _routing_enabled(): raise RuntimeError( "This method is only available when metadata routing is enabled." " You can enable it using" " sklearn.set_config(enable_metadata_routing=True)." ) if self.validate_keys and (set(kw) - set(self.keys)): raise TypeError( f"Unexpected args: {set(kw) - set(self.keys)} in {self.name}. " f"Accepted arguments are: {set(self.keys)}" ) # This makes it possible to use the decorated method as an unbound method, # for instance when monkeypatching. # https://github.com/scikit-learn/scikit-learn/issues/28632 if instance is None: _instance = args[0] args = args[1:] else: _instance = instance # Replicating python's behavior when positional args are given other than # `self`, and `self` is only allowed if this method is unbound. if args: raise TypeError( f"set_{self.name}_request() takes 0 positional argument but" f" {len(args)} were given" ) requests = _instance._get_metadata_request() method_metadata_request = getattr(requests, self.name) for prop, alias in kw.items(): if alias is not UNCHANGED: method_metadata_request.add_request(param=prop, alias=alias) _instance._metadata_request = requests return _instance -
def set_score_request(self: GreedyTreeRegressor, *, sample_weight: bool | str | None = '$UNCHANGED$') ‑> GreedyTreeRegressor-
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a :term:
meta-estimatorand metadata routing is enabled withenable_metadata_routing=True(see :func:sklearn.set_config). Please check the :ref:User Guide <metadata_routing>on how the routing mechanism works.The options for each parameter are:
-
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided. -
False: metadata is not requested and the meta-estimator will not pass it toscore. -
None: metadata is not requested, and the meta-estimator will raise an error if the user provides it. -
str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version: 1.3
Parameters
sample_weight:str, True, False,orNone, default=sklearn.utils.metadata_routing.UNCHANGED- Metadata routing for
sample_weightparameter inscore.
Returns
self:object- The updated object.
Expand source code
def func(*args, **kw): """Updates the `_metadata_request` attribute of the consumer (`instance`) for the parameters provided as `**kw`. This docstring is overwritten below. See REQUESTER_DOC for expected functionality. """ if not _routing_enabled(): raise RuntimeError( "This method is only available when metadata routing is enabled." " You can enable it using" " sklearn.set_config(enable_metadata_routing=True)." ) if self.validate_keys and (set(kw) - set(self.keys)): raise TypeError( f"Unexpected args: {set(kw) - set(self.keys)} in {self.name}. " f"Accepted arguments are: {set(self.keys)}" ) # This makes it possible to use the decorated method as an unbound method, # for instance when monkeypatching. # https://github.com/scikit-learn/scikit-learn/issues/28632 if instance is None: _instance = args[0] args = args[1:] else: _instance = instance # Replicating python's behavior when positional args are given other than # `self`, and `self` is only allowed if this method is unbound. if args: raise TypeError( f"set_{self.name}_request() takes 0 positional argument but" f" {len(args)} were given" ) requests = _instance._get_metadata_request() method_metadata_request = getattr(requests, self.name) for prop, alias in kw.items(): if alias is not UNCHANGED: method_metadata_request.add_request(param=prop, alias=alias) _instance._metadata_request = requests return _instance -