Comprehensive static code analysis tool for Python that performs deep code inspection without executing the program
—
Main entry points and core functionality for running pylint analysis programmatically. These functions and classes provide the foundation for static code analysis, configuration management, and result collection.
Primary functions for running pylint analysis from Python code, providing programmatic access to pylint's command-line functionality.
def run_pylint(argv=None):
"""
Run pylint analysis from command line arguments.
Args:
argv (list, optional): Command line arguments. If None, uses sys.argv[1:]
Example:
run_pylint(['mymodule.py', '--output-format=json'])
"""
def run_pyreverse(argv=None):
"""
Run pyreverse UML diagram generation.
Args:
argv (list, optional): Command line arguments. If None, uses sys.argv[1:]
Example:
run_pyreverse(['mypackage/', '--output-format=svg'])
"""
def run_symilar(argv=None):
"""
Run symilar duplicate code detection.
Args:
argv (list, optional): Command line arguments. If None, uses sys.argv[1:]
Example:
run_symilar(['mymodule.py', '--min-lines=5'])
"""Utility function for managing Python's module search path to prevent import conflicts during analysis.
def modify_sys_path():
"""
Modify sys.path for execution as Python module.
Removes the current working directory from sys.path to prevent
pylint from inadvertently importing user code with the same names
as stdlib or pylint's own modules.
"""Main controller class that manages the entire checking process, configuration, checkers, and message collection.
class PyLinter:
"""
Main linter class controlling the checking process.
Inherits from ArgumentsManager, MessageStateHandler,
ReportsHandlerMixIn, and BaseChecker.
"""
def check(self, files_or_modules):
"""
Check files or modules for issues.
Args:
files_or_modules (list): List of file paths or module names to check
"""
def register_checker(self, checker):
"""
Register a new checker with the linter.
Args:
checker: Instance of BaseChecker or subclass
"""
def get_checkers(self):
"""
Get all available checkers.
Returns:
dict: Mapping of checker names to checker instances
"""
def add_message(self, msgid, line=None, node=None, args=None,
confidence=None, col_offset=None):
"""
Add a message/warning to the results.
Args:
msgid (str): Message identifier (e.g., 'unused-variable')
line (int, optional): Line number
node (astroid.Node, optional): AST node
args (tuple, optional): Message arguments
confidence (str, optional): Confidence level
col_offset (int, optional): Column offset
"""
def check_astroid_module(self, ast_node, walker, rawcheckers):
"""
Check an astroid AST module.
Args:
ast_node: Astroid AST node
walker: AST walker instance
rawcheckers: List of raw file checkers
"""
def generate_reports(self):
"""Generate analysis reports."""
def set_reporter(self, reporter):
"""
Set the message reporter.
Args:
reporter: Instance of BaseReporter or subclass
"""
def load_default_plugins(self):
"""Load default plugin checkers."""Handler class for processing command-line arguments and executing the linter.
class Run:
"""
Command-line interface handler for pylint.
Processes command-line arguments, configures the linter,
and executes the checking process.
"""
def __init__(self, args, reporter=None, exit=True, do_exit=None):
"""
Initialize and run pylint.
Args:
args (list): Command line arguments
reporter: Custom reporter instance
exit (bool): Whether to call sys.exit()
do_exit: Deprecated, use exit parameter
"""Functions for running pylint checks in parallel to improve performance on large codebases.
def check_parallel(linter, jobs, files, extra_packages_paths=None):
"""
Run checking in parallel across multiple processes.
Args:
linter: PyLinter instance
jobs (int): Number of parallel jobs
files: Iterable of FileItem objects to check
extra_packages_paths (list, optional): Additional package paths
Returns:
None: Results are collected via the linter's reporter
"""Utilities for expanding module names and discovering package paths.
def expand_modules(files_or_modules, source_roots, ignore_list, ignore_list_re, ignore_list_paths_re):
"""
Expand module names to actual file paths.
Args:
files_or_modules (list): Module names or file paths to expand
source_roots (list): Source root directories for module discovery
ignore_list (list): File patterns to ignore
ignore_list_re (list): Compiled regex patterns to ignore files
ignore_list_paths_re (list): Compiled regex patterns to ignore paths
Returns:
tuple: (modules_dict, errors_list) where modules_dict maps module names
to module description dictionaries and errors_list contains
error descriptions for files that couldn't be processed
"""
def discover_package_path(modulepath, source_roots):
"""
Discover package path from a module path and source roots.
Args:
modulepath (str): Path to the module file or directory
source_roots (list): Source root directories to search
Returns:
str: Discovered package path
"""Functions for saving and loading analysis results to improve performance on repeated runs.
def load_results(base):
"""
Load cached analysis results.
Args:
base (str): Base path for cache files
Returns:
dict: Cached results if available
"""
def save_results(results, base):
"""
Save analysis results to cache.
Args:
results: Analysis results to save
base (str): Base path for cache files
"""import pylint
from pylint.lint import PyLinter
from pylint.reporters import TextReporter
import io
# Using the main entry point
pylint.run_pylint(['mymodule.py', '--errors-only'])
# Using PyLinter directly
output = io.StringIO()
reporter = TextReporter(output)
linter = PyLinter()
linter.set_reporter(reporter)
linter.check(['mymodule.py'])
print(output.getvalue())from pylint.lint import PyLinter
from pylint.reporters import JSONReporter
linter = PyLinter()
linter.set_reporter(JSONReporter())
# Register custom checker
from mylinter.custom_checker import MyCustomChecker
linter.register_checker(MyCustomChecker(linter))
# Configure options
linter.config.max_line_length = 120
linter.config.disable = ['missing-docstring']
linter.check(['mypackage/'])from pylint.lint import PyLinter, check_parallel
import multiprocessing
linter = PyLinter()
files = ['module1.py', 'module2.py', 'module3.py']
jobs = multiprocessing.cpu_count()
stats = check_parallel(files, linter, jobs)
print(f"Total errors: {stats.error}")
print(f"Total warnings: {stats.warning}")Install with Tessl CLI
npx tessl i tessl/pypi-pylint