Skip to content

Simple decorators to 'force' parameter types in python for functions/coroutines

Notifications You must be signed in to change notification settings

PogoDigitalism/TypeForcer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 

Repository files navigation

TypeForcer

Simple copy-paste decorators to 'force' types for function parameters in Python with only type hinting!

Comes with GenericAlias support! (nested type hints in Iterables such as list[int])

To be changed:

  • Move up in typing abstraction layers (checking for types inherited ABCs and not direct type matching). Better support for custom types

To be added:

  • tuple (Ellipsis) support
  • Iterator (no Exhaustion) support

Its easy to use:

from force_types import *
# force types supports ONE level of type hint nesting: -> list[str | int]
# does NOT support: -> list[dict[str, int]] (TWO level hint nesting)


@force_types # when stacking decorators, force_type must be applied as last (bottom)
def your_function(foo: list[str | int], bar: int) -> None:
    pass


@async_force_types # when stacking decorators, async_force_type must be applied as last (bottom)
async def async_your_function(foo: str, bar: int) -> None:
   pass
your_function(['hi', 5.0], bar=5) # will throw a TypeError (due to invalid passed list (float instead of int))

>>> TypeError: ['hi', 5.0] -> Invalid type <class 'list'> for argument "foo" with hinted type list[str | int]

About

Simple decorators to 'force' parameter types in python for functions/coroutines

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages