@@ -502,8 +502,8 @@ def _next_wrapper(self, this: None) -> int: # pylint: disable=unused-argument
502
502
pointer.
503
503
504
504
"""
505
- @_deprecate_positional_args
506
- def data_handle (
505
+ @require_pos_args ( True )
506
+ def input_data (
507
507
data : Any ,
508
508
* ,
509
509
feature_names : Optional [FeatureNames ] = None ,
@@ -528,7 +528,7 @@ def data_handle(
528
528
** kwargs ,
529
529
)
530
530
# pylint: disable=not-callable
531
- return self ._handle_exception (lambda : self .next (data_handle ), 0 )
531
+ return self ._handle_exception (lambda : self .next (input_data ), 0 )
532
532
533
533
@abstractmethod
534
534
def reset (self ) -> None :
@@ -554,7 +554,7 @@ def next(self, input_data: Callable) -> int:
554
554
raise NotImplementedError ()
555
555
556
556
557
- # Notice for `_deprecate_positional_args `
557
+ # Notice for `require_pos_args `
558
558
# Authors: Olivier Grisel
559
559
# Gael Varoquaux
560
560
# Andreas Mueller
@@ -563,50 +563,63 @@ def next(self, input_data: Callable) -> int:
563
563
# Nicolas Tresegnie
564
564
# Sylvain Marie
565
565
# License: BSD 3 clause
566
- def _deprecate_positional_args ( f : Callable [..., _T ]) -> Callable [..., _T ]:
566
+ def require_pos_args ( error : bool ) -> Callable [[ Callable [ ..., _T ]], Callable [..., _T ] ]:
567
567
"""Decorator for methods that issues warnings for positional arguments
568
568
569
569
Using the keyword-only argument syntax in pep 3102, arguments after the
570
- * will issue a warning when passed as a positional argument.
570
+ * will issue a warning or error when passed as a positional argument.
571
571
572
572
Modified from sklearn utils.validation.
573
573
574
574
Parameters
575
575
----------
576
- f : function
577
- function to check arguments on
576
+ error :
577
+ Whether to throw an error or raise a warning.
578
578
"""
579
- sig = signature (f )
580
- kwonly_args = []
581
- all_args = []
582
-
583
- for name , param in sig .parameters .items ():
584
- if param .kind == Parameter .POSITIONAL_OR_KEYWORD :
585
- all_args .append (name )
586
- elif param .kind == Parameter .KEYWORD_ONLY :
587
- kwonly_args .append (name )
588
-
589
- @wraps (f )
590
- def inner_f (* args : Any , ** kwargs : Any ) -> _T :
591
- extra_args = len (args ) - len (all_args )
592
- if extra_args > 0 :
593
- # ignore first 'self' argument for instance methods
594
- args_msg = [
595
- f"{ name } " for name , _ in zip (
596
- kwonly_args [:extra_args ], args [- extra_args :]
597
- )
598
- ]
599
- # pylint: disable=consider-using-f-string
600
- warnings .warn (
601
- "Pass `{}` as keyword args. Passing these as positional "
602
- "arguments will be considered as error in future releases." .
603
- format (", " .join (args_msg )), FutureWarning
604
- )
605
- for k , arg in zip (sig .parameters , args ):
606
- kwargs [k ] = arg
607
- return f (** kwargs )
608
579
609
- return inner_f
580
+ def throw_if (func : Callable [..., _T ]) -> Callable [..., _T ]:
581
+ """Throw error/warning if there are positional arguments after the asterisk.
582
+
583
+ Parameters
584
+ ----------
585
+ f :
586
+ function to check arguments on.
587
+
588
+ """
589
+ sig = signature (func )
590
+ kwonly_args = []
591
+ all_args = []
592
+
593
+ for name , param in sig .parameters .items ():
594
+ if param .kind == Parameter .POSITIONAL_OR_KEYWORD :
595
+ all_args .append (name )
596
+ elif param .kind == Parameter .KEYWORD_ONLY :
597
+ kwonly_args .append (name )
598
+
599
+ @wraps (func )
600
+ def inner_f (* args : Any , ** kwargs : Any ) -> _T :
601
+ extra_args = len (args ) - len (all_args )
602
+ if extra_args > 0 :
603
+ # ignore first 'self' argument for instance methods
604
+ args_msg = [
605
+ f"{ name } "
606
+ for name , _ in zip (kwonly_args [:extra_args ], args [- extra_args :])
607
+ ]
608
+ # pylint: disable=consider-using-f-string
609
+ msg = "Pass `{}` as keyword args." .format (", " .join (args_msg ))
610
+ if error :
611
+ raise TypeError (msg )
612
+ warnings .warn (msg , FutureWarning )
613
+ for k , arg in zip (sig .parameters , args ):
614
+ kwargs [k ] = arg
615
+ return func (** kwargs )
616
+
617
+ return inner_f
618
+
619
+ return throw_if
620
+
621
+
622
+ _deprecate_positional_args = require_pos_args (False )
610
623
611
624
612
625
class DMatrix : # pylint: disable=too-many-instance-attributes,too-many-public-methods
0 commit comments