Skip to main content

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.

Metadata

Release files for hintcast 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hintcast 0.1.0
File Size Uploaded Provenance
hintcast-0.1.0.tar.gz 3.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for hintcast 0.1.0
File Interpreter ABI Platform
hintcast-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.8 kB

Release files / hintcast-0.1.0.tar.gz

Download URL hintcast-0.1.0.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f345fcdcc0d81b3222f9de0598d2421a4af182942b1f2b6a5726a933b104c4f1
BLAKE2b-256 checksum
How to use checksums
bf4310887a3d78fc08c643a0e3e6e76a2e7232ddebf64f23cf2bbc55841bcb0d
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / hintcast-0.1.0-py3-none-any.whl

Download URL hintcast-0.1.0-py3-none-any.whl
Size 3.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
57bccbd67d0d368d2974a619e9cd8eedfbbc1422a9e544349e787916ad3dca72
BLAKE2b-256 checksum
How to use checksums
e4fc4e333b15069e462749aa091af0d1aae444eb73aa450c0d8f0363fa25171a
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for hintcast 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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page