module documentation

Type checking of attribute access

Class FreezeTypeVarsVisitor Undocumented
Class MemberContext 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 apply_class_attr_hook Undocumented
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_all_type_vars Undocumented
Function is_instance_var Return if var is an instance variable according to PEP 526.
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 may_be_awaitable_attribute Check if the given type has the attribute when awaited.
Function report_missing_attribute Undocumented
Function type_object_type Return the type of a type object.
Function validate_super_call Undocumented
Function _analyze_member_access Undocumented
def add_class_tvars(t: ProperType, isuper: Instance|None, is_classmethod: bool, original_type: Type, original_vars: Sequence[TypeVarLikeType]|None = None) -> Type: (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).

def analyze_class_attribute_access(itype: Instance, name: str, mx: MemberContext, override_info: TypeInfo|None = None, original_vars: Sequence[TypeVarLikeType]|None = None) -> Type|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).

def analyze_decorator_or_funcbase_access(defn: Decorator|FuncBase, itype: Instance, info: TypeInfo, self_type: Type|None, name: str, mx: MemberContext) -> Type: (source)

Analyzes the type behind method access. The function itself can possibly be decorated. See: https://github.com/python/mypy/issues/10409

def analyze_descriptor_access(descriptor_type: Type, mx: MemberContext) -> Type: (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.

def analyze_enum_class_attribute_access(itype: Instance, name: str, mx: MemberContext) -> Type|None: (source)

Undocumented

def analyze_instance_member_access(name: str, typ: Instance, mx: MemberContext, override_info: TypeInfo|None) -> Type: (source)

Undocumented

def analyze_member_access(name: str, typ: Type, context: Context, is_lvalue: bool, is_super: bool, is_operator: bool, msg: MessageBuilder, *, original_type: Type, chk: mypy.checker.TypeChecker, override_info: TypeInfo|None = None, in_literal_context: bool = False, self_type: Type|None = None, module_symbol_table: SymbolTable|None = None, no_deferral: bool = False, is_self: bool = False) -> Type: (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)

def analyze_member_var_access(name: str, itype: Instance, info: TypeInfo, mx: MemberContext) -> Type: (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

def analyze_none_member_access(name: str, typ: NoneType, mx: MemberContext) -> Type: (source)

Undocumented

def analyze_type_callable_member_access(name: str, typ: FunctionLike, mx: MemberContext) -> Type: (source)

Undocumented

def analyze_type_type_member_access(name: str, typ: TypeType, mx: MemberContext, override_info: TypeInfo|None) -> Type: (source)

Undocumented

def analyze_typeddict_access(name: str, typ: TypedDictType, mx: MemberContext, override_info: TypeInfo|None) -> Type: (source)

Undocumented

def analyze_union_member_access(name: str, typ: UnionType, mx: MemberContext) -> Type: (source)

Undocumented

def analyze_var(name: str, var: Var, itype: Instance, info: TypeInfo, mx: MemberContext, *, implicit: bool = False) -> Type: (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

def apply_class_attr_hook(mx: MemberContext, hook: Callable[[AttributeContext], Type]|None, result: Type) -> Type|None: (source)

Undocumented

def check_final_member(name: str, info: TypeInfo, msg: MessageBuilder, ctx: Context): (source)

Give an error if the name being assigned was declared as final.

def check_self_arg(functype: FunctionLike, dispatched_arg_type: Type, is_classmethod: bool, context: Context, name: str, msg: MessageBuilder) -> FunctionLike: (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.

def freeze_all_type_vars(member_type: Type): (source)

Undocumented

def is_instance_var(var: Var) -> bool: (source)

Return if var is an instance variable according to PEP 526.

def is_valid_constructor(n: SymbolNode|None) -> bool: (source)

Does this node represents a valid constructor method? This includes normal functions, overloaded functions, and decorators that return a callable type.

def lookup_member_var_or_accessor(info: TypeInfo, name: str, is_lvalue: bool) -> SymbolNode|None: (source)

Find the attribute/accessor node that refers to a member of a type.

def may_be_awaitable_attribute(name: str, typ: Type, mx: MemberContext, override_info: TypeInfo|None = None) -> bool: (source)

Check if the given type has the attribute when awaited.

def report_missing_attribute(original_type: Type, typ: Type, name: str, mx: MemberContext, override_info: TypeInfo|None = None) -> Type: (source)

Undocumented

def type_object_type(info: TypeInfo, named_type: Callable[[str], Instance]) -> ProperType: (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'.

def validate_super_call(node: FuncBase, mx: MemberContext): (source)

Undocumented

def _analyze_member_access(name: str, typ: Type, mx: MemberContext, override_info: TypeInfo|None = None) -> Type: (source)

Undocumented