A type-checking utility for Python functions
Project description
Typeca
Typeca is a Python decorator for enforcing type checks on both positional and keyword arguments on functions with annotated types.
It ensures that arguments passed to functions and the function's return value match their specified types, raising a TypeError if any type mismatch is found.
P.S. Anyway, this decorator would negatively affect a function`s performance, so the best approach would be to use it during development and testing phases.
%timeit -n 10 -r 7 gen_array(1_000_000)
48 ms ± 1.36 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
%timeit -n 10 -r 7 gen_array_type_enforced(1_000_000)
424 ms ± 34.2 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
Supported Python Versions
- Python 3.10 and later.
Features
- Flexible Enforcement: Skips type checking for arguments without annotations.
- Nested Annotation Check: The decorator supports recursive type checking for nested data structures.
- Configurable Cache Size: Uses a cache to store function signatures, with a configurable maxsize parameter (default is 64).
- Enable/Disable Type Checking: Users can enable or disable type enforcement on a function by using the enable parameter, defaults to True.
- Error Handling: Raises a TypeError if a type mismatch is detected for either function args or the return value.
Supported Types
- Standard Types: Such as int, str, float, bool, and other built-in types.
- Annotated Data Structures:
- list[T]: Checks that the value is a list and that every element conforms to type T.
- dict[K, V]: Checks that the value is a dictionary, and that each key has type K and each value has type V.
- tuple[T1, T2, ...]: Checks that the value is a tuple, and that each element has specified type (e.g., tuple[int, str] for (41, 'Saturday')).
- set[T]: Checks that the value is a set and that every element conforms to type T.
- frozenset[T]: Checks that the value is a frozenset and that every element conforms to type T.
- Type Combinations:
- Union[T1, T2, ...]: Checks if the value matches one of the types in the Union, e.g., Union[int, str] would accept both int and str. (Supports both traditional Union from typing and the new | syntax introduced in Python 3.10)
- Optional[T]: Equivalent to Union[T, None], checks that the value is either None or matches type T.
Installation
pip install typeca
Usage
Use @type_enforcer to enforce type checks on your functions:
from typeca import type_enforcer
@type_enforcer()
def two_num_product(a: int, b: int) -> int:
return a * b
two_num_product(2, 3) # Output: 6
two_num_product(2, '3') # Raises TypeError
Examples
Example 1: Simple Type Enforcement
@type_enforcer()
def add(a: int, b: int) -> int:
return a + b
add(3, 4) # Works fine
add(3, '4') # Raises TypeError
Example 2: Complex Data Structures
Supports lists, dictionaries, and tuples with type annotations:
@type_enforcer()
def process_items(items: list[int]) -> list[int]:
return [item * 2 for item in items]
process_items([1, 2, 3]) # Works fine
process_items(['a', 'b', 'c']) # Raises TypeError
Example 3: Disable Type Enforcement
At any moment you can disable check to improve performance of the function:
@type_enforcer(enable=False)
def process_array(*args) -> list[int]:
return list(args) * 2
process_array(1, 2, 3) # Works without type enforcement
Example 4: Custom Cache Size
The type_enforcer decorator will cache up to 128 unique signatures for process_data:
@type_enforcer(maxsize=128)
def process_data(x: int, y: int) -> int:
return x + y
process_data(5, 10) # Type enforcement applies, returns 15
process_data("5", "10") # Raises TypeError
Example 5: Using frozenset
@type_enforcer()
def unique_numbers(data: frozenset[int]) -> frozenset[int]:
return frozenset([n * 2 for n in data])
unique_numbers(frozenset({1, 2, 3})) # Returns: frozenset({2, 4, 6})
unique_numbers(frozenset({1, 2, "3"})) # Raises TypeError
Example 6: Using Union with | syntax
@type_enforcer()
def process_value(value: int | str) -> str:
return f"Processed: {value}"
process_value(42) # Returns: "Processed: 42"
process_value("hello") # Returns: "Processed: hello"
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file typeca-1.0.1.tar.gz.
File metadata
- Download URL: typeca-1.0.1.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d90144a9838481ed4a8ee65bd8f9f1724672f05033bbb1d635bb3c8b6829edd2
|
|
| MD5 |
14758dbaede163516de01a45eb3a3dd2
|
|
| BLAKE2b-256 |
5a49c2e8817314db8483cbc1c6a28b16434e7818104365fd16ab9ac60260c26f
|
File details
Details for the file typeca-1.0.1-py3-none-any.whl.
File metadata
- Download URL: typeca-1.0.1-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ed9f39bdf4a2bde11910e516262be5db11e0cd1a9a14ac035714f172250e793
|
|
| MD5 |
e2cb236769b89be66a4aa66578b24d2c
|
|
| BLAKE2b-256 |
a31174225e1ad342e346408c5198eded7c9df72f0cd72ca056720ed776dbf5b3
|