anodi
¶
-
annotated
(func=None, returns=<class 'funcsigs._empty'>)[source]¶ Decorator to treat
func
‘s default args as a combination of annotations and default values, migrating the annotations tofunc.__annotations__
, leaving only the defaults in__defaults__
).The optional
returns
keyword parameter is placed in the resulting__annotations__
dict.Each default value must be a tuple,
(annotation, default)
. To supply an unannotated parameter with a default value, use theempty
marker object. To supply an annotation without a default value, use a 1-tuple:(annotation,)
.Note that the Python 2.x rules prohibiting non-default parameters from coming after defaults still apply, but we don’t enforce those rules. The effect of using the
(annotation,)
form after using the(annotation, default)
form is likely to be surprising, at best.You may specify an unannotated parameter by using an empty tuple as its default value. This is to allow placing unannotated parameters after annotated parameters. Ordinarily, this would not be allowed, since the annotated parameter would mark the start of default values, requiring defaults on all subsequent parameters.
We do not support nested tuple parameters.
We also don’t yet have a way to add annotations to the
*args
or**kwargs
catch-all parameters, since they don’t take defaults.Example:
>>> from anodi import annotated, empty >>> @annotated ... def example (a, b, c=(int,), d=(), e=(empty, "hi")): ... pass ... >>> example.__annotations__ {'c': <type 'int'>} >>> example.__defaults__ ('hi',)
>>> @annotated(returns=int) ... def example (a, b, c=(int,), d=(), e=(empty, "hi")): ... pass ... >>> example.__annotations__ {'c': <type 'int'>, 'return': <type 'int'>} >>> example.__defaults__ ('hi',)