A small extendable python type checker library
Project description
simple_type_checker
A small extendable python type checker library
Basic Usage
Checking a type:
from simple_type_checker import type_check
assert type_check("A string", str)
As a decorator:
from simple_type_checker import type_check, TypeCheckFailed
@type_checker.returns(int)
def id_(x):
return x
_ = id_(1) # Passes
try:
id_("string")
except TypeCheckFailed as e:
print("Bad input: ", e)
With custom types
Basic types where isinstance
is sufficient:
from simple_type_checker import TypeChecker
class Foo:
pass
type_check = TypeChecker(Foo)
assert type_checker(Foo(), Foo)
Special type checking logic desired
from simple_type_checker import TypeChecker, Checker
class Bar:
def __init__(self, b):
self.can_check = b
class CheckerBar(Checker):
def __call__(self, obj: Any, type_: Any) -> bool:
return type_ == Bar and isinstance(obj, Bar) and obj.can_check
type_check = TypeChecker(advanced=(Bar,))
assert type_checker(Bad(True), Bar)
assert not type_checker(Bad(False), Bar)
This use case is useful for container types that take arguments, for example.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
simple_type_checker-1.0.0.tar.gz
(17.5 kB
view hashes)
Built Distribution
Close
Hashes for simple_type_checker-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2effe6db7a9115513920f9d55e7534f7f6d3cf31b26dd51b21f31aebbed4bdc6 |
|
MD5 | b36c290c03875f8422a692a39c9e05a6 |
|
BLAKE2b-256 | d3339d808bd7e39e2c2720e063d3cd1068703d12691d6462185d54fb7ca196ab |
Close
Hashes for simple_type_checker-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e2e8aedab144196e06009c06f2b9d5aea40485dcca49c957afa3d1909af6fb3 |
|
MD5 | 15357e6f97fc7c5f0a3b0c1a3eaefc30 |
|
BLAKE2b-256 | 76e271969784f23fa5a596e47f048029383e290324e11af2512f35aedba243ea |