@@ -1086,9 +1086,12 @@ def _serialize(self):
1086
1086
1087
1087
def __iter__ (self ):
1088
1088
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
+ ),
1092
1095
)
1093
1096
for name , route_mapping in self ._route_mappings .items ():
1094
1097
yield (name , route_mapping )
@@ -1234,7 +1237,7 @@ def __init__(self, name, keys, validate_keys=True):
1234
1237
1235
1238
def __get__ (self , instance , owner ):
1236
1239
# we would want to have a method which accepts only the expected args
1237
- def func (** kw ):
1240
+ def func (* args , * *kw ):
1238
1241
"""Updates the request for provided parameters
1239
1242
1240
1243
This docstring is overwritten below.
@@ -1253,15 +1256,32 @@ def func(**kw):
1253
1256
f"arguments are: { set (self .keys )} "
1254
1257
)
1255
1258
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 ()
1257
1277
method_metadata_request = getattr (requests , self .name )
1258
1278
1259
1279
for prop , alias in kw .items ():
1260
1280
if alias is not UNCHANGED :
1261
1281
method_metadata_request .add_request (param = prop , alias = alias )
1262
- instance ._metadata_request = requests
1282
+ _instance ._metadata_request = requests
1263
1283
1264
- return instance
1284
+ return _instance
1265
1285
1266
1286
# Now we set the relevant attributes of the function so that it seems
1267
1287
# like a normal method to the end user, with known expected arguments.
@@ -1525,13 +1545,13 @@ def process_routing(_obj, _method, /, **kwargs):
1525
1545
metadata to corresponding methods or corresponding child objects. The object
1526
1546
names are those defined in `obj.get_metadata_routing()`.
1527
1547
"""
1528
- if not _routing_enabled () and not kwargs :
1548
+ if not kwargs :
1529
1549
# If routing is not enabled and kwargs are empty, then we don't have to
1530
1550
# try doing any routing, we can simply return a structure which returns
1531
1551
# an empty dict on routed_params.ANYTHING.ANY_METHOD.
1532
1552
class EmptyRequest :
1533
1553
def get (self , name , default = None ):
1534
- return default if default else {}
1554
+ return Bunch ( ** { method : dict () for method in METHODS })
1535
1555
1536
1556
def __getitem__ (self , name ):
1537
1557
return Bunch (** {method : dict () for method in METHODS })
0 commit comments