class documentation

class NamedTupleAnalyzer: (source)

View In Hierarchy

Undocumented

Method __init__ Undocumented
Method analyze​_namedtuple​_classdef Analyze if given class definition can be a named tuple definition.
Method build​_namedtuple​_typeinfo Undocumented
Method check​_namedtuple Check if a call defines a namedtuple.
Method check​_namedtuple​_classdef Parse and validate fields in named tuple class definition.
Method fail Undocumented
Method parse​_namedtuple​_args Parse a namedtuple() call into data needed to construct a type.
Method parse​_namedtuple​_fields​_with​_types Parse typed named tuple fields.
Method save​_namedtuple​_body Preserve the generated body of class-based named tuple and then restore it.
Method store​_namedtuple​_info Undocumented
Instance Variable api Undocumented
Instance Variable options Undocumented
def __init__(self, options, api): (source)

Undocumented

Parameters
options:OptionsUndocumented
api:SemanticAnalyzerInterfaceUndocumented
def analyze_namedtuple_classdef(self, defn, is_stub_file): (source)

Analyze if given class definition can be a named tuple definition.

Return a tuple where first item indicates whether this can possibly be a named tuple, and the second item is the corresponding TypeInfo (may be None if not ready and should be deferred).

Parameters
defn:ClassDefUndocumented
is​_stub​_file:boolUndocumented
Returns
Tuple[bool, Optional[TypeInfo]]Undocumented
def build_namedtuple_typeinfo(self, name, items, types, default_items, line): (source)

Undocumented

Parameters
name:strUndocumented
items:List[str]Undocumented
types:List[Type]Undocumented
default​_items:Mapping[str, Expression]Undocumented
line:intUndocumented
Returns
TypeInfoUndocumented
def check_namedtuple(self, node, var_name, is_func_scope): (source)

Check if a call defines a namedtuple.

The optional var_name argument is the name of the variable to which this is assigned, if any.

Return a tuple of two items:
  • Internal name of the named tuple (e.g. the name passed as an argument to namedtuple) or None if it is not a valid named tuple
  • Corresponding TypeInfo, or None if not ready.

If the definition is invalid but looks like a namedtuple, report errors but return (some) TypeInfo.

Parameters
node:ExpressionUndocumented
var​_name:Optional[str]Undocumented
is​_func​_scope:boolUndocumented
Returns
Tuple[Optional[str], Optional[TypeInfo]]Undocumented
def check_namedtuple_classdef(self, defn, is_stub_file): (source)

Parse and validate fields in named tuple class definition.

Return a three tuple:
  • field names
  • field types
  • field default values

or None, if any of the types are not ready.

Parameters
defn:ClassDefUndocumented
is​_stub​_file:boolUndocumented
Returns
Optional[Tuple[List[str], List[Type], Dict[str, Expression]]]Undocumented
def fail(self, msg, ctx): (source)

Undocumented

Parameters
msg:strUndocumented
ctx:ContextUndocumented
def parse_namedtuple_args(self, call, fullname): (source)

Parse a namedtuple() call into data needed to construct a type.

Returns a 5-tuple: - List of argument names - List of argument types - List of default values - First argument of namedtuple - Whether all types are ready.

Return None if the definition didn't typecheck.

Parameters
call:CallExprUndocumented
fullname:strUndocumented
Returns
Optional[Tuple[List[str], List[Type], List[Expression], str, bool]]Undocumented
def parse_namedtuple_fields_with_types(self, nodes, context): (source)

Parse typed named tuple fields.

Return (names, types, defaults, whether types are all ready), or None if error occurred.

Parameters
nodes:List[Expression]Undocumented
context:ContextUndocumented
Returns
Optional[Tuple[List[str], List[Type], List[Expression], bool]]Undocumented
@contextmanager
def save_namedtuple_body(self, named_tuple_info): (source)

Preserve the generated body of class-based named tuple and then restore it.

Temporarily clear the names dict so we don't get errors about duplicate names that were already set in build_namedtuple_typeinfo (we already added the tuple field names while generating the TypeInfo, and actual duplicates are already reported).

Parameters
named​_tuple​_info:TypeInfoUndocumented
Returns
Iterator[None]Undocumented
def store_namedtuple_info(self, info, name, call, is_typed): (source)

Undocumented

Parameters
info:TypeInfoUndocumented
name:strUndocumented
call:CallExprUndocumented
is​_typed:boolUndocumented

Undocumented

options = (source)

Undocumented