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: Options, api: SemanticAnalyzerInterface): (source)

Undocumented

def analyze_namedtuple_classdef(self, defn: ClassDef, is_stub_file: bool, is_func_scope: bool) -> tuple[bool, TypeInfo|None]: (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).

def build_namedtuple_typeinfo(self, name: str, items: list[str], types: list[Type], default_items: Mapping[str, Expression], line: int, existing_info: TypeInfo|None) -> TypeInfo: (source)

Undocumented

def check_namedtuple(self, node: Expression, var_name: str|None, is_func_scope: bool) -> tuple[str|None, TypeInfo|None, list[TypeVarLikeType]]: (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.

def check_namedtuple_classdef(self, defn: ClassDef, is_stub_file: bool) -> tuple[list[str], list[Type], dict[str, Expression], list[Statement]]|None: (source)

Parse and validate fields in named tuple class definition. Return a four tuple: * field names * field types * field default values * valid statements or None, if any of the types are not ready.

def fail(self, msg: str, ctx: Context): (source)

Undocumented

def parse_namedtuple_args(self, call: CallExpr, fullname: str) -> None|tuple[list[str], list[Type], list[Expression], str, list[TypeVarLikeType], bool]: (source)

Parse a namedtuple() call into data needed to construct a type. Returns a 6-tuple: - List of argument names - List of argument types - List of default values - First argument of namedtuple - All typevars found in the field definition - Whether all types are ready. Return None if the definition didn't typecheck.

def parse_namedtuple_fields_with_types(self, nodes: list[Expression], context: Context) -> tuple[list[str], list[Type], list[Expression], bool]|None: (source)

Parse typed named tuple fields. Return (names, types, defaults, whether types are all ready), or None if error occurred.

@contextmanager
def save_namedtuple_body(self, named_tuple_info: TypeInfo) -> Iterator[None]: (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).

def store_namedtuple_info(self, info: TypeInfo, name: str, call: CallExpr, is_typed: bool): (source)

Undocumented

Undocumented

Undocumented