CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-types--pyflakes

Type stubs for pyflakes, a Python source code analysis tool designed to detect errors in Python files

Overview
Eval results
Files

report-generation.mddocs/

Report Generation

Configurable reporting system for displaying analysis results from pyflakes. The Reporter class handles output formatting and stream management for different types of messages and errors.

Capabilities

Reporter Class

Main class for handling output of analysis results, providing configurable streams for warnings and errors with extensible formatting.

class Reporter:
    """
    Reporter for pyflakes analysis results.
    
    Manages output streams and formatting for different types of messages
    including warnings, errors, and syntax errors.
    """
    
    def __init__(self, warningStream, errorStream) -> None:
        """
        Initialize reporter with output streams.
        
        Parameters:
        - warningStream: Output stream for warning messages
        - errorStream: Output stream for error messages
        """

Message Reporting

Methods for outputting different types of analysis results with appropriate formatting and stream routing.

def flake(self, message) -> None:
    """
    Report a pyflakes message (warning or error).
    
    Parameters:
    - message: Message instance to report
    
    Formats and outputs the message to the appropriate stream based on
    message type and severity.
    """

Usage Example:

from pyflakes.reporter import Reporter
from pyflakes.api import check
import sys
import io

# Create string buffers to capture output
warnings_buffer = io.StringIO()
errors_buffer = io.StringIO()

# Create reporter with custom streams
reporter = Reporter(warnings_buffer, errors_buffer)

# Check some code
code = """
import os  # Unused import
print(undefined_var)  # Undefined name
"""

result = check(code, "example.py", reporter)

# Get captured output
warnings_output = warnings_buffer.getvalue()
errors_output = errors_buffer.getvalue()

print("Warnings:", warnings_output)
print("Errors:", errors_output)

Syntax Error Reporting

Specialized method for reporting Python syntax errors with detailed location and context information.

def syntaxError(self, filename, msg, lineno, offset, text) -> None:
    """
    Report a Python syntax error.
    
    Parameters:
    - filename: Name of file containing syntax error
    - msg: Error message describing the syntax problem
    - lineno: Line number where error occurs
    - offset: Character offset within the line
    - text: Source text line containing the error
    
    Provides detailed syntax error reporting with line context and
    position indicators.
    """

Usage Example:

from pyflakes.reporter import Reporter
import sys

reporter = Reporter(sys.stderr, sys.stderr)

# Report a syntax error (typically called internally)
reporter.syntaxError(
    "badfile.py",
    "invalid syntax",
    lineno=5,
    offset=10,
    text="if x = 5:"  # Invalid assignment in if
)

Unexpected Error Reporting

Method for reporting unexpected internal errors during analysis, useful for debugging and error handling.

def unexpectedError(self, filename, msg) -> None:
    """
    Report an unexpected error during analysis.
    
    Parameters:
    - filename: Name of file being analyzed when error occurred
    - msg: Error message describing the unexpected condition
    
    Used for reporting internal errors or unexpected conditions that
    occur during the analysis process.
    """

Custom Reporter Implementation

You can extend the Reporter class to customize output formatting, add logging, or integrate with other tools:

from pyflakes.reporter import Reporter
import json
import sys

class JSONReporter(Reporter):
    """Custom reporter that outputs JSON-formatted results."""
    
    def __init__(self):
        super().__init__(sys.stdout, sys.stderr)
        self.results = []
    
    def flake(self, message):
        """Collect messages as structured data."""
        self.results.append({
            'type': type(message).__name__,
            'filename': message.filename,
            'line': message.lineno,
            'column': message.col,
            'message': str(message),
            'args': message.message_args
        })
    
    def syntaxError(self, filename, msg, lineno, offset, text):
        """Collect syntax errors as structured data."""
        self.results.append({
            'type': 'SyntaxError',
            'filename': filename,
            'line': lineno,
            'column': offset,
            'message': msg,
            'text': text
        })
    
    def output_json(self):
        """Output collected results as JSON."""
        print(json.dumps(self.results, indent=2))

# Usage
from pyflakes.api import check

reporter = JSONReporter()
check("import os\nprint(undefined)", "test.py", reporter)
reporter.output_json()

Integration with Analysis Workflow

The Reporter works seamlessly with all pyflakes analysis functions:

from pyflakes.api import check, checkPath, checkRecursive
from pyflakes.reporter import Reporter
import sys

# Create a reporter
reporter = Reporter(sys.stdout, sys.stderr)

# Use with different analysis functions
check("import unused", "string.py", reporter)
checkPath("myfile.py", reporter)
checkRecursive(["src/", "tests/"], reporter)

Output Stream Configuration

Different configurations for various use cases:

from pyflakes.reporter import Reporter
import sys
import io

# Standard output (warnings and errors to stderr)
reporter = Reporter(sys.stderr, sys.stderr)

# Separate streams
warnings_file = open("warnings.txt", "w")
errors_file = open("errors.txt", "w")
reporter = Reporter(warnings_file, errors_file)

# Capture to strings
warnings_buffer = io.StringIO()
errors_buffer = io.StringIO()
reporter = Reporter(warnings_buffer, errors_buffer)

# Silent reporter (no output)
null_stream = io.StringIO()
reporter = Reporter(null_stream, null_stream)

Install with Tessl CLI

npx tessl i tessl/pypi-types--pyflakes

docs

ast-checker.md

code-analysis.md

error-messages.md

index.md

report-generation.md

tile.json