class Plugin(CommonPluginApi): (source)
Known subclasses: mypy.plugin.ChainedPlugin
, mypy.plugins.default.DefaultPlugin
, mypy.suggestions.SuggestionPlugin
Base class of all type checker plugins.
This defines a no-op plugin. Subclasses can override some methods to provide some actual functionality.
All get_ methods are treated as pure functions (you should assume that results might be cached). A plugin should return None from a get_ method to give way to other plugins.
Look at the comments of various *Context objects for additional information on various hooks.
Method | __init__ |
Undocumented |
Method | get_additional_deps |
Customize dependencies for a module. |
Method | get_attribute_hook |
Adjust type of a class attribute. |
Method | get_base_class_hook |
Update class definition for given base classes. |
Method | get_class_decorator_hook |
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 | lookup_fully_qualified |
Lookup a symbol by its full name (including module). |
Method | report_config_data |
Get representation of configuration data for a module. |
Method | set_modules |
Undocumented |
Instance Variable | options |
Undocumented |
Instance Variable | python_version |
Undocumented |
Instance Variable | _modules |
Undocumented |
mypy.plugin.ChainedPlugin
, mypy.suggestions.SuggestionPlugin
Undocumented
Parameters | |
options:Options | Undocumented |
mypy.plugin.ChainedPlugin
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.
Parameters | |
file:MypyFile | Undocumented |
Returns | |
List[ | Undocumented |
mypy.plugin.ChainedPlugin
, mypy.plugins.default.DefaultPlugin
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 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.
Parameters | |
fullname:str | Undocumented |
Returns | |
Optional[ | Undocumented |
mypy.plugin.ChainedPlugin
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.
Parameters | |
fullname:str | Undocumented |
Returns | |
Optional[ | Undocumented |
mypy.plugin.ChainedPlugin
, mypy.plugins.default.DefaultPlugin
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.
The hook is called with full names of all class decorators, for example
Parameters | |
fullname:str | Undocumented |
Returns | |
Optional[ | Undocumented |
mypy.plugin.ChainedPlugin
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.
Parameters | |
fullname:str | Undocumented |
Returns | |
Optional[ | Undocumented |
mypy.plugin.ChainedPlugin
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().
Parameters | |
fullname:str | Undocumented |
Returns | |
Optional[ | Undocumented |
mypy.plugin.ChainedPlugin
, mypy.plugins.default.DefaultPlugin
, mypy.suggestions.SuggestionPlugin
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'.
Parameters | |
fullname:str | Undocumented |
Returns | |
Optional[ | Undocumented |
mypy.plugin.ChainedPlugin
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'.
Parameters | |
fullname:str | Undocumented |
Returns | |
Optional[ | Undocumented |
mypy.plugin.ChainedPlugin
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.
Parameters | |
fullname:str | Undocumented |
Returns | |
Optional[ | Undocumented |
mypy.plugin.ChainedPlugin
, mypy.plugins.default.DefaultPlugin
, mypy.suggestions.SuggestionPlugin
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).
Parameters | |
fullname:str | Undocumented |
Returns | |
Optional[ | Undocumented |
mypy.plugin.ChainedPlugin
, mypy.plugins.default.DefaultPlugin
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__'.
Parameters | |
fullname:str | Undocumented |
Returns | |
Optional[ | Undocumented |
mypy.plugin.ChainedPlugin
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
.
Parameters | |
fullname:str | Undocumented |
Returns | |
Optional[ | Undocumented |
Lookup a symbol by its full name (including module).
This lookup function available for all plugins. Return None if a name is not found. This function doesn't support lookup from current scope. Use SemanticAnalyzerPluginInterface.lookup_qualified() for this.
Parameters | |
fullname:str | Undocumented |
Returns | |
Optional[ | Undocumented |
mypy.plugin.ChainedPlugin
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.
Parameters | |
ctx:ReportConfigContext | Undocumented |
Returns | |
Any | Undocumented |
mypy.plugin.ChainedPlugin
Undocumented
Parameters | |
modules:Dict[ | Undocumented |