10.6. typped.pratt_types

This module contains classes defining types and methods which can be called by the PrattParser class when checking types. It defines the class TypeSig and the class TypeObject, as well as a dict class for storing parameterized types.

All checking of type equivalence has been abstracted into this module, via utility routines (often static methods of classes).

Terminology:

  • Function parameters or formal arguments are the identifiers which appear in the function definition (and the function signature). The type specifications for parameters will be called formal types, since the term parameter is used differently below.

    The original_sig of a construct is the TypeSig of formal arguments.

    The expanded_sig of a construct is the original_sig expanded to the correct number of formal arguments (the first step of type matching).

  • Function arguments or actual arguments are the values which are actually passed to the function on a function call. These are resolved at parse-time. Their types will be referred to as actual types.

    The actual_sig of a construct is the TypeSig of actual types, resolved during parsing.

Note that the formal type of a parameter can possibly match more than one actual type. At least one must match, however.

The each instance of the PrattParser class holds all of its defined types in a TypeTable class, defined in this module.

class typped.pratt_types.Varargs(*args, **kwargs)[source]

Bases: object

This class is used to define type signatures which can check a variable number of arguments. When used at the end of an argument list for a TypeSig instance it will repeat any arguments inside it as many times as necessary.

If exact_repeat is true (the default) then an exception will be raised if no multiple of the repeated arguments matches the actual arguments. Otherwise the arguments will be truncated to fit.

class typped.pratt_types.TypeSig(val_type=None, arg_types=None)[source]

Bases: object

The type specification for a function. Generally set at function definition. Can also be used for actual types, since it is just a container. The “functions” themselves can be any syntactic construct that produces a node in the final parse tree, with of the node representing the arguments.

A TypeSig instance is essentially just a tuple (val_type, arg_types), where val_type is a TypeObjectSubclass and the arg_types is a tuple of them. Using a separate class instead of a tuple allows for additional information to be stored with the data and produces better error messages. The class also provides a convenient place to localize some routines which operate on type signatures and lists of type signatures.Properties are used automatically convert wildcard None arguments to the corresponding TypeObject instances in the attributes val_type and arg_types.

For the purposes of equality comparison these objects are equivalent to the tuple form. Equality is exact equality and does not hold for formal signatures and their corresponding actual signatures. For that, use matches_formal_sig. Equality also ignores any attributes (other than val_type and arg_types) which might be added to or modified in a TypeSig instance.

Note that None is a wildcard which matches any type argument, and None for the arg_types list or tuple matches any arguments and any number of arguments (it is expanded during parsing to as many None arguments as are required). Note that TypeSig() == TypeSig(None) == TypeSig(None, None). To specify an object like a literal which takes no arguments an empty list or tuple should be used for arg_types, as in TypeSig(None, []); the val_type argument can be set to the desired type if it is typed.

A single TypeObject as an arg_types argument (i.e., not an iterable) is expanded to be a tuple of that type of objects, of any required length. For example, a function might take an arbitrary number of arguments but they must all have Int type.

The values type and the argument types can be accessed by indexing the 0 and 1 element of an instance, respectively, or by using the val_type and arg_types attributes.

val_type

Return the val_type property saved in _val_type.

arg_types

Return the arg_types property saved in _arg_types.

convert_val_type_wildcards(val_type)[source]

Convert val_type argument to a TypeObject instance and set attribute.

convert_arg_types_wildcards(arg_types)[source]

Convert all arg_types arguments to TypeObject instances and return it.

static get_all_matching_expanded_sigs(sig_list, list_of_child_sig_lists, tnode=None, raise_err_on_empty=True)[source]

Return the list of all the signatures on sig_list whose arguments match some choice of child/argument signatures from list_of_child_sig_lists Note that this does not look at the return types, and returns all signatures with matching arguments.

This is the main method of this class which is actually called from the PrattParser to find all the argument-matching signatures. (If overloading on return types is used then PrattParser also calls get_child_sigs_matching_return_arg_type on the second pass, to check return matches.)

The sig_list argument should be a list of TypeSig instances representing formal types.

The list_of_child_sig_lists argument should be a list of lists, where each sublist is a list of all the possible signatures for a child node. The sublists should be in the same order as the children/arguments.

The returned list is a list of expanded formal type signatures (i.e., with None arguments expanded and with a fixed number of arguments). The original, unexpanded formal signature which matched is saved as an attribute original_formal_sig of each matching expanded formal signature which is returned.

static get_sigs_expanded_for_num_actual_args(num_args, sig_list)[source]

Expand the signatures on list sig_list so that any None argument not inside a tuple or list is converted to a tuple of None having num_args of argument.

This routine also expands argument signatures consisting of a single TypeObject to take any number of arguments of that type.

Each expanded version has the original, unexpanded signature saved with it as an attribute called original_formal_sig.

static get_sigs_matching_num_args(num_args, sig_list, tnode=None, raise_err_on_empty=True)[source]

Return a list of signatures from sig_list which match num_args as the number of arguments. The optional token-tree node tnode is only used for improved error reporting.

This routine assumes that a formal_sig attribute has been set for all passed-in signatures (for example, as set by the method expand_sigs_with_None_args.

static filter_sigs_matching_child_types(sig_list, list_of_child_sig_lists, tnode=None, raise_err_on_empty=True)[source]

Return a list of all signatures in sig_list which have arguments that match (in order) the return type of some child signature in the corresponding sublist of list_of_child_sig_lists. The latter should be a list containing lists of all the expanded signatures for each child, in corresponding order.

The number of arguments is assumed to already match (see get_sigs_matching_num_args). This just filters the list, removing the non-matches.

The optional token-tree node tnode is only used for improved error reporting.

static get_child_sigs_matching_return_arg_type(child, return_type, matching_sigs)[source]

Called with a child node, the expected return type for that child, and a list of matching sigs for the child. Returns all the child.matching_sigs which also match in return type.

This is used in pass two of the type-checking. When called, the matching_sigs parameter is passed the child.matching_sigs attribute which was already set on pass one of the checking. If the result is unique the return type of the child is known, and so the full signature can be resolved and assigned.

static append_sig_to_list_replacing_if_identical(sig_list, sig)[source]

Return sig_list either with sig appended or with any identical signature (according only to type equality, not attributes) replaced if one is there. This is called in register_handler_funs.

matches_formal_sig(formal_sig)[source]

Test if this signature as an actual signature matches formal_sig as a formal signature. Note the difference between this and equality! This is the one which should be called to determine signature equivalence based on type equivalences and possible conversions.

NOT CURRENTLY USED, low-level matches_formal_type is used instead.

typped.pratt_types.actual_matches_formal_default(actual_type, formal_type)[source]

The default function to check whether actual types match formal types. Currently uses straight equality, nothing else. Users should not call this directly, but instead call the TypeObject method matches_formal_type on the actual type. Users may set their own versions of this function.

class typped.pratt_types.TypeObject(type_label, actual_matches_formal_fun=<function actual_matches_formal_default>)[source]

Bases: object

Instances of this class represent types.

def_conversion(to_type, priority=0, tree_data=None)[source]

Define an automatic conversion to be applied.

undef_conversion(to_type)[source]

Undefine a conversion.

matches_formal_type(formal_type)[source]

Test whether this object as an actual type matches formal_type as a formal type. All checks for type matches use this method.

A hook is provided to pass in a different function to do the comparison. Set the TypeObject initializer keyword actual_matches_formal_fun to the function on the creation of each type. It should take two TypeObject arguments: the actual one followed by the formal one. Different functions can be passed to different type objects, or a generic one can be passed to all of them.

See also the related method matches_formal_sig of TypeSig which checks both the value and the arguments in a signature

short_repr()[source]

This repr prints None types as simply “None” rather than “TypeObject(None)”.

class typped.pratt_types.TypeTable(parser_instance)[source]

Bases: object

A type table holding instances of the TypeObject class, one for each defined type in the language. Each PrattParser instance has such a table, which holds the objects which represent each type defined in the language that the particular parser parses.

aliases = {}
create_typeobject(type_label)[source]

Create a TypeObject instance for a type with string label type_label and store it in the type table. Return the new instance. Raises a TypeException if an instance for type_label has already been created.

undef_typeobject(type_label)[source]

Un-define the token with label type_label. The TypeObject instance previously associated with that label is removed from the dictionary.

exception typped.pratt_types.TypeErrorInParsedLanguage[source]

Bases: typped.shared_settings_and_exceptions.ParserException

Raised when the there is a type error in the language being parsed, as opposed to a TypeError in the Python code.

exception typped.pratt_types.TypeModuleException[source]

Bases: typped.shared_settings_and_exceptions.ParserException

An exception in the code of the pratt_types module.