runtime-type-checker
This package performs type-check at runtime with help of type annotations.
How to use this package
There are two ways to perform type checks using this package.
I provide a few simple examples here. For a complete overview, have a look at the package's unit tests.
1- the check_type function
You can check an object against a type or an annotation via the check_type function.
The function returns None if the check was successful or raises a TypeError in case of error.
Note that this function does not check recursively for e.g. the attributes of a class.
from typing import List, Sequence, Optional, Mapping
from dataclasses import dataclass
from runtime_type_checker import check_type
check_type("a", str) # OK
check_type(["a"], List[str]) # OK
check_type(["a", 1], Sequence[str]) # raises TypeError
@dataclass
class Foo:
a: int
b: Optional[Mapping[str, int]] = None
check_type(Foo(1), Foo) # OK
check_type(Foo(1), int) # raises TypeError
2- The check_types decorator
You can also type-check classes upon instance creation and functions or methods upon call through the check_types
decorator:
from typing import Optional, Mapping
from dataclasses import dataclass
from runtime_type_checker import check_types
def run_typed(f):
return check_types(dataclass(f))
@check_types
@dataclass
class Foo:
a: int
b: Optional[Mapping[str, int]] = None
Foo(1) # returns an instance of foo
Foo(0, {"a": "b"}) # raises TypeError
@check_types
def bar(a: bool, **options: str) -> str:
return options.get("b", "missing") if a else "unknown"
bar(True, b="1") # returns "1"
bar(True, c=1) # raises TypeError
Package features and short-comings
1- Features
- simplicity: there's only one function and one decorator to keep in mind.
- robustness: this package relies on the
typing-inspectfor the heavy lifting. This package is maintained by core contributors to the typing module, which means very little hacks on my side to work with older versions of python.
2- Short-comings
- coverage: I don't offer coverage for all features of type annotations: for example Protocol, Generators, IO are not currently supported. Generics are not really well handled.
Metadata
Release files for runtime-type-checker 0.6.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| runtime_type_checker-0.6.0-py3-none-any.whl | Python 3 | none | any | Details for runtime_type_checker-0.6.0-py3-none-any.whl |
Release files / runtime_type_checker-0.6.0-py3-none-any.whl
| Download URL | runtime_type_checker-0.6.0-py3-none-any.whl |
|---|---|
| Size | 11.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
61cf3f03d7e9abe85564027192fdbd9e4e68c2fc1dc0f5693c25aa8f108f73e1
|
|
BLAKE2b-256 checksum How to use checksums |
e390abe822e4c7562293900836fe2559597810eddf87d15d083a3da0b49a40d9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.9.6
|
Security for runtime-type-checker 0.6.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.