module documentation

Semantic analysis of types

Class CollectAllInnerTypesQuery Undocumented
Class DivergingAliasDetector See docstring of detect_diverging_alias() for details.
Class HasAnyFromUnimportedType Undocumented
Class HasExplicitAny Undocumented
Class HasSelfType Undocumented
Class InstanceFixer Undocumented
Class MsgCallback Undocumented
Class TypeAnalyser Semantic analyzer for types.
Class TypeVarLikeQuery Find TypeVar and ParamSpec references in an unbound type.
Function analyze_type_alias Analyze r.h.s. of a (potential) type alias definition.
Function check_for_explicit_any Undocumented
Function collect_all_inner_types Return all types that `t` contains
Function detect_diverging_alias This detects type aliases that will diverge during type checking.
Function expand_type_alias Expand a (generic) type alias target following the rules outlined in TypeAlias docstring.
Function find_self_type Undocumented
Function fix_instance Fix a malformed instance by replacing all type arguments with Any.
Function fix_instance_types Recursively fix all instance types (type argument count) in a given type.
Function flatten_tvars Undocumented
Function get_omitted_any Undocumented
Function has_any_from_unimported_type Return true if this type is Any because an import was not followed.
Function has_explicit_any Whether this type is or type it contains is an Any coming from explicit type annotation
Function make_optional_type Return the type corresponding to Optional[t].
Function no_subscript_builtin_alias Undocumented
Function remove_dups Undocumented
Function set_any_tvars Undocumented
Constant ARG_KINDS_BY_CONSTRUCTOR Undocumented
Constant GENERIC_STUB_NOT_AT_RUNTIME_TYPES Undocumented
Constant SELF_TYPE_NAMES Undocumented
Constant type_constructors Undocumented
Type Variable T Undocumented
Type Alias TypeVarLikeList Undocumented
def analyze_type_alias(type: Type, api: SemanticAnalyzerCoreInterface, tvar_scope: TypeVarLikeScope, plugin: Plugin, options: Options, is_typeshed_stub: bool, allow_placeholder: bool = False, in_dynamic_func: bool = False, global_scope: bool = True, allowed_alias_tvars: list[TypeVarLikeType]|None = None) -> tuple[Type, set[str]]: (source)

Analyze r.h.s. of a (potential) type alias definition. If `node` is valid as a type alias rvalue, return the resulting type and a set of full names of type aliases it depends on (directly or indirectly). 'node' must have been semantically analyzed.

def check_for_explicit_any(typ: Type|None, options: Options, is_typeshed_stub: bool, msg: MessageBuilder, context: Context): (source)

Undocumented

def collect_all_inner_types(t: Type) -> list[Type]: (source)

Return all types that `t` contains

def detect_diverging_alias(node: TypeAlias, target: Type, lookup: Callable[[str, Context], SymbolTableNode|None], scope: TypeVarLikeScope) -> bool: (source)

This detects type aliases that will diverge during type checking. For example F = Something[..., F[List[T]]]. At each expansion step this will produce *new* type aliases: e.g. F[List[int]], F[List[List[int]]], etc. So we can't detect recursion. It is a known problem in the literature, recursive aliases and generic types don't always go well together. It looks like there is no known systematic solution yet. # TODO: should we handle such aliases using type_recursion counter and some large limit? They may be handy in rare cases, e.g. to express a union of non-mixed nested lists: Nested = Union[T, Nested[List[T]]] ~> Union[T, List[T], List[List[T]], ...]

def expand_type_alias(node: TypeAlias, args: list[Type], fail: MsgCallback, no_args: bool, ctx: Context, *, unexpanded_type: Type|None = None, disallow_any: bool = False, use_standard_error: bool = False) -> Type: (source)

Expand a (generic) type alias target following the rules outlined in TypeAlias docstring. Here: target: original target type args: types to be substituted in place of type variables fail: error reporter callback no_args: whether original definition used a bare generic `A = List` ctx: context where expansion happens unexpanded_type, disallow_any, use_standard_error: used to customize error messages

def find_self_type(typ: Type, lookup: Callable[[str], SymbolTableNode|None]) -> bool: (source)

Undocumented

def fix_instance(t: Instance, fail: MsgCallback, note: MsgCallback, disallow_any: bool, python_version: tuple[int, int], use_generic_error: bool = False, unexpanded_type: Type|None = None): (source)

Fix a malformed instance by replacing all type arguments with Any. Also emit a suitable error if this is not due to implicit Any's.

def fix_instance_types(t: Type, fail: MsgCallback, note: MsgCallback, python_version: tuple[int, int]): (source)

Recursively fix all instance types (type argument count) in a given type. For example 'Union[Dict, List[str, int]]' will be transformed into 'Union[Dict[Any, Any], List[Any]]' in place.

def flatten_tvars(lists: list[list[T]]) -> list[T]: (source)

Undocumented

def get_omitted_any(disallow_any: bool, fail: MsgCallback, note: MsgCallback, orig_type: Type, python_version: tuple[int, int], fullname: str|None = None, unexpanded_type: Type|None = None) -> AnyType: (source)

Undocumented

def has_any_from_unimported_type(t: Type) -> bool: (source)

Return true if this type is Any because an import was not followed. If type t is such Any type or has type arguments that contain such Any type this function will return true.

def has_explicit_any(t: Type) -> bool: (source)

Whether this type is or type it contains is an Any coming from explicit type annotation

def make_optional_type(t: Type) -> Type: (source)

Return the type corresponding to Optional[t]. Note that we can't use normal union simplification, since this function is called during semantic analysis and simplification only works during type checking.

def no_subscript_builtin_alias(name: str, propose_alt: bool = True) -> str: (source)

Undocumented

def remove_dups(tvars: list[T]) -> list[T]: (source)

Undocumented

def set_any_tvars(node: TypeAlias, newline: int, newcolumn: int, *, from_error: bool = False, disallow_any: bool = False, fail: MsgCallback|None = None, unexpanded_type: Type|None = None) -> Type: (source)

Undocumented

ARG_KINDS_BY_CONSTRUCTOR = (source)

Undocumented

Value
{'mypy_extensions.Arg': ARG_POS,
 'mypy_extensions.DefaultArg': ARG_OPT,
 'mypy_extensions.NamedArg': ARG_NAMED,
 'mypy_extensions.DefaultNamedArg': ARG_NAMED_OPT,
 'mypy_extensions.VarArg': ARG_STAR,
 'mypy_extensions.KwArg': ARG_STAR2}
GENERIC_STUB_NOT_AT_RUNTIME_TYPES: set[str] = (source)

Undocumented

Value
set(['queue.Queue', 'builtins._PathLike', 'asyncio.futures.Future'])
SELF_TYPE_NAMES: set[str] = (source)

Undocumented

Value
set(['typing.Self', 'typing_extensions.Self'])
type_constructors = (source)

Undocumented

Value
set(['typing.Callable',
     'typing.Optional',
     'typing.Tuple',
     'typing.Type',
     'typing.Union',
     *LITERAL_TYPE_NAMES,
     *ANNOTATED_TYPE_NAMES])

Undocumented

Value
TypeVar('T')
TypeVarLikeList = (source)

Undocumented

Value
List[Tuple[str, TypeVarLikeExpr]]