Skip to content

Commit 69f2351

Browse files
committed
Update README
1 parent 0052623 commit 69f2351

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

README.md

+56-1
Original file line numberDiff line numberDiff line change
@@ -773,9 +773,64 @@ match(int, SubclassOf(int, float))
773773
```
774774

775775

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+
776828
### `Arguments(*types)`
777829

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.
779834

780835
```python
781836
def f(x: int, y: float, z):

0 commit comments

Comments
 (0)