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_check.returns
def id_(x) -> int:
return x
_ = id_(1) # Passes
try:
id_("string")
except TypeCheckFailed as e:
print("Bad input: ", e)
Decorators for aruments exist as well: type_check.args
. Both can be done at once with type_check.decorate
.
With custom types
Basic types where isinstance
is sufficient:
from simple_type_checker import TypeChecker
class Foo:
pass
type_check = TypeChecker(Foo)
assert type_check(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_check(Bad(True), Bar)
assert not type_check(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-2.1.0.tar.gz
(18.9 kB
view hashes)
Built Distribution
Close
Hashes for simple_type_checker-2.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b1691760099f4f17909677ad893faaa6f3e64cb274f83765ff7718c2bf3abd8 |
|
MD5 | aef0363acd71922b431f08f880d44488 |
|
BLAKE2b-256 | 69685560aae1bad0b3a2a1da3b20c19247d4d1fa7964fde0d687496584e10fd4 |
Close
Hashes for simple_type_checker-2.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 784a3ed889efd13c192d5cede8554e708ec21bd0b32866012cc6f41bbd0e3f74 |
|
MD5 | 4292e23be371da2b7ed7402cd22ec4b2 |
|
BLAKE2b-256 | 5da50300b37773ac500f38a9b552265af975993d2f393b9db04c2b2404faf488 |