A lightweight Python type checker. Designed with the developer in mind.
Project description
Waycheck (Python Type Checker)
A lightweight Python type checker. Designed with the developer in mind. The Waycheck class is a decorator, which means you can use it by prepending '@' above the function you'd like to type check. No library dependencies under the hood - Pure Python ;)!
Install
python -m pip --install waycheck
Getting Started
You can refer to the demo.py
file in this repository or the code block below
to see how Waycheck
works.
import waycheck
# Defining the type for each argument
@Waycheck(a=int, b=int)
def main(a, b):
print('Total: ', a + b)
# Defining the default type for ALL arguments
@Waycheck(default_type=int)
def main2(a, b):
print('Total: ', a + b)
if __name__ == '__main__':
main(5, 10) # this will pass
main('5', 10) # this will fail
main2(5, 10) # this will pass
main2('5', 10) # this will fail
Accepted Types
Waycheck can type check any valid Python Type provided to it, as well as a wildcard type. A wildcard type is used when an argument can be any value. This is handy when you're doing your own type checking within your function and would like Waycheck to skip type checking that argument.
You can specify a wildcard by using '*'. See an example below:
@Waycheck(a='*', b=bool)
def main(a, b):
if b:
return a
You can also pass a tuple of Types, instead of '*' (wildcat). See an example below:
@Waycheck(a=(str, int))
def main(a):
print('Argument a is: ', a, ' - type: ', type(a))
Exmaples
Refer to demo.py
file.
FAQ
Q: My type checking is failling/passing, but I know it shouldn't be. Why is this happening? A: This happens when you don't specify the correct order of appearance for the Waycheck class. The order in which the arguments are passed to the function, should be the exact same order you provide to the Waycheck class. By not doing this, unexpected type checking behavior will occur, such as passing when it should fail or failing when it should pass.
Note: This will occur if the argument all have different expected type values. i.e if all arguments are supposed to be integers, then the order doesn't matter. Example:
# this works as expected
@Waycheck(a=int, b=int)
def main(a, b):
...
# this will fail if all the arguments
# are not the same expected type
@Waycheck(b=int, a=int)
def main(a, b):
...
Q: Can I pass my own custom error class?
A: No. The default error given is WaycheckTypeError
. You could use a blanket Exeception check (try/except) then throw whatever error you want.
Q: I keep getting an IndexError, why? A: If you're going to type check the function you must provide Waycheck with the expected type for ALL the arguments, not just some of them.
Changelog
April 2022:
- Initial Release - v0.0.1
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
File details
Details for the file waycheck-0.0.1.tar.gz
.
File metadata
- Download URL: waycheck-0.0.1.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 947e6e243d8ea9e0ec226e4f33ada0ace8b12af1ff0db9bb03adf46d7484e824 |
|
MD5 | cf8b28aed329d112737ef3858e7f41ac |
|
BLAKE2b-256 | 0629503001782054b91f68f848536ee6d0cf59ac15a538184314d7194253763c |