Decorators that allow for automatic type-casting of arguments for a function or strict enforcement of type hints.
Project description
hintcast
Decorators that allow for automatic type-casting of arguments for a function or strict enforcement of type hints.
Usage Summary
Import whatever decorators you want with an import statement like from hintcast import cast_hints, strict_hints
.
Casting Types
- To add automated type-casting to your function/method, use the
@cast_hints
decorator for your method. If a type cannot be cast, a warning will be logged. - To attempt to cast but raise a TypeError exception if unable to, pass the keyword argument "strict". E.g.
@cast_hints(strict=True)
. - To pass
None
values without casting, pass the keyword argument "cast_none". E.g.@cast_hints(cast_none=False)
.
Enforcing strict types
- You can also use the
@strict_hints
decorator. This will NOT attempt to cast the types, but will raise a ValueError if a type not matching your hint is passed.
Usage Examples
Casting Types
from hintcast import cast_hints
@cast_hints
def add_two(num: int, text: str):
print(repr(num+2))
print(repr(text+"2"))
Output
>>> add_two("3", 3)
5
'32'
>>> add_two("3", None)
5
'None2'
Casting types without casting NoneType
from hintcast import cast_hints
@cast_hints(cast_none=False)
def add_two(num: int, text: str):
print(repr(num+2))
print(repr(text+"2"))
Output
>>> add_two("3", 3)
5
'32'
>>> add_two("3", None)
5
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
Casting types and raising TypeError when type could not be cast
from hintcast import cast_hints
@cast_hints(strict=True)
def add_two(num: int, text: str):
print(repr(num+2))
print(repr(tex+"2"))
Output
>>> add_two("3", 3)
5
'32'
>>> add_two("test", 3)
TypeError: Could not convert num to <class 'int'>. invalid literal for int() with base 10: 'test'
Enforcing strict typing without casting
from hintcast import strict_hints
@strict_hints
def add_two(num: int, text: str):
print(repr(num+2))
print(repr(text+"2"))
Output
>>> add_two("3", 3)
TypeError: num is not of type <class 'int'>
>>> add_two(3, 3)
TypeError: text is not of type <class 'str'>
Future Development Plans
- Test support for all primitive types
- Add support for checking function return type hints
- Add support for built-in non-primitive data structures such as arrays, lists, etc.
- Add support for hinting of types within lists, dictionaries, etc.
- Add ability to enforce typing project-wide
When all of the functionality above has been implemented and tested, the version number will be incremented to 1.0.0 and this project will leave beta status.
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
hintcast-0.1.0.tar.gz
(3.9 kB
view details)
Built Distribution
File details
Details for the file hintcast-0.1.0.tar.gz
.
File metadata
- Download URL: hintcast-0.1.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f345fcdcc0d81b3222f9de0598d2421a4af182942b1f2b6a5726a933b104c4f1 |
|
MD5 | 194412ede78054a546b09e89daf3034d |
|
BLAKE2b-256 | bf4310887a3d78fc08c643a0e3e6e76a2e7232ddebf64f23cf2bbc55841bcb0d |
File details
Details for the file hintcast-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: hintcast-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57bccbd67d0d368d2974a619e9cd8eedfbbc1422a9e544349e787916ad3dca72 |
|
MD5 | c0d06c25882182d061b336f375fcd625 |
|
BLAKE2b-256 | e4fc4e333b15069e462749aa091af0d1aae444eb73aa450c0d8f0363fa25171a |