Static analysis tool that detects errors in Python code without importing it.
npx @tessl/cli install tessl/pypi-pyflakes@3.4.0Pyflakes is a static analysis tool that detects errors in Python code without importing it, making it safe and fast. It analyzes programs by parsing source files and detects various programming errors like undefined variables, unused imports, and syntax issues. Unlike other linters, Pyflakes focuses solely on error detection without complaining about code style, maintaining a strict philosophy of minimizing false positives.
pip install pyflakesimport pyflakes.api
import pyflakes.checker
import pyflakes.reporter
import pyflakes.messagesFor basic usage:
from pyflakes.api import check, checkPath, checkRecursive
from pyflakes.reporter import Reporterimport pyflakes.api
# Check a code string
code = """
import os
x = 1
print(y) # This will be flagged as undefined
"""
warnings = pyflakes.api.check(code, 'example.py')
print(f"Found {warnings} warnings")
# Check a file
warnings = pyflakes.api.checkPath('myfile.py')
# Check multiple paths recursively
from pyflakes.reporter import _makeDefaultReporter
reporter = _makeDefaultReporter()
warnings = pyflakes.api.checkRecursive(['src/', 'tests/'], reporter)Pyflakes uses a multi-layered architecture:
This design enables Pyflakes to safely analyze Python code without importing it while maintaining detailed scope information to minimize false positives.
Primary interface for checking Python code, supporting code strings, individual files, and recursive directory analysis. These functions provide the main entry points for integrating Pyflakes into development workflows.
def check(codeString: str, filename: str, reporter=None) -> int: ...
def checkPath(filename: str, reporter=None) -> int: ...
def checkRecursive(paths: list, reporter) -> int: ...
def iterSourceCode(paths: list): ...
def isPythonFile(filename: str) -> bool: ...
def main(prog=None, args=None): ...Advanced code analysis engine that performs AST-based static analysis with comprehensive scope tracking, binding management, and AST node handling. The Checker class provides fine-grained control over the analysis process with 65+ methods covering all Python AST node types.
class Checker:
def __init__(self, tree, filename='(none)', builtins=None, withDoctest='PYFLAKES_DOCTEST' in os.environ, file_tokens=()): ...
# Core attributes and properties
messages: list
deadScopes: list
scopeStack: list
futuresAllowed: bool
annotationsFutureEnabled: bool
# Core analysis methods
def report(self, messageClass, *args, **kwargs): ...
def handleNode(self, node, parent): ...
def deferFunction(self, callable): ...
def addBinding(self, node, value): ...
# All AST node handlers (FUNCTIONDEF, IMPORT, CALL, etc.)
# Complete binding system (Assignment, Importation, etc.)
# Complete scope system (ModuleScope, FunctionScope, etc.)Comprehensive set of 48 structured warning and error classes representing different types of issues that can be detected in Python code. Each message type provides specific information about the problem and its location.
class Message:
def __init__(self, filename, loc): ...
class UndefinedName(Message): ...
class UnusedImport(Message): ...
class RedefinedWhileUnused(Message): ...
class FStringMissingPlaceholders(Message): ...
class PercentFormatInvalidFormat(Message): ...
# ... all 48 message types documentedFlexible output formatting system for presenting analysis results to users. The Reporter class can be customized to integrate Pyflakes into different development environments and workflows.
class Reporter:
def __init__(self, warningStream, errorStream): ...
def unexpectedError(self, filename, msg): ...
def syntaxError(self, filename, msg, lineno, offset, text): ...
def flake(self, message): ...