A quick way to verify function signatures in Python
Project description
sigmatch: check function signatures
This small library allows you to quickly check whether any called object matches the signature you expect. This may be useful to you, for example, if you write libraries that work with callbacks.
Install it:
pip install sigmatch
Now to check the signatures of the callable objects, you need to create a SignatureMatcher
object, which will "bake" a description of the parameters you expect. You can pass the following arguments to the constructor of the SignatureMatcher
class (they are all strings):
"."
- corresponds to an ordinary positional argument without a default value."some_argument_name"
- corresponds to an argument with a default value. The content of the string is the name of the argument."*"
- corresponds to packing multiple positional arguments without default values (*args)."**"
- corresponds to packing several named arguments with default values (**kwargs).
Note that the arguments can only go in this order, that is, you cannot specify the packing before the positional argument, otherwise you will get an IncorrectArgumentsOrderError
. When you have prepared a SignatureMatcher
object, you can apply it to function objects and get a response (True
/False
) whether their signatures match the expected ones. As an example, see what a function and a SignatureMatcher
object for it mights look like:
from sigmatch import SignatureMatcher
def function(a, b, c=5, *d, **e):
...
matcher = SignatureMatcher('.', '.', 'c', '*', '**')
print(matcher.match(function)) # True
You can also pass all the expected arguments as a single line, separated by commas. Positional arguments do not have to be separated, they can be presented as a fused set of dots. See:
matcher = SignatureMatcher('.., c, *, **')
The match()
method works with both regular and coroutine functions, as well as with lambdas, generators, classes, methods, and many other callable objects. By default, the match()
method returns a boolean value, but you can ask the library to immediately raise an exception if the function does not have the signature you need:
matcher.match(function, raise_exception=True)
To catch this exception, import the SignatureMismatchError
:
from sigmatch import SignatureMatcher, SignatureMismatchError
try:
SignatureMatcher('.').match(lambda: None, raise_exception=True)
except SignatureMismatchError:
print('Deal with it (⌐■_■)') # It'll be printed.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sigmatch-0.0.4.tar.gz
.
File metadata
- Download URL: sigmatch-0.0.4.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e91a7ff7612203b796df8308b542b6e2f4a086ed6ad382b9123332e3b93916e |
|
MD5 | 06ff017b7d4024e8cc1ea757a1eeb192 |
|
BLAKE2b-256 | b0d9f938450c2c597c07797d7bdaa358f83f8beb2a6ef0325d673725dae8ee7c |
File details
Details for the file sigmatch-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: sigmatch-0.0.4-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52a1207b24e0ce4bd91061cf1c62ab65091ae59db3f0bb389831f44e6fbb3225 |
|
MD5 | 12f6887c35346df68574151fcb8dbef0 |
|
BLAKE2b-256 | 78d12e365e3aae80abdf8517eecb6bb86259f3e8097593c1f6566f798d2eccf6 |