@@ -1086,9 +1086,12 @@ def _serialize(self):
10861086
10871087 def __iter__ (self ):
10881088 if self ._self_request :
1089- yield "$self_request" , RouterMappingPair (
1090- mapping = MethodMapping .from_str ("one-to-one" ),
1091- router = self ._self_request ,
1089+ yield (
1090+ "$self_request" ,
1091+ RouterMappingPair (
1092+ mapping = MethodMapping .from_str ("one-to-one" ),
1093+ router = self ._self_request ,
1094+ ),
10921095 )
10931096 for name , route_mapping in self ._route_mappings .items ():
10941097 yield (name , route_mapping )
@@ -1234,7 +1237,7 @@ def __init__(self, name, keys, validate_keys=True):
12341237
12351238 def __get__ (self , instance , owner ):
12361239 # we would want to have a method which accepts only the expected args
1237- def func (** kw ):
1240+ def func (* args , * *kw ):
12381241 """Updates the request for provided parameters
12391242
12401243 This docstring is overwritten below.
@@ -1253,15 +1256,32 @@ def func(**kw):
12531256 f"arguments are: { set (self .keys )} "
12541257 )
12551258
1256- requests = instance ._get_metadata_request ()
1259+ # This makes it possible to use the decorated method as an unbound
1260+ # method, for instance when monkeypatching.
1261+ # https://github.com/scikit-learn/scikit-learn/issues/28632
1262+ if instance is None :
1263+ _instance = args [0 ]
1264+ args = args [1 :]
1265+ else :
1266+ _instance = instance
1267+
1268+ # Replicating python's behavior when positional args are given other
1269+ # than `self`, and `self` is only allowed if this method is unbound.
1270+ if args :
1271+ raise TypeError (
1272+ f"set_{ self .name } _request() takes 0 positional argument but"
1273+ f" { len (args )} were given"
1274+ )
1275+
1276+ requests = _instance ._get_metadata_request ()
12571277 method_metadata_request = getattr (requests , self .name )
12581278
12591279 for prop , alias in kw .items ():
12601280 if alias is not UNCHANGED :
12611281 method_metadata_request .add_request (param = prop , alias = alias )
1262- instance ._metadata_request = requests
1282+ _instance ._metadata_request = requests
12631283
1264- return instance
1284+ return _instance
12651285
12661286 # Now we set the relevant attributes of the function so that it seems
12671287 # like a normal method to the end user, with known expected arguments.
@@ -1525,13 +1545,13 @@ def process_routing(_obj, _method, /, **kwargs):
15251545 metadata to corresponding methods or corresponding child objects. The object
15261546 names are those defined in `obj.get_metadata_routing()`.
15271547 """
1528- if not _routing_enabled () and not kwargs :
1548+ if not kwargs :
15291549 # If routing is not enabled and kwargs are empty, then we don't have to
15301550 # try doing any routing, we can simply return a structure which returns
15311551 # an empty dict on routed_params.ANYTHING.ANY_METHOD.
15321552 class EmptyRequest :
15331553 def get (self , name , default = None ):
1534- return default if default else {}
1554+ return Bunch ( ** { method : dict () for method in METHODS })
15351555
15361556 def __getitem__ (self , name ):
15371557 return Bunch (** {method : dict () for method in METHODS })
0 commit comments