@@ -773,9 +773,64 @@ match(int, SubclassOf(int, float))
773
773
```
774
774
775
775
776
+ ### ` Parameters(...) `
777
+
778
+ Matches the parameters of a callable.
779
+
780
+ ``` python
781
+ def f (x : int , * xs : float , y : str , ** kwargs : bool ):
782
+ pass
783
+
784
+
785
+ match (f, Parameters(int , VarArgs(float ), y = str , KwArgs(bool )))
786
+ ```
787
+
788
+ Each argument to Parameters is expected to be the type of a positional argument.
789
+
790
+ ` Parameters ` matches function signatures if their positional arguments match completely, i.e.
791
+
792
+ ``` python
793
+ def f (x : int , y : float ):
794
+ pass
795
+
796
+
797
+ print (bool (match(f, Parameters(int )))) # False
798
+ print (bool (match(f, Parameters(int , float )))) # True
799
+ print (bool (match(f, Parameters(int , Remaining(_))))) # True
800
+ ```
801
+
802
+ Keyword arguments are matched only if they are keyword only arguments. In contrast to positional arguments it matches
803
+ also impartially (which aligns with the non-strict matching behavior with respect to dictionaries):
804
+
805
+ ``` python
806
+ def f (x : int , * , y : str , z : float ):
807
+ pass
808
+
809
+
810
+ print (bool (match(f, Parameters(int )))) # True
811
+ print (bool (match(f, Parameters(y = str )))) # False – positional parameters not matched
812
+ print (bool (match(f, Parameters(int , y = str )))) # True
813
+ ```
814
+
815
+ This can be changed with ` Strict ` :
816
+
817
+ ``` python
818
+ def f (x : int , * , y : str , z : float ):
819
+ pass
820
+
821
+
822
+ print (bool (match(f, Strict(Parameters(int ))))) # False
823
+ print (bool (match(f, Strict(Parameters(int , y = str ))))) # False (z not mentioned but present)
824
+ print (bool (match(f, Strict(Parameters(int , y = str , z = float ))))) # True (has y and z exactly)
825
+ ```
826
+
827
+
776
828
### ` Arguments(*types) `
777
829
778
- Matches a callable if it's type annotations correspond to the given types. Very useful for implementing rich APIs.
830
+ <span style =" color : red " >** DEPRECATED, use ` Parameters ` instead (see above)** </span >
831
+
832
+
833
+ Matches a callable if it's type annotations correspond to the given types.
779
834
780
835
``` python
781
836
def f (x : int , y : float , z ):
0 commit comments