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_sigof a construct is theTypeSigof formal arguments.The
expanded_sigof a construct is theoriginal_sigexpanded 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_sigof a construct is theTypeSigof 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:
objectThis 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
TypeSiginstance it will repeat any arguments inside it as many times as necessary.If
exact_repeatis 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:
objectThe 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
TypeSiginstance is essentially just a tuple(val_type, arg_types), whereval_typeis aTypeObjectSubclassand thearg_typesis 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 wildcardNonearguments to the correspondingTypeObjectinstances in the attributesval_typeandarg_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 thanval_typeandarg_types) which might be added to or modified in aTypeSiginstance.Note that
Noneis a wildcard which matches any type argument, andNonefor thearg_typeslist or tuple matches any arguments and any number of arguments (it is expanded during parsing to as manyNonearguments as are required). Note thatTypeSig() == TypeSig(None) == TypeSig(None, None). To specify an object like a literal which takes no arguments an empty list or tuple should be used forarg_types, as inTypeSig(None, []); theval_typeargument can be set to the desired type if it is typed.A single
TypeObjectas anarg_typesargument (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 haveInttype.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_typeandarg_typesattributes.-
val_type¶ Return the
val_typeproperty saved in_val_type.
-
arg_types¶ Return the
arg_typesproperty saved in_arg_types.
-
convert_val_type_wildcards(val_type)[source]¶ Convert
val_typeargument to aTypeObjectinstance and set attribute.
-
convert_arg_types_wildcards(arg_types)[source]¶ Convert all
arg_typesarguments toTypeObjectinstances 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_listwhose arguments match some choice of child/argument signatures fromlist_of_child_sig_listsNote 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
PrattParserto find all the argument-matching signatures. (If overloading on return types is used thenPrattParseralso callsget_child_sigs_matching_return_arg_typeon the second pass, to check return matches.)The
sig_listargument should be a list ofTypeSiginstances representing formal types.The
list_of_child_sig_listsargument 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
Nonearguments expanded and with a fixed number of arguments). The original, unexpanded formal signature which matched is saved as an attributeoriginal_formal_sigof 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_listso that anyNoneargument not inside a tuple or list is converted to a tuple ofNonehavingnum_argsof argument.This routine also expands argument signatures consisting of a single
TypeObjectto 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_listwhich matchnum_argsas the number of arguments. The optional token-tree nodetnodeis only used for improved error reporting.This routine assumes that a
formal_sigattribute has been set for all passed-in signatures (for example, as set by the methodexpand_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_listwhich have arguments that match (in order) the return type of some child signature in the corresponding sublist oflist_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
tnodeis 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_sigswhich also match in return type.This is used in pass two of the type-checking. When called, the
matching_sigsparameter is passed thechild.matching_sigsattribute 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_listeither withsigappended or with any identical signature (according only to type equality, not attributes) replaced if one is there. This is called inregister_handler_funs.
-
matches_formal_sig(formal_sig)[source]¶ Test if this signature as an actual signature matches
formal_sigas 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_typeis 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
TypeObjectmethodmatches_formal_typeon 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:
objectInstances of this class represent types.
-
def_conversion(to_type, priority=0, tree_data=None)[source]¶ Define an automatic conversion to be applied.
-
matches_formal_type(formal_type)[source]¶ Test whether this object as an actual type matches
formal_typeas 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
TypeObjectinitializer keywordactual_matches_formal_funto the function on the creation of each type. It should take twoTypeObjectarguments: 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_sigofTypeSigwhich checks both the value and the arguments in a signature
-
-
class
typped.pratt_types.TypeTable(parser_instance)[source]¶ Bases:
objectA type table holding instances of the
TypeObjectclass, one for each defined type in the language. EachPrattParserinstance has such a table, which holds the objects which represent each type defined in the language that the particular parser parses.-
aliases= {}¶
-
-
exception
typped.pratt_types.TypeErrorInParsedLanguage[source]¶ Bases:
typped.shared_settings_and_exceptions.ParserExceptionRaised when the there is a type error in the language being parsed, as opposed to a
TypeErrorin the Python code.
-
exception
typped.pratt_types.TypeModuleException[source]¶ Bases:
typped.shared_settings_and_exceptions.ParserExceptionAn exception in the code of the
pratt_typesmodule.