Typeable: Type Casting Simplified

Release v0.7. (Installation)

Typeable is a concise yet expressive Python type casting library.


>>> data = {'a': ["0", True, 2, 3.14], 'b': range(4)}
>>> DataType = dict[str,list[typing.Union[bool,float,int]]]
>>> typeable.cast(DataType, data)
{'a': [False, True, 2, 3.14], 'b': [0, 1, 2, 3]}

>>> @typeable.cast.function
... def my_function(d: DataType):
...     return d
>>> my_function(data)
{'a': [False, True, 2, 3.14], 'b': [0, 1, 2, 3]}

>>> with typeable.declare('Json') as _J:
...     Json = typing.Union[float, bool, int, str, None, dict[str, _J], list[_J]]
>>> json.dumps(typeable.cast(Json, data))
'{"a": ["0", true, 2, 3.14], "b": [0, 1, 2, 3]}'

>>> class X(typeable.Object):
...     a: list[bool]
...     b: list[int]
>>> ctx = typeable.Context()
>>> with ctx.capture() as error:
...     x = X(data, ctx=ctx)
Traceback (most recent call last):
    ...
TypeError: No implementation found for 'bool' from float
>>> error.location
('a', 3)

Typeable implements runtime type casting using Python’s annotation syntax. Approaches from a practical point of view, and loves simplicity.