Type-Strict
Python's missing runtime type checker
Installation
pip install type-strict
Usage
from type_strict import strict
@strict
def person_age(name: str, age: int) -> str
return f"{name} is {age}"
# If the values mismatch the types, a TypeError is thrown.
person_age(...)
Use Cases
If you ever find yourself asserting all your types are correct, for example...
def func(person: Person, employee: Employee):
if not isinstance(person, Person):
raise TypeError("Expected person to be of type Person")
if not isinstance(employee, Employee):
raise TypeError("Expected employee to be of type Employee")
...
this library can simplify the same functionality into the following:
@strict
def func(person: Person, employee: Employee):
...
Features
- Python 3.9+ support.
- Works with the
typinglibrary and default/custom Python types. - Checks embedded types recursively. (Eg
Dict[str, List[int]].) - Checks return type if specified.
- Works with any combination of args, kwargs, and defaults.
- Informative error messages.
- In the event of a failure, type-strict swallows the error to avoid user impact.
Limitations
- Performance: Type checking is done at runtime. So, there is a performance impact—especially if the value has many members, for example, a long list. For static type checking consider mypy.
- Ignores variable-length args (varargs).
Examples
Wrong value.
@strict
def my_func(arg1: List[int]):
...
# Raises:
# TypeError('Value (whoops!) in "my_func(arg1=typing.List[int])" is not of type int')
my_func([1, 2, "whoops!", 3])
Wrong data structure.
@strict
def my_func(arg1: Dict[str, int]):
...
# Raises:
# TypeError('Expected type typing.Dict[str, int] in "my_func(arg1=typing.Dict[str, int])" got list')
my_func([1, 2, 3])
Wrong return type.
@strict
def my_func() -> int:
return "one"
# Raises:
# TypeError('Return value (one) in "my_func() -> int" is not of type int')
my_func()
Metadata
Release files for type-strict 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| type-strict-0.1.0.tar.gz | 4.5 kB | Without provenance | Details for type-strict-0.1.0.tar.gz |
Release files / type-strict-0.1.0.tar.gz
| Download URL | type-strict-0.1.0.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
611b89b5d1bcb8c0a3f1554a13338b9f4b262f484eb58991613cddcc25b04bf8
|
|
BLAKE2b-256 checksum How to use checksums |
47ca0e23bb9acf5be0a713ac3aabb50e1a9d518e54d165df49171c8af87202a6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.12
|
Security for type-strict 0.1.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.