class documentation

class PyLinter(_ArgumentsManager, _MessageStateHandler, reporters.ReportsHandlerMixIn, checkers.BaseChecker): (source)

Known subclasses: pylint.testutils.UnittestLinter

View In Hierarchy

Lint Python modules using external checkers. This is the main checker controlling the other ones and the reports generation. It is itself both a raw checker and an astroid checker in order to: * handle message activation / deactivation at the module level * handle some basic but necessary stats' data (number of classes, methods...) IDE plugin developers: you may have to call `astroid.MANAGER.clear_cache()` across runs if you want to ensure the latest code version is actually checked. This class needs to support pickling for parallel linting to work. The exception is reporter member; see check_parallel function for more details.

Static Method should_analyze_file Returns whether a module should be checked.
Method __init__ Undocumented
Method add_ignored_message Prepares a message to be added to the ignored message storage.
Method add_message Adds a message given by ID or name.
Method any_fail_on_issues Undocumented
Method check Main checking entry: check a list of files or modules from their name.
Method check_astroid_module Check a module from its astroid representation.
Method check_single_file Undocumented
Method check_single_file_item Check single file item.
Method disable_reporters Disable all reporters.
Method enable_fail_on_messages Enable 'fail on' msgs.
Method generate_reports Close the whole package /module, it's time to make reports !
Method get_ast Return an ast(roid) representation of a module or a string.
Method get_checker_names Get all the checker names that this linter knows about.
Method get_checkers Return all available checkers as an ordered list.
Method initialize Initialize linter for linting.
Method load_default_plugins Undocumented
Method load_plugin_configuration Call the configuration hook for plugins.
Method load_plugin_modules Check a list of pylint plugins modules, load and register them.
Method open Initialize counters.
Method option_groups.setter Undocumented
Method prepare_checkers Return checkers needed for activated messages and reports.
Method register_checker This method auto registers the checker.
Method register_reporter Registers a reporter class on the _reporters attribute.
Method report_order Return a list of reporters.
Method set_current_module Set the name of the currently analyzed module and init statistics for it.
Method set_reporter Set the reporter used to display messages and reports.
Class Variable crash_file_path Undocumented
Class Variable option_groups_descs Undocumented
Instance Variable config Undocumented
Instance Variable current_file Undocumented
Instance Variable current_name Undocumented
Instance Variable fail_on_symbols List of message symbols on which pylint should fail, set by --fail-on.
Instance Variable file_state Undocumented
Instance Variable msg_status Undocumented
Instance Variable msgs_store Undocumented
Instance Variable options Undocumented
Instance Variable reporter Undocumented
Instance Variable reports Undocumented
Instance Variable stats Undocumented
Property option_groups Undocumented
Method _add_one_message After various checks have passed a single Message is passed to the reporter and added to stats.
Method _astroid_module_checker Context manager for checking ASTs.
Method _check_astroid_module Check given AST node with given walker and checkers.
Method _check_file Check a file using the passed utility functions (get_ast and check_astroid_module).
Method _discover_files Discover python modules and packages in sub-directory.
Method _emit_stashed_messages Undocumented
Method _expand_files Get modules and errors from a list of modules and handle errors.
Method _get_asts Get the AST for all given FileItems.
Method _get_file_descr_from_stdin Return file description (tuple of module name, file path, base name) from given file path.
Method _get_namespace_for_file Undocumented
Method _iterate_file_descrs Return generator yielding file descriptions (tuples of module name, file path, base name).
Method _lint_file Lint a file using the passed utility function check_astroid_module).
Method _lint_files Lint all AST modules from a mapping..
Method _load_reporter_by_name Undocumented
Method _load_reporters Load the reporters if they are available on _reporters.
Method _parse_error_mode Parse the current state of the error mode.
Method _report_evaluation Make the global evaluation report.
Instance Variable _by_id_managed_msgs Undocumented
Instance Variable _checkers Dictionary of registered and initialized checkers.
Instance Variable _dynamic_plugins Set of loaded plugin names.
Instance Variable _error_mode Undocumented
Instance Variable _ignore_file Undocumented
Instance Variable _ignore_paths Undocumented
Instance Variable _option_groups Undocumented
Instance Variable _reporters Dictionary of possible but non-initialized reporters.
Instance Variable _stashed_messages Some messages in the options (for --enable and --disable) are encountered too early to warn about them.

Inherited from _MessageStateHandler:

Method disable Disable a message for a scope.
Method disable_next Disable a message for the next line.
Method disable_noerror_messages Disable message categories other than `error` and `fatal`.
Method enable Enable a message for a scope.
Method is_message_enabled Return whether this message is enabled for the current file, line and confidence level.
Method list_messages_enabled Undocumented
Method process_tokens Process tokens from the current module to search for module/block level options.
Instance Variable linter Undocumented
Method _get_message_state_scope Returns the scope at which a message was enabled/disabled.
Method _get_messages_to_set Do some tests and find the actual messages of which the status should be set.
Method _is_one_message_enabled Checks state of a single message for the current file.
Method _register_by_id_managed_msg If the msgid is a numeric one, then register it to inform the user it could furnish instead a symbolic msgid.
Method _set_msg_status Do some tests and then iterate over message definitions to set state.
Method _set_one_msg_status Set the status of an individual message.
Instance Variable _bw_options_methods Undocumented
Instance Variable _msgs_state Undocumented
Instance Variable _options_methods Undocumented
Instance Variable _pragma_lineno Undocumented

Inherited from ReportsHandlerMixIn (via _MessageStateHandler):

Method disable_report Disable the report of the given id.
Method enable_report Enable the report of the given id.
Method make_reports Render registered reports.
Method register_report Register a report.
Method report_is_enabled Is the report associated to the given identifier enabled ?
Instance Variable _reports Undocumented
Instance Variable _reports_state Undocumented
@staticmethod
def should_analyze_file(modname: str, path: str, is_argument: bool = False) -> bool: (source)

Returns whether a module should be checked. This implementation returns True for all python source file, indicating that all files should be linted. Subclasses may override this method to indicate that modules satisfying certain conditions should not be linted. :param str modname: The name of the module to be checked. :param str path: The full path to the source code of the module. :param bool is_argument: Whether the file is an argument to pylint or not. Files which respect this property are always checked, since the user requested it explicitly. :returns: True if the module should be checked.

def __init__(self, options: Options = (), reporter: (reporters.BaseReporter|reporters.MultiReporter)|None = None, option_groups: tuple[tuple[str, str], ...] = (), pylintrc: str|None = None): (source)
def add_ignored_message(self, msgid: str, line: int, node: nodes.NodeNG|None = None, confidence: interfaces.Confidence|None = interfaces.UNDEFINED): (source)

Prepares a message to be added to the ignored message storage. Some checks return early in special cases and never reach add_message(), even though they would normally issue a message. This creates false positives for useless-suppression. This function avoids this by adding those message to the ignored msgs attribute

def add_message(self, msgid: str, line: int|None = None, node: nodes.NodeNG|None = None, args: Any|None = None, confidence: interfaces.Confidence|None = None, col_offset: int|None = None, end_lineno: int|None = None, end_col_offset: int|None = None): (source)

Adds a message given by ID or name. If provided, the message string is expanded using args. AST checkers must provide the node argument (but may optionally provide line if the line number is different), raw and token checkers must provide the line argument.

def any_fail_on_issues(self) -> bool: (source)

Undocumented

def check(self, files_or_modules: Sequence[str]|str): (source)

Main checking entry: check a list of files or modules from their name. files_or_modules is either a string or list of strings presenting modules to check.

def check_astroid_module(self, ast_node: nodes.Module, walker: ASTWalker, rawcheckers: list[checkers.BaseRawFileChecker], tokencheckers: list[checkers.BaseTokenChecker]) -> bool|None: (source)

Check a module from its astroid representation. For return value see _check_astroid_module

def check_single_file(self, name: str, filepath: str, modname: str): (source)

Undocumented

def check_single_file_item(self, file: FileItem): (source)

Check single file item. The arguments are the same that are documented in _check_files initialize() should be called before calling this method

def disable_reporters(self): (source)

Disable all reporters.

def enable_fail_on_messages(self): (source)

Enable 'fail on' msgs. Convert values in config.fail_on (which might be msg category, msg id, or symbol) to specific msgs, then enable and flag them for later.

def generate_reports(self) -> int|None: (source)

Close the whole package /module, it's time to make reports ! if persistent run, pickle results for later comparison

def get_ast(self, filepath: str, modname: str, data: str|None = None) -> nodes.Module|None: (source)

Return an ast(roid) representation of a module or a string. :param filepath: path to checked file. :param str modname: The name of the module to be checked. :param str data: optional contents of the checked file. :returns: the AST :rtype: astroid.nodes.Module :raises AstroidBuildingError: Whenever we encounter an unexpected exception

def get_checker_names(self) -> list[str]: (source)

Get all the checker names that this linter knows about.

def get_checkers(self) -> list[BaseChecker]: (source)

Return all available checkers as an ordered list.

def initialize(self): (source)

Initialize linter for linting. This method is called before any linting is done.

def load_default_plugins(self): (source)

Undocumented

def load_plugin_configuration(self): (source)

Call the configuration hook for plugins. This walks through the list of plugins, grabs the "load_configuration" hook, if exposed, and calls it to allow plugins to configure specific settings. The result of attempting to load the plugin of the given name is stored in the dynamic plugins dictionary in ``load_plugin_modules`` above. ..note:: This function previously always tried to load modules again, which led to some confusion and silent failure conditions as described in GitHub issue #7264. Making it use the stored result is more efficient, and means that we avoid the ``init-hook`` problems from before.

def load_plugin_modules(self, modnames: list[str]): (source)

Check a list of pylint plugins modules, load and register them. If a module cannot be loaded, never try to load it again and instead store the error message for later use in ``load_plugin_configuration`` below.

def open(self): (source)

Initialize counters.

@option_groups.setter
def option_groups(self, value: tuple[tuple[str, str], ...]): (source)

Undocumented

def prepare_checkers(self) -> list[BaseChecker]: (source)

Return checkers needed for activated messages and reports.

def register_checker(self, checker: checkers.BaseChecker): (source)

This method auto registers the checker.

def register_reporter(self, reporter_class: type[reporters.BaseReporter]): (source)

Registers a reporter class on the _reporters attribute.

def report_order(self) -> list[BaseChecker]: (source)

Return a list of reporters.

def set_current_module(self, modname: str|None, filepath: str|None = None): (source)

Set the name of the currently analyzed module and init statistics for it.

def set_reporter(self, reporter: reporters.BaseReporter|reporters.MultiReporter): (source)

Set the reporter used to display messages and reports.

crash_file_path: str = (source)

Undocumented

option_groups_descs: dict[str, str] = (source)

Undocumented

Undocumented

current_file = (source)

Undocumented

current_name = (source)

Undocumented

fail_on_symbols: list[str] = (source)

List of message symbols on which pylint should fail, set by --fail-on.

file_state = (source)

Undocumented

msg_status: int = (source)

Undocumented

msgs_store = (source)

Undocumented

options: Options = (source)

Undocumented

reporter = (source)

Undocumented

Undocumented

Undocumented

@property
option_groups: tuple[tuple[str, str], ...] = (source)

Undocumented

def _add_one_message(self, message_definition: MessageDefinition, line: int|None, node: nodes.NodeNG|None, args: Any|None, confidence: interfaces.Confidence|None, col_offset: int|None, end_lineno: int|None, end_col_offset: int|None): (source)

After various checks have passed a single Message is passed to the reporter and added to stats.

@contextlib.contextmanager
def _astroid_module_checker(self) -> Iterator[Callable[[nodes.Module], bool|None]]: (source)

Context manager for checking ASTs. The value in the context is callable accepting AST as its only argument.

def _check_astroid_module(self, node: nodes.Module, walker: ASTWalker, rawcheckers: list[checkers.BaseRawFileChecker], tokencheckers: list[checkers.BaseTokenChecker]) -> bool|None: (source)

Check given AST node with given walker and checkers. :param astroid.nodes.Module node: AST node of the module to check :param pylint.utils.ast_walker.ASTWalker walker: AST walker :param list rawcheckers: List of token checkers to use :param list tokencheckers: List of raw checkers to use :returns: True if the module was checked, False if ignored, None if the module contents could not be parsed

def _check_file(self, get_ast: GetAstProtocol, check_astroid_module: Callable[[nodes.Module], bool|None], file: FileItem): (source)

Check a file using the passed utility functions (get_ast and check_astroid_module). :param callable get_ast: callable returning AST from defined file taking the following arguments - filepath: path to the file to check - name: Python module name :param callable check_astroid_module: callable checking an AST taking the following arguments - ast: AST of the module :param FileItem file: data about the file :raises AstroidError: for any failures stemming from astroid

def _discover_files(self, files_or_modules: Sequence[str]) -> Iterator[str]: (source)

Discover python modules and packages in sub-directory. Returns iterator of paths to discovered modules and packages.

def _emit_stashed_messages(self): (source)

Undocumented

def _expand_files(self, files_or_modules: Sequence[str]) -> dict[str, ModuleDescriptionDict]: (source)

Get modules and errors from a list of modules and handle errors.

def _get_asts(self, fileitems: Iterator[FileItem], data: str|None) -> dict[FileItem, nodes.Module|None]: (source)

Get the AST for all given FileItems.

def _get_file_descr_from_stdin(self, filepath: str) -> Iterator[FileItem]: (source)

Return file description (tuple of module name, file path, base name) from given file path. This method is used for creating suitable file description for _check_files when the source is standard input.

def _get_namespace_for_file(self, filepath: Path, namespaces: DirectoryNamespaceDict) -> argparse.Namespace|None: (source)

Undocumented

def _iterate_file_descrs(self, files_or_modules: Sequence[str]) -> Iterator[FileItem]: (source)

Return generator yielding file descriptions (tuples of module name, file path, base name). The returned generator yield one item for each Python module that should be linted.

def _lint_file(self, file: FileItem, module: nodes.Module, check_astroid_module: Callable[[nodes.Module], bool|None]): (source)

Lint a file using the passed utility function check_astroid_module). :param FileItem file: data about the file :param nodes.Module module: the ast module to lint :param Callable check_astroid_module: callable checking an AST taking the following arguments - ast: AST of the module :raises AstroidError: for any failures stemming from astroid

def _lint_files(self, ast_mapping: dict[FileItem, nodes.Module|None], check_astroid_module: Callable[[nodes.Module], bool|None]): (source)

Lint all AST modules from a mapping..

def _load_reporter_by_name(self, reporter_name: str) -> reporters.BaseReporter: (source)

Undocumented

def _load_reporters(self, reporter_names: str): (source)

Load the reporters if they are available on _reporters.

def _parse_error_mode(self): (source)

Parse the current state of the error mode. Error mode: enable only errors; no reports, no persistent.

def _report_evaluation(self) -> int|None: (source)

Make the global evaluation report.

_by_id_managed_msgs: list[ManagedMessage] = (source)

Undocumented

_checkers: defaultdict[str, list[checkers.BaseChecker]] = (source)

Dictionary of registered and initialized checkers.

_dynamic_plugins = (source)

Set of loaded plugin names.

_error_mode: bool = (source)

Undocumented

_ignore_paths = (source)

Undocumented

_option_groups = (source)

Undocumented

_reporters: dict[str, type[reporters.BaseReporter]] = (source)

Dictionary of possible but non-initialized reporters.

_stashed_messages = (source)

Some messages in the options (for --enable and --disable) are encountered too early to warn about them. i.e. before all option providers have been fully parsed. Thus, this dict stores option_value and msg_id needed to (later) emit the messages keyed on module names.