class documentation

class ChainedPlugin(Plugin): (source)

View In Hierarchy

A plugin that represents a sequence of chained plugins. Each lookup method returns the hook for the first plugin that reports a match. This class should not be subclassed -- use Plugin as the base class for all plugins.

Method __init__ Initialize chained plugin.
Method get_additional_deps Customize dependencies for a module.
Method get_attribute_hook Adjust type of an instance attribute.
Method get_base_class_hook Update class definition for given base classes.
Method get_class_attribute_hook Adjust type of a class attribute.
Method get_class_decorator_hook Update class definition for given class decorators.
Method get_class_decorator_hook_2 Update class definition for given class decorators.
Method get_customize_class_mro_hook Customize MRO for given classes.
Method get_dynamic_class_hook Semantically analyze a dynamic class definition.
Method get_function_hook Adjust the return type of a function call.
Method get_function_signature_hook Adjust the signature of a function.
Method get_metaclass_hook Update class definition for given declared metaclasses.
Method get_method_hook Adjust return type of a method call.
Method get_method_signature_hook Adjust the signature of a method.
Method get_type_analyze_hook Customize behaviour of the type analyzer for given full names.
Method report_config_data Get representation of configuration data for a module.
Method set_modules Undocumented
Method _find_hook Undocumented
Instance Variable _plugins Undocumented

Inherited from Plugin:

Method lookup_fully_qualified Lookup a symbol by its full name (including module).
Instance Variable options Undocumented
Instance Variable python_version Undocumented
Instance Variable _modules Undocumented
def __init__(self, options: Options, plugins: list[Plugin]): (source)

Initialize chained plugin. Assume that the child plugins aren't mutated (results may be cached).

def get_additional_deps(self, file: MypyFile) -> list[tuple[int, str, int]]: (source)

Customize dependencies for a module. This hook allows adding in new dependencies for a module. It is called after parsing a file but before analysis. This can be useful if a library has dependencies that are dynamic based on configuration information, for example. Returns a list of (priority, module name, line number) tuples. The line number can be -1 when there is not a known real line number. Priorities are defined in mypy.build (but maybe shouldn't be). 10 is a good choice for priority.

def get_attribute_hook(self, fullname: str) -> Callable[[AttributeContext], Type]|None: (source)

Adjust type of an instance attribute. This method is called with attribute full name using the class of the instance where the attribute was defined (or Var.info.fullname for generated attributes). For classes without __getattr__ or __getattribute__, this hook is only called for names of fields/properties (but not methods) that exist in the instance MRO. For classes that implement __getattr__ or __getattribute__, this hook is called for all fields/properties, including nonexistent ones (but still not methods). For example: class Base: x: Any def __getattr__(self, attr: str) -> Any: ... class Derived(Base): ... var: Derived var.x var.y get_attribute_hook is called with '__main__.Base.x' and '__main__.Base.y'. However, if we had not implemented __getattr__ on Base, you would only get the callback for 'var.x'; 'var.y' would produce an error without calling the hook.

def get_base_class_hook(self, fullname: str) -> Callable[[ClassDefContext], None]|None: (source)

Update class definition for given base classes. Same as get_class_decorator_hook() but for base classes. Base classes don't need to refer to TypeInfos, if a base class refers to a variable with Any type, this hook will still be called.

def get_class_attribute_hook(self, fullname: str) -> Callable[[AttributeContext], Type]|None: (source)

Adjust type of a class attribute. This method is called with attribute full name using the class where the attribute was defined (or Var.info.fullname for generated attributes). For example: class Cls: x: Any Cls.x get_class_attribute_hook is called with '__main__.Cls.x' as fullname.

def get_class_decorator_hook(self, fullname: str) -> Callable[[ClassDefContext], None]|None: (source)

Update class definition for given class decorators. The plugin can modify a TypeInfo _in place_ (for example add some generated methods to the symbol table). This hook is called after the class body was semantically analyzed, but *there may still be placeholders* (typically caused by forward references). NOTE: Usually get_class_decorator_hook_2 is the better option, since it guarantees that there are no placeholders. The hook is called with full names of all class decorators. The hook can be called multiple times per class, so it must be idempotent.

def get_class_decorator_hook_2(self, fullname: str) -> Callable[[ClassDefContext], bool]|None: (source)

Update class definition for given class decorators. Similar to get_class_decorator_hook, but this runs in a later pass when placeholders have been resolved. The hook can return False if some base class hasn't been processed yet using class hooks. It causes all class hooks (that are run in this same pass) to be invoked another time for the file(s) currently being processed. The hook can be called multiple times per class, so it must be idempotent.

def get_customize_class_mro_hook(self, fullname: str) -> Callable[[ClassDefContext], None]|None: (source)

Customize MRO for given classes. The plugin can modify the class MRO _in place_. This method is called with the class full name before its body was semantically analyzed.

def get_dynamic_class_hook(self, fullname: str) -> Callable[[DynamicClassDefContext], None]|None: (source)

Semantically analyze a dynamic class definition. This plugin hook allows one to semantically analyze dynamic class definitions like: from lib import dynamic_class X = dynamic_class('X', []) For such definition, this hook will be called with 'lib.dynamic_class'. The plugin should create the corresponding TypeInfo, and place it into a relevant symbol table, e.g. using ctx.api.add_symbol_table_node().

def get_function_hook(self, fullname: str) -> Callable[[FunctionContext], Type]|None: (source)

Adjust the return type of a function call. This method is called after type checking a call. Plugin may adjust the return type inferred by mypy, and/or emit some error messages. Note, this hook is also called for class instantiation calls, so that in this example: from lib import Class, do_stuff do_stuff(42) Class() This method will be called with 'lib.do_stuff' and then with 'lib.Class'.

def get_function_signature_hook(self, fullname: str) -> Callable[[FunctionSigContext], FunctionLike]|None: (source)

Adjust the signature of a function. This method is called before type checking a function call. Plugin may infer a better type for the function. from lib import Class, do_stuff do_stuff(42) Class() This method will be called with 'lib.do_stuff' and then with 'lib.Class'.

def get_metaclass_hook(self, fullname: str) -> Callable[[ClassDefContext], None]|None: (source)

Update class definition for given declared metaclasses. Same as get_class_decorator_hook() but for metaclasses. Note: this hook will be only called for explicit metaclasses, not for inherited ones. TODO: probably it should also be called on inherited metaclasses.

def get_method_hook(self, fullname: str) -> Callable[[MethodContext], Type]|None: (source)

Adjust return type of a method call. This is the same as get_function_hook(), but is called with the method full name (again, using the class where the method is defined).

def get_method_signature_hook(self, fullname: str) -> Callable[[MethodSigContext], FunctionLike]|None: (source)

Adjust the signature of a method. This method is called before type checking a method call. Plugin may infer a better type for the method. The hook is also called for special Python dunder methods except __init__ and __new__ (use get_function_hook to customize class instantiation). This function is called with the method full name using the class where it was _defined_. For example, in this code: from lib import Special class Base: def method(self, arg: Any) -> Any: ... class Derived(Base): ... var: Derived var.method(42) x: Special y = x[0] this method is called with '__main__.Base.method', and then with 'lib.Special.__getitem__'.

def get_type_analyze_hook(self, fullname: str) -> Callable[[AnalyzeTypeContext], Type]|None: (source)

Customize behaviour of the type analyzer for given full names. This method is called during the semantic analysis pass whenever mypy sees an unbound type. For example, while analysing this code: from lib import Special, Other var: Special def func(x: Other[int]) -> None: ... this method will be called with 'lib.Special', and then with 'lib.Other'. The callback returned by plugin must return an analyzed type, i.e. an instance of `mypy.types.Type`.

def report_config_data(self, ctx: ReportConfigContext) -> Any: (source)

Get representation of configuration data for a module. The data must be encodable as JSON and will be stored in the cache metadata for the module. A mismatch between the cached values and the returned will result in that module's cache being invalidated and the module being rechecked. This can be called twice for each module, once after loading the cache to check if it is valid and once while writing new cache information. If is_check in the context is true, then the return of this call will be checked against the cached version. Otherwise the call is being made to determine what to put in the cache. This can be used to allow consulting extra cache files in certain complex situations. This can be used to incorporate external configuration information that might require changes to typechecking.

def set_modules(self, modules: dict[str, MypyFile]): (source)

Undocumented

def _find_hook(self, lookup: Callable[[Plugin], T]) -> T|None: (source)

Undocumented

_plugins = (source)

Undocumented