EnforceTypes

Simple decorators for enforcing types during runtime.


License
MIT
Install
pip install EnforceTypes==0.0.8

Documentation

License PyPI PePy stats

EnforceTypes

Simple decorators for enforcing types during runtime.

The idea behind this is to reduce time spent developing and using packages that are computationally intensive by allowing for early raising and exiting rather than running for a long time and failing on type or class problems. Currently the easiest way to do this, which is most commonly seen in packages and modules that have type hints applied, is by adding isinstance checks and assert statements throughout the code. This quickly becomes repetitive, doesn't help readability of the code and can easily be forgotten.

The decorators provided in this module resolve that problem by handling these type checks (as annotated) and raising when unexpected types are provided. They also aid in debugging and development by providing both the expected type and the provided argument's runtime type. There are separate decorators for functions and classes, please do not use them the wrong way around.

Due to some very iffy results and instability in the way decorators work when combined, please try using decorators in a different order before submitting bug reports. I can't help that, it's a limitation with the way Python itself handles it.

I'm working on combining common use cases (such as @dataclass which has already been implemented) and would gladly take suggestions! I've also been putting some effort into splitting the package, so auto-imports and __all__ might be somewhat affected until v0.1.0, which I'll release once I'm confident that it's mostly stable (except for the API)