typedargs.metadata module

The basic class that is used to store metadata about a function.

Summary

Classes:

AnnotatedMetadata All of the associated metadata for an annotated function or class.

Reference

class typedargs.metadata.AnnotatedMetadata(func, name=None)[source]

Bases: object

All of the associated metadata for an annotated function or class.

Parameters:
  • func (callable) – The function that we are annotated so that we can pull out relevant argument spec information. If func is a class, we pull arguments from its __init__ routine
  • name (str) – An optional name that will overwrite the default name of this function
spec_filled(pos_args, kw_args)[source]

Check if we have enough arguments to call this function.

Parameters:
  • pos_args (list) – A list of all the positional values we have.
  • kw_args (dict) – A dict of all of the keyword args we have.
Returns:

True if we have a filled spec, False otherwise.

Return type:

bool

add_param(name, type_class, type_name, validators, desc=None)[source]

Add type information for a parameter by name.

Parameters:
  • name (str) – The name of the parameter we wish to annotate
  • type_class (type) – Parameter type class
  • type_name (str) – The name of the parameter’s type
  • validators (list) – A list of either strings or n tuples that each specify a validator defined for type_name. If a string is passed, the validator is invoked with no extra arguments. If a tuple is passed, the validator will be invoked with the extra arguments.
  • desc (str) – Optional parameter description.
typed_returnvalue(type_name, formatter=None)[source]

Add type information to the return value of this function.

Parameters:
  • type_name (str) – The name of the type of the return value.
  • formatter (str) – An optional name of a formatting function specified for the type given in type_name.
string_returnvalue()[source]

Mark the return value as data that should be converted with str.

custom_returnvalue(printer, desc=None)[source]

Use a custom function to print the return value.

Parameters:
  • printer (callable) – A function that should take in the return value and convert it to a string.
  • desc (str) – An optional description of the return value.
has_varargs()[source]

Check if this function supports variable arguments.

has_kwargs()[source]

Check if this function supports arbitrary keyword arguments.

returns_data()[source]

Check if this function returns data.

match_shortname(name, filled_args=None)[source]

Try to convert a prefix into a parameter name.

If the result could be ambiguous or there is no matching parameter, throw an ArgumentError

Parameters:
  • name (str) – A prefix for a parameter name
  • filled_args (list) – A list of filled positional arguments that will be removed from consideration.
Returns:

The full matching parameter name

Return type:

str

param_type(name: str) → Union[type, str, None][source]

Get the parameter type information by name.

Parameters:name (str) – The full name of a parameter.
Returns:The type name or None if no type information is given.
Return type:str
signature(name=None)[source]

Return our function signature as a string.

By default this function uses the annotated name of the function however if you need to override that with a custom name you can pass name=<custom name>

Parameters:name (str) – Optional name to override the default name given in the function signature.
Returns:The formatted function signature
Return type:str
format_returnvalue(value)[source]

Format the return value of this function as a string.

Parameters:value (object) – The return value that we are supposed to format.
Returns:
The formatted return value, or None if this function indicates
that it does not return data
Return type:str
convert_positional_argument(index, arg_value)[source]

Convert and validate a positional argument.

Parameters:
  • index (int) – The positional index of the argument
  • arg_value (object) – The value to convert and validate
Returns:

The converted value.

Return type:

object

check_spec(pos_args, kwargs=None)[source]

Check if there are any missing or duplicate arguments.

Parameters:
  • pos_args (list) – A list of arguments that will be passed as positional arguments.
  • kwargs (dict) – A dictionary of the keyword arguments that will be passed.
Returns:

A dictionary of argument name to argument value, pulled from either

the value passed or the default value if no argument is passed.

Return type:

dict

Raises:
  • ArgumentError – If a positional or keyword argument does not fit in the spec.
  • ValidationError – If an argument is passed twice.
convert_argument(arg_name, arg_value)[source]

Given a parameter with type information, convert and validate it.

Parameters:
  • arg_name (str) – The name of the argument to convert and validate
  • arg_value (object) – The value to convert and validate
Returns:

The converted value.

Return type:

object