Package: pysignature
# PySignaturePySingature is a python package that provides a set of utilitiesfor simple function parameter typechecking, without modifyingthe target function and with minimal boilerplate or impacton performance.## UsagePySignature provides two main components: a small set ofclasses that represent type assertions and a decorator for definingthe function's signature.### ExampleLet's take a look at a (very) simple function:```pythondef fn(x, y):return x * y```We can add parameter typechecking using the PySignature in thefollowing way:```pythonfrom pysignature import typecheckedfrom pysignature.types import Numeric@typechecked(x=Numeric, y=Numeric)def fn(x, y):return x + y```Now if we call `fn` with non-numeric types (neither ints nor floats)we get an exception `FunctionTypeCheckError` with detailed informationabout the wrong parameters in its property `errors`.### Complex type assertionsWe can go beyond numeric validation and use complex type assertionsprovided by this module, or we can use any callable that uppon errorreturns a `TypeError` (such as `float`).To expand our previous example, let's consider the fact that the `*`operator works if the left operand is a list or a string and the rightoperand is an integer (`Numeric` accepts floats). We can make useof built-in function `int` and PySignature's `List`, `Or`, `List` and `Any`type assertions.```pythonfrom pysignature import typecheckedfrom pysignature.types import String, Or, List, Any@typechecked(x=Or(String, List(Any)), y=int)def fn(x, y):return x + y```### Variadic and keyword argumentsPySignature supports typechecking for `*args` and `**kwargs` argumenttypes. The only limitation is that they all must conform to a singletype. They are specified in the following way:```python@typechecked(x=String, _variadic=Numeric, _named=List(List(Any)))def fn(x, *extra, **options):# ... Do somethingreturn```### Bypassing typechecksEach function decorated with `pysignature.typechecked` getsa property named `untyped` that ignores the typecheckingfunctionality. In our original example, that would meanwe can use `fn.untyped(None, 1)`.## RationalePySignature was created to cover a very specific need: allowfor robust handling of parameters coming from the external world(the internet) without burdening the data scientists at[Intelimétrica](https://intelimetrica.com/) with the factthat their functions might be called with unsafe arguments.The idea is to use PySignature in the (normally) small subsetof functions that are exposed from a certain module to theoutside world, so the mantainers of the function can developwithout extra cognitive burden and the clients of such functioncan use them with extra confidence and robustness.## What PySignature is notPySignature is not (neither tries to be) an extension to Python'stype system. It does not attempt to bring full static typyingto the languge and while it is not slow, it is not recommendedfor functions that are too be called in very tight loops.## InstallationSimply install via pip with:```$ pip install pysignature```Or, directly download the package and run```$ python setup.py install```## ContributeYes please! Help us with documentation, reporting bugs,implementing new type assertions or improving codeThe development requirements are minimal. The only considerationis that we use [pytest](https://github.com/pytest-dev/pytest/)for testing.## LICENSEPySignature uses the Apache License 2.0.