3 ms·
I've added a type checking decorator for function annotations to my assertions helper library, https://github.com/kislyuk/ensure https://github.com/kislyuk/ensu
by ak217 12y ago
I've added a type checking decorator for function annotations to my assertions helper library, https://github.com/kislyuk/ensure https://github.com/kislyuk/ensure:
from ensure import ensure_annotations
@ensure_annotations
def f(x: int, y: float) -> float:
return x+y
f(1, 2.3)
>>> 3.3
f(1, 2)
>>> ensure.EnsureError: Argument y to <function f at 0x109b7c710> does not match annotation type <class 'float'>
It's useful sometimes.
- thomasahle 12y agoHow does that work? Is it abusing dictionary syntax without braces?
- ak217 12y agoThe PEP describes it nicely :) I'm not a huge fan of the "->" syntax myself, but I'm not sure what would work better.