module documentation
(source)

Type checking of attribute access
Class ​Member​Context Information and objects needed to type check attribute access.
Function add​_class​_tvars Instantiate type variables during analyze_class_attribute_access, e.g T and Q in the following:
Function analyze​_class​_attribute​_access Analyze access to an attribute on a class object.
Function analyze​_decorator​_or​_funcbase​_access Analyzes the type behind method access.
Function analyze​_descriptor​_access Type check descriptor access.
Function analyze​_enum​_class​_attribute​_access Undocumented
Function analyze​_instance​_member​_access Undocumented
Function analyze​_member​_access Return the type of attribute 'name' of 'typ'.
Function analyze​_member​_var​_access Analyse attribute access that does not target a method.
Function analyze​_none​_member​_access Undocumented
Function analyze​_type​_callable​_member​_access Undocumented
Function analyze​_type​_type​_member​_access Undocumented
Function analyze​_typeddict​_access Undocumented
Function analyze​_union​_member​_access Undocumented
Function analyze​_var Analyze access to an attribute via a Var node.
Function check​_final​_member Give an error if the name being assigned was declared as final.
Function check​_self​_arg Check that an instance has a valid type for a method with annotated 'self'.
Function freeze​_type​_vars Undocumented
Function instance​_alias​_type Type of a type alias node targeting an instance, when appears in runtime context.
Function is​_valid​_constructor Does this node represents a valid constructor method?
Function lookup​_member​_var​_or​_accessor Find the attribute/accessor node that refers to a member of a type.
Function type​_object​_type Return the type of a type object.
Function _analyze​_member​_access Undocumented
def add_class_tvars(t, isuper, is_classmethod, original_type, original_vars=None): (source)

Instantiate type variables during analyze_class_attribute_access, e.g T and Q in the following:

class A(Generic[T]):
@classmethod def foo(cls: Type[Q]) -> Tuple[T, Q]: ...

class B(A[str]): pass B.foo()

Args:

t: Declared type of the method (or property) isuper: Current instance mapped to the superclass where method was defined, this

is usually done by map_instance_to_supertype()

is_classmethod: True if this method is decorated with @classmethod original_type: The value of the type B in the expression B.foo() or the corresponding

component in case of a union (this is used to bind the self-types)

original_vars: Type variables of the class callable on which the method was accessed

Returns:
Expanded method type with added type variables (when needed).
Parameters
t:ProperTypeUndocumented
isuper:Optional[Instance]Undocumented
is​_classmethod:boolUndocumented
original​_type:TypeUndocumented
original​_vars:Optional[Sequence[TypeVarLikeType]]Undocumented
Returns
TypeUndocumented
def analyze_class_attribute_access(itype, name, mx, override_info=None, original_vars=None): (source)

Analyze access to an attribute on a class object.

itype is the return type of the class object callable, original_type is the type of E in the expression E.var, original_vars are type variables of the class callable (for generic classes).

Parameters
itype:InstanceUndocumented
name:strUndocumented
mx:MemberContextUndocumented
override​_info:Optional[TypeInfo]Undocumented
original​_vars:Optional[Sequence[TypeVarLikeType]]Undocumented
Returns
Optional[Type]Undocumented
def analyze_decorator_or_funcbase_access(defn, itype, info, self_type, name, mx): (source)

Analyzes the type behind method access.

The function itself can possibly be decorated. See: https://github.com/python/mypy/issues/10409

Parameters
defn:Union[Decorator, FuncBase]Undocumented
itype:InstanceUndocumented
info:TypeInfoUndocumented
self​_type:Optional[Type]Undocumented
name:strUndocumented
mx:MemberContextUndocumented
Returns
TypeUndocumented
def analyze_descriptor_access(descriptor_type, mx): (source)

Type check descriptor access.

Arguments:
descriptor_type: The type of the descriptor attribute being accessed
(the type of f in a.f when f is a descriptor).

mx: The current member access context.

Return:
The return type of the appropriate __get__ overload for the descriptor.
Parameters
descriptor​_type:TypeUndocumented
mx:MemberContextUndocumented
Returns
TypeUndocumented
def analyze_enum_class_attribute_access(itype, name, mx): (source)

Undocumented

Parameters
itype:InstanceUndocumented
name:strUndocumented
mx:MemberContextUndocumented
Returns
Optional[Type]Undocumented
def analyze_instance_member_access(name, typ, mx, override_info): (source)

Undocumented

Parameters
name:strUndocumented
typ:InstanceUndocumented
mx:MemberContextUndocumented
override​_info:Optional[TypeInfo]Undocumented
Returns
TypeUndocumented
def analyze_member_access(name, typ, context, is_lvalue, is_super, is_operator, msg, *, original_type, chk, override_info=None, in_literal_context=False, self_type=None, module_symbol_table=None): (source)

Return the type of attribute 'name' of 'typ'.

The actual implementation is in '_analyze_member_access' and this docstring also applies to it.

This is a general operation that supports various different variations:

  1. lvalue or non-lvalue access (setter or getter access)
  2. supertype access when using super() (is_super == True and 'override_info' should refer to the supertype)

'original_type' is the most precise inferred or declared type of the base object that we have available. When looking for an attribute of 'typ', we may perform recursive calls targeting the fallback type, and 'typ' may become some supertype of 'original_type'. 'original_type' is always preserved as the 'typ' type used in the initial, non-recursive call. The 'self_type' is a component of 'original_type' to which generic self should be bound (a narrower type that has a fallback to instance). Currently this is used only for union types.

'module_symbol_table' is passed to this function if 'typ' is actually a module and we want to keep track of the available attributes of the module (since they are not available via the type object directly)

Parameters
name:strUndocumented
typ:TypeUndocumented
context:ContextUndocumented
is​_lvalue:boolUndocumented
is​_super:boolUndocumented
is​_operator:boolUndocumented
msg:MessageBuilderUndocumented
original​_type:TypeUndocumented
chk:mypy.checker.TypeCheckerUndocumented
override​_info:Optional[TypeInfo]Undocumented
in​_literal​_context:boolUndocumented
self​_type:Optional[Type]Undocumented
module​_symbol​_table:Optional[SymbolTable]Undocumented
Returns
TypeUndocumented
def analyze_member_var_access(name, itype, info, mx): (source)

Analyse attribute access that does not target a method.

This is logically part of analyze_member_access and the arguments are similar.

original_type is the type of E in the expression E.var

Parameters
name:strUndocumented
itype:InstanceUndocumented
info:TypeInfoUndocumented
mx:MemberContextUndocumented
Returns
TypeUndocumented
def analyze_none_member_access(name, typ, mx): (source)

Undocumented

Parameters
name:strUndocumented
typ:NoneTypeUndocumented
mx:MemberContextUndocumented
Returns
TypeUndocumented
def analyze_type_callable_member_access(name, typ, mx): (source)

Undocumented

Parameters
name:strUndocumented
typ:FunctionLikeUndocumented
mx:MemberContextUndocumented
Returns
TypeUndocumented
def analyze_type_type_member_access(name, typ, mx, override_info): (source)

Undocumented

Parameters
name:strUndocumented
typ:TypeTypeUndocumented
mx:MemberContextUndocumented
override​_info:Optional[TypeInfo]Undocumented
Returns
TypeUndocumented
def analyze_typeddict_access(name, typ, mx, override_info): (source)

Undocumented

Parameters
name:strUndocumented
typ:TypedDictTypeUndocumented
mx:MemberContextUndocumented
override​_info:Optional[TypeInfo]Undocumented
Returns
TypeUndocumented
def analyze_union_member_access(name, typ, mx): (source)

Undocumented

Parameters
name:strUndocumented
typ:UnionTypeUndocumented
mx:MemberContextUndocumented
Returns
TypeUndocumented
def analyze_var(name, var, itype, info, mx, *, implicit=False): (source)

Analyze access to an attribute via a Var node.

This is conceptually part of analyze_member_access and the arguments are similar.

itype is the class object in which var is defined original_type is the type of E in the expression E.var if implicit is True, the original Var was created as an assignment to self

Parameters
name:strUndocumented
var:VarUndocumented
itype:InstanceUndocumented
info:TypeInfoUndocumented
mx:MemberContextUndocumented
implicit:boolUndocumented
Returns
TypeUndocumented
def check_final_member(name, info, msg, ctx): (source)
Give an error if the name being assigned was declared as final.
Parameters
name:strUndocumented
info:TypeInfoUndocumented
msg:MessageBuilderUndocumented
ctx:ContextUndocumented
def check_self_arg(functype, dispatched_arg_type, is_classmethod, context, name, msg): (source)

Check that an instance has a valid type for a method with annotated 'self'.

For example if the method is defined as:
class A:
def f(self: S) -> T: ...

then for 'x.f' we check that meet(type(x), A) <: S. If the method is overloaded, we select only overloads items that satisfy this requirement. If there are no matching overloads, an error is generated.

Note: dispatched_arg_type uses a meet to select a relevant item in case if the original type of 'x' is a union. This is done because several special methods treat union types in ad-hoc manner, so we can't use MemberContext.self_type yet.

Parameters
functype:FunctionLikeUndocumented
dispatched​_arg​_type:TypeUndocumented
is​_classmethod:boolUndocumented
context:ContextUndocumented
name:strUndocumented
msg:MessageBuilderUndocumented
Returns
FunctionLikeUndocumented
def freeze_type_vars(member_type): (source)

Undocumented

Parameters
member​_type:TypeUndocumented
def instance_alias_type(alias, named_type): (source)

Type of a type alias node targeting an instance, when appears in runtime context.

As usual, we first erase any unbound type variables to Any.

Parameters
alias:TypeAliasUndocumented
named​_type:Callable[[str], Instance]Undocumented
Returns
TypeUndocumented
def is_valid_constructor(n): (source)

Does this node represents a valid constructor method?

This includes normal functions, overloaded functions, and decorators that return a callable type.

Parameters
n:Optional[SymbolNode]Undocumented
Returns
boolUndocumented
def lookup_member_var_or_accessor(info, name, is_lvalue): (source)
Find the attribute/accessor node that refers to a member of a type.
Parameters
info:TypeInfoUndocumented
name:strUndocumented
is​_lvalue:boolUndocumented
Returns
Optional[SymbolNode]Undocumented
def type_object_type(info, named_type): (source)

Return the type of a type object.

For a generic type G with type variables T and S the type is generally of form

Callable[..., G[T, S]]

where ... are argument types for the __init__/__new__ method (without the self argument). Also, the fallback type will be 'type' instead of 'function'.

Parameters
info:TypeInfoUndocumented
named​_type:Callable[[str], Instance]Undocumented
Returns
ProperTypeUndocumented
def _analyze_member_access(name, typ, mx, override_info=None): (source)

Undocumented

Parameters
name:strUndocumented
typ:TypeUndocumented
mx:MemberContextUndocumented
override​_info:Optional[TypeInfo]Undocumented
Returns
TypeUndocumented