Comprehensive static code analysis tool for Python that performs deep code inspection without executing the program
npx @tessl/cli install tessl/pypi-pylint@3.3.0A comprehensive static code analysis tool for Python that performs deep code inspection without executing the program. Pylint uses advanced inference techniques through its internal astroid representation to understand code semantics beyond simple syntax checking, enabling it to detect complex issues like incorrect API usage even when imports are aliased. The tool enforces coding standards, identifies potential bugs, detects code smells, and provides refactoring suggestions while being highly configurable to adapt to different project requirements.
pip install pylintimport pylintFor direct API access:
from pylint import run_pylint, run_pyreverse, run_symilar
from pylint.lint import PyLinter
from pylint.checkers import BaseChecker
from pylint.reporters import BaseReporterimport pylint
# Run pylint on files from Python code
pylint.run_pylint(['mymodule.py', '--output-format=json'])
# Generate UML diagrams
pylint.run_pyreverse(['mypackage/', '--output-format=svg'])
# Find code duplicates
pylint.run_symilar(['mymodule.py', '--min-lines=5'])
# Programmatic linting
from pylint.lint import PyLinter
from pylint.reporters import TextReporter
import io
output = io.StringIO()
reporter = TextReporter(output)
linter = PyLinter()
linter.set_reporter(reporter)
linter.check(['mymodule.py'])
print(output.getvalue())Pylint's modular architecture enables comprehensive static analysis:
This design allows users to customize analysis depth, enable/disable specific checks, create custom checkers, and integrate pylint into diverse development workflows.
Main entry points for running pylint analysis programmatically, including the primary linting functions, configuration management, and result collection.
def run_pylint(argv=None):
"""Run pylint analysis from command line arguments."""
def modify_sys_path():
"""Modify sys.path for execution as Python module."""
class PyLinter:
"""Main linter class controlling the checking process."""
def check(self, files_or_modules): ...
def register_checker(self, checker): ...
def add_message(self, msgid, line, node, args, confidence, col_offset): ...Framework for creating custom checkers that analyze specific code patterns. Includes base classes, message definition system, and integration with the linter.
class BaseChecker:
"""Abstract base class for all checkers."""
name: str
msgs: dict
options: tuple
class BaseRawFileChecker(BaseChecker):
"""Base class for raw file checkers."""
class BaseTokenChecker(BaseChecker):
"""Base class for token-based checkers."""Comprehensive set of 78+ built-in checkers covering variables, imports, formatting, type checking, classes, design analysis, and more specialized areas.
# Core checker modules (examples)
import pylint.checkers.variables
import pylint.checkers.imports
import pylint.checkers.format
import pylint.checkers.typecheck
import pylint.checkers.exceptions
import pylint.checkers.classesOptional extensions providing 33+ additional specialized checkers for code style, complexity analysis, docstring validation, and advanced pattern detection.
# Extension initialization pattern
def initialize(linter):
"""Register extension checker with linter."""
# Available extensions (examples)
import pylint.extensions.code_style
import pylint.extensions.mccabe
import pylint.extensions.docparamsFlexible reporting system supporting multiple output formats (text, JSON, HTML) and custom reporter development for integration with different tools and workflows.
class BaseReporter:
"""Abstract base for all reporters."""
def handle_message(self, msg): ...
def set_output(self, output): ...
class TextReporter(BaseReporter): ...
class JSONReporter(BaseReporter): ...
class MultiReporter(BaseReporter): ...Hierarchical configuration system that handles command-line arguments, configuration files, and programmatic settings with validation and default values.
def find_default_config_files():
"""Locate default configuration files."""
class ArgumentsManager:
"""Command-line argument management."""
def find_pylintrc():
"""Find pylintrc configuration file."""Structured message definitions with categories, confidence levels, and detailed location information for precise error reporting and filtering.
class Message:
"""Individual message/warning representation."""
msg_id: str
symbol: str
msg: str
confidence: str
line: int
column: int
class MessageDefinition:
"""Message type definition."""UML diagram generation tool that analyzes Python code structure and creates class diagrams, package diagrams, and dependency visualizations in multiple formats.
def run_pyreverse(argv=None):
"""Generate UML diagrams from Python code."""
class PackageDiagram: ...
class ClassDiagram: ...
class DotWriter: ...
class PlantUmlWriter: ...Testing framework for developing and validating custom checkers, including test case base classes, message validation, and functional testing support.
class CheckerTestCase:
"""Base class for checker unit tests."""
class UnittestLinter:
"""Test-specific linter implementation."""
def set_config(**kwargs):
"""Set configuration for tests."""# Confidence levels
HIGH: str = "HIGH"
CONTROL_FLOW: str = "CONTROL_FLOW"
INFERENCE: str = "INFERENCE"
INFERENCE_FAILURE: str = "INFERENCE_FAILURE"
UNDEFINED: str = "UNDEFINED"
# Message types
MSG_TYPES: dict = {
'I': 'info',
'C': 'convention',
'R': 'refactor',
'W': 'warning',
'E': 'error',
'F': 'fatal'
}
# File extensions
PY_EXTS: tuple = ('.py', '.pyw', '.pyc', '.pyo')__version__: str # Package version
version: str # Version alias
# Message type mappings
MSG_TYPES_STATUS: dict
PY310_PLUS: bool # Python 3.10+ version flag
PY311_PLUS: bool # Python 3.11+ version flag
PY312_PLUS: bool # Python 3.12+ version flagclass InvalidMessageError(Exception):
"""Invalid message creation error."""
class UnknownMessageError(Exception):
"""Unregistered message error."""
class DeletedMessageError(Exception):
"""Deleted message encountered error."""
class EmptyReportError(Exception):
"""Empty report generation error."""
class InvalidReporterError(Exception):
"""Invalid reporter configuration error."""