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 to func.__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 the empty 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',)
returns(annotation)[source]

Decorator to add annotation to func‘s return annotation, as though it were a Python 3 -> ... annotation.

>>> from anodi import returns
>>> @returns(int)
... def example ():
...    pass
...
>>> example.__annotations__
{'return': <type 'int'>}

anodi.tools

class TypeName(typeobj)[source]

Expose a __repr__ around typeobj to return a (best-effort) “meaningful” name (e.g., for writing function signatures).

document(func)[source]

Decorator to insert an annotated function signature into the docstring.

typename(t)[source]

Caching wrapper around TypeName.