Skip to main content

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

Metadata

Release files for waycheck 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for waycheck 0.0.1
File Size Uploaded
waycheck-0.0.1.tar.gz 3.3 kB Details

Release files / waycheck-0.0.1.tar.gz

Download URL waycheck-0.0.1.tar.gz
Size 3.3 kB
Tags Source
SHA-256 checksum
How to use checksums
947e6e243d8ea9e0ec226e4f33ada0ace8b12af1ff0db9bb03adf46d7484e824
BLAKE2b-256 checksum
How to use checksums
0629503001782054b91f68f848536ee6d0cf59ac15a538184314d7194253763c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.10

Release history Release notifications | RSS feed

This release

0.0.1 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page