https://github.com/zbo14/argtypes
A python decorator to check argument types
https://github.com/zbo14/argtypes
decorators type-checking
Last synced: over 1 year ago
JSON representation
A python decorator to check argument types
- Host: GitHub
- URL: https://github.com/zbo14/argtypes
- Owner: zbo14
- License: unlicense
- Created: 2018-02-06T05:29:33.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-02-07T23:13:50.000Z (over 8 years ago)
- Last Synced: 2025-01-31T14:28:58.599Z (over 1 year ago)
- Topics: decorators, type-checking
- Language: Python
- Size: 3.91 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## argtypes
A python decorator to check argument types.
Adapted from [this](https://stackoverflow.com/a/15577293) stack overflow answer.
### Usage
Example
```python
from argtypes import argtypes
def type_error(f, *args):
try:
f(*args)
raise Exception('expected TypeError')
except TypeError:
pass
@argtypes(arg=str)
def expects_string(arg):
pass
@argtypes(arg1=int, arg2=list)
def expects_int_and_list(arg1, arg2):
pass
@argtypes(arg=(float, tuple))
def expects_float_or_tuple(arg):
pass
if __name__ == '__main__':
## ok
expects_string('Damn good coffee!')
expects_int_and_list(8, [1, 2, 3, 4, 5, 6, 7])
expects_float_or_tuple(0.1)
expects_float_or_tuple((0, 1))
## type errors
type_error(expects_string, False)
type_error(expects_int_and_list, 8, ())
type_error(expects_int_and_list, 'Ducks on a lake!', [1, 2, 3])
type_error(expects_float_or_tuple, 1)
print('Done!')
```
### Tests
`python3 -m pytest test`