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_check import type_check
assert type_check("A string", str)
As a decorator:
from simple_type_check import type_check, TypeCheckFailed
@type_check.returns
def id_(x) -> int:
return x
_ = id_(1) # Passes
try:
_ = id_("string") # Raises
except TypeCheckFailed as e:
pass
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_check import TypeChecker
class Foo:
pass
type_check = TypeChecker(Foo)
assert type_check(Foo(), Foo)
Special type checking logic desired:
from simple_type_check import TopLevelCheck, TypeChecker
Bar = list[int] | list["Bar"]
class BarCheck(TopLevelCheck):
def __call__(self, obj, type_) -> bool:
return type_ == "Bar" and self._recurse(obj, Bar)
type_check = TypeChecker(advanced=[BarCheck])
type_check([1, [2], [[3]]], Bar) # Should pass
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.2.0.tar.gz
(19.1 kB
view details)
Built Distribution
File details
Details for the file simple_type_checker-2.2.0.tar.gz
.
File metadata
- Download URL: simple_type_checker-2.2.0.tar.gz
- Upload date:
- Size: 19.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca3c6aed04ef6fdb867b4b063a866fd9bc3e9605c0d9049cf0b064153520881e |
|
MD5 | 03044f2ab29c104d1881787f7e694250 |
|
BLAKE2b-256 | d938d4482cbabac91924eed00d3225906d1c152e11c10aa6eb1d95f916abbdff |
File details
Details for the file simple_type_checker-2.2.0-py3-none-any.whl
.
File metadata
- Download URL: simple_type_checker-2.2.0-py3-none-any.whl
- Upload date:
- Size: 20.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a02d7531add1079d9415ab9487448df82ae5da40b6104a6d6d4f492e898e7253 |
|
MD5 | 0a656ae2f2725aa08cc15b7c6cdab69b |
|
BLAKE2b-256 | 8aeeda9c7f35cffad137a742cab69a74b99fd58d123943ce2fe0024655315a30 |