Decorator which checks whether the function is called with the correct type of parameters
Project description
Strong Typing
Decorator which checks whether the function is called with the correct type of parameters.
And raises TypeMisMatch if the used parameters in a function call where invalid.
See _test_typing.py_ for more informations.
But why??
I really love python and his freedom but with the new option of adding type hints I wanted to get rid of writing
if isinstance(value, whatever)
in my programs.In a bigger project it happened that some developers used a really tiny IDE and others a more advanced one which highlighted typing issues. And there the trouble began, we had a bug and after a longer debugging session we found out that the issue was a wrong type of an argument, it doesn't crashed the program but the output was totally not what we expected.
And this is the reason why I created this package.
Getting Started
As normal decorator
@match_typing
def foo_bar(a: str, b: int, c: list):
...
as class method decorator
class Foo:
...
@match_typing
def foo_bar(self, a: int):
...
You can also use a mix of typed and untyped parameters but then only the typed parameters are check on runtime
@match_typing
def foo_bar(with_type_a: str, without_type_a, with_type_b: list, without_type_b):
...
# no exception
foo_bar('hello', 'world', [1, 2, 3], ('a', 'b'))
# will raise an exception
foo_bar(123, 'world', [1, 2, 3], ('a', 'b'))
It is also possibile to add you own exception
@match_typing(excep_raise=SomeException)
def foo_bar(with_type_a: str, without_type_a, with_type_b: list, without_type_b):
...
And last but not least you can also enable internal cache with cache_size = 1
@match_typing(cache_size=1)
def foo_bar(a: tuple, b: MyClass):
...
At the current state it will work with
- builtin types like: str, int, tuple etc
- from typing:
- List
- Tuple
- Union also nested ( Tuple[Union[str, int], Union[list, tuple]] )
- Any
- Dict
- Set
- Type
- Iterator
- Callable
- with string types representation like
class A:
@match_typing
def func_a(self, a: 'A'):
Tested for Versions
- 3.6, 3.7, 3.8
Prerequisites
- pytest
Installing
- pip install strongtyping
- pip install git+https://github.com/FelixTheC/strongtyping.git
Running the tests
- python test_typing.py
Versioning
- For the versions available, see the tags on this repository.
Authors
- Felix Eisenmenger - Initial work
License
- This project is licensed under the MIT License - see the LICENSE.md file for details
Special thanks
- Thanks to Ruud van der Ham for helping me improve my code
- And all how gave me Feedback in the Pythonista Cafe
Project details
Release history Release notifications | RSS feed
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 strongtyping-1.1.16.tar.gz
.
File metadata
- Download URL: strongtyping-1.1.16.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74ff8162acd080a71f382c74e177740337491cf08f90179baa86069a7e6bdf5e |
|
MD5 | 41c0b21f01cdc7132c8caac6998613a0 |
|
BLAKE2b-256 | ad81cd77cc515fd8b694ac92c60aa6412778be7075f58eee6d5f41acd3af3d97 |
File details
Details for the file strongtyping-1.1.16-py3-none-any.whl
.
File metadata
- Download URL: strongtyping-1.1.16-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3790b1926dcda85fde5d6d0ceae2880edd8d2251b165bd0fb4e62c37a65f4054 |
|
MD5 | d5f0a29f2eb2231d7eba50e9f92e296a |
|
BLAKE2b-256 | f95a33a62478fb5db13cfa2b23269a4138a454e75d940de86a8d978ab980cbe9 |