class documentation

class TypedDictAnalyzer: (source)

View In Hierarchy

Undocumented

Method __init__ Undocumented
Method add_keys_and_types_from_base Undocumented
Method analyze_base_args Analyze arguments of base type expressions as types.
Method analyze_typeddict_classdef Analyze a class that may define a TypedDict.
Method analyze_typeddict_classdef_fields Analyze fields defined in a TypedDict class definition.
Method build_typeddict_typeinfo Undocumented
Method check_typeddict Check if a call defines a TypedDict.
Method fail Undocumented
Method fail_typeddict_arg Undocumented
Method is_typeddict Undocumented
Method map_items_to_base Map item types to how they would look in their base with type arguments applied.
Method note Undocumented
Method parse_typeddict_args Parse typed dict call expression.
Method parse_typeddict_fields_with_types Parse typed dict items passed as pairs (name expression, type expression).
Instance Variable api Undocumented
Instance Variable msg Undocumented
Instance Variable options Undocumented
def __init__(self, options: Options, api: SemanticAnalyzerInterface, msg: MessageBuilder): (source)

Undocumented

def add_keys_and_types_from_base(self, base: Expression, keys: list[str], types: list[Type], required_keys: set[str], ctx: Context): (source)

Undocumented

def analyze_base_args(self, base: IndexExpr, ctx: Context) -> list[Type]|None: (source)

Analyze arguments of base type expressions as types. We need to do this, because normal base class processing happens after the TypedDict special-casing (plus we get a custom error message).

def analyze_typeddict_classdef(self, defn: ClassDef) -> tuple[bool, TypeInfo|None]: (source)

Analyze a class that may define a TypedDict. Assume that base classes have been analyzed already. Note: Unlike normal classes, we won't create a TypeInfo until the whole definition of the TypeDict (including the body and all key names and types) is complete. This is mostly because we store the corresponding TypedDictType in the TypeInfo. Return (is this a TypedDict, new TypeInfo). Specifics: * If we couldn't finish due to incomplete reference anywhere in the definition, return (True, None). * If this is not a TypedDict, return (False, None).

def analyze_typeddict_classdef_fields(self, defn: ClassDef, oldfields: list[str]|None = None) -> tuple[list[str]|None, list[Type], list[Statement], set[str]]: (source)

Analyze fields defined in a TypedDict class definition. This doesn't consider inherited fields (if any). Also consider totality, if given. Return tuple with these items: * List of keys (or None if found an incomplete reference --> deferral) * List of types for each key * List of statements from defn.defs.body that are legally allowed to be a part of a TypedDict definition * Set of required keys

def build_typeddict_typeinfo(self, name: str, items: list[str], types: list[Type], required_keys: set[str], line: int, existing_info: TypeInfo|None) -> TypeInfo: (source)

Undocumented

def check_typeddict(self, node: Expression, var_name: str|None, is_func_scope: bool) -> tuple[bool, TypeInfo|None, list[TypeVarLikeType]]: (source)

Check if a call defines a TypedDict. The optional var_name argument is the name of the variable to which this is assigned, if any. Return a pair (is it a typed dict, corresponding TypeInfo). If the definition is invalid but looks like a TypedDict, report errors but return (some) TypeInfo. If some type is not ready, return (True, None).

def fail(self, msg: str, ctx: Context, *, code: ErrorCode|None = None): (source)

Undocumented

def fail_typeddict_arg(self, message: str, context: Context) -> tuple[str, list[str], list[Type], bool, list[TypeVarLikeType], bool]: (source)

Undocumented

def is_typeddict(self, expr: Expression) -> bool: (source)

Undocumented

def map_items_to_base(self, valid_items: dict[str, Type], tvars: list[TypeVarLikeType], base_args: list[Type]) -> dict[str, Type]: (source)

Map item types to how they would look in their base with type arguments applied. We would normally use expand_type() for such task, but we can't use it during semantic analysis, because it can (indirectly) call is_subtype() etc., and it will crash on placeholder types. So we hijack replace_alias_tvars() that was initially intended to deal with eager expansion of generic type aliases during semantic analysis.

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

Undocumented

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

Parse typed dict call expression. Return names, types, totality, was there an error during parsing. If some type is not ready, return None.

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

Parse typed dict items passed as pairs (name expression, type expression). Return names, types, was there an error. If some type is not ready, return None.

Undocumented

Undocumented

Undocumented