Enforce Typehints at Runtime
Project description
Typestrong
Aggressive Typehints
With this decorator, at runtime a function's arguments are compared against the typehints
and if they don't match, an excpetion is thrown.
This essentially makes your function arguments dynamically typed.
This is not the same as static analysis like mypy, which trusts you'll pass in values to match the typehints.
Install
pip install typestrong
Import
from Typestrong import typsestrict
Use
Simply attach
@typestrong
def addition(val1: int, val2: int):
print(val1 + val2)
To any function.
Now if you pass in an argument during runtime it's assessed against the typehints
and if they don't match up an exception is thrown.
For example if I call that above function with
addition('test', 1)
I will get the exception
Argument "arg3" in function "examplefunc" expected value of type "<class 'list'>", got "<class 'int'>"
run tests with
pytest Test_Typestrong.py -v
Usage with init and classes
Don't attach the decorator to a class, attach it to the init function if required.
For instance methods it assumes that self is the first argument in the function defintion. This follows PEP standards.
Example
class ExampleClass:
@typestrong
def __init__(self, var1: str):
self.var1 = var1
@typestrong
def examplefunc(self), val2: str:
return(f'example value {val2}')
In this example the init function is now strictly enforcing the typehints, and so is the examplefunc
Subclasses
Yes it recognises a subclass being passed into a function in-place of a typehinted parent class, and will consider this a matching argument. (No exception)
class ParentClass:
class ChildClass(ParentClass)
@typestrong
def exampleFunc(var1: ParentClass):
print('in here')
exampleFunc will work if ppased a ParentClass instance of a childClass instance
Literals
I'm a big fan of typing.Literal, it allows you to pass in a list of possible values,
Typestrong will ensure the incoming value is found within the Literal options. So
@typestrong
def exampleFunc(var1: Literal['Cat', 'Dog', 'Fish']):
print('var1')
Will work if passed a 'Cat' value, but not if passed a 'Cow' or integer.
Unions
Unions are honoured as well, so if you write
@typestrong
def exampleFunc(var1: (str | int)):
pass
It will check that the incoming value matches one of those.
The typechecking makes a recursive call, so any value it can check outside a union it is able to check inside one.
Project details
Release history Release notifications | RSS feed
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 typestrong-1.0.0.tar.gz.
File metadata
- Download URL: typestrong-1.0.0.tar.gz
- Upload date:
- Size: 12.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
606d743a34d0cc9c2d5561d94d46fe3ac6562005e76caff7055477aea3f15109
|
|
| MD5 |
ab7da19883168af14b4b6445a7b27f80
|
|
| BLAKE2b-256 |
c529166c648782517ec3f0fe47c77530f8085d1564579a39828cf4a47144bbd0
|
File details
Details for the file typestrong-1.0.0-py3-none-any.whl.
File metadata
- Download URL: typestrong-1.0.0-py3-none-any.whl
- Upload date:
- Size: 12.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe58c7983f562bfb597978dd92bc3018e65f60ace0001073cd073fdc9d4e296e
|
|
| MD5 |
77374b6cbc3644d311b297a1f0d8e453
|
|
| BLAKE2b-256 |
2a46b68aa7dcf4b8d245ccdb0b94475ec810bda44729c04786b6290a810198db
|