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

code-analysis.mddocs/

Code Analysis API

High-level functions for checking Python code that provide the main entry points for pyflakes functionality. These functions handle code strings, individual files, and directory traversal with integrated error reporting.

Capabilities

Code String Analysis

Check Python code directly from strings, useful for runtime code validation, editor integrations, and dynamic analysis workflows.

def check(codeString: str, filename: str, reporter: Reporter | None = ...) -> int:
    """
    Check Python code string for errors.
    
    Parameters:
    - codeString: str, Python source code to analyze
    - filename: str, filename for error reporting (can be virtual)
    - reporter: Reporter | None, output handler for messages (None creates default)
    
    Returns:
    int: Number of errors found
    """

Usage Example:

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

code = """
import os
import sys  # Unused import
x = undefined_variable  # Undefined name
"""

reporter = Reporter(sys.stderr, sys.stderr)
error_count = check(code, "example.py", reporter)
print(f"Found {error_count} issues")

File Analysis

Analyze individual Python files on disk, automatically handling file reading and encoding detection.

def checkPath(filename, reporter: Reporter | None = ...) -> int:
    """
    Check Python file at given path.
    
    Parameters:
    - filename: path to Python file to analyze
    - reporter: Reporter | None, output handler for messages
    
    Returns:
    int: Number of errors found
    """

Usage Example:

from pyflakes.api import checkPath
from pyflakes.reporter import Reporter
import sys

reporter = Reporter(sys.stderr, sys.stderr)
error_count = checkPath("mymodule.py", reporter)
if error_count == 0:
    print("No issues found!")

Directory Tree Analysis

Recursively analyze Python files in directory trees, with automatic Python file detection and parallel processing capabilities.

def checkRecursive(paths: Iterable[Any], reporter: Reporter) -> int:
    """
    Recursively check Python files in given paths.
    
    Parameters:
    - paths: Iterable[Any], directories and files to analyze recursively
    - reporter: Reporter, output handler for messages (required)
    
    Returns:
    int: Total number of errors found across all files
    """

Usage Example:

from pyflakes.api import checkRecursive
from pyflakes.reporter import Reporter
import sys

reporter = Reporter(sys.stderr, sys.stderr)
paths = ["src/", "tests/", "scripts/single_file.py"]
total_errors = checkRecursive(paths, reporter)
print(f"Total errors across all files: {total_errors}")

Python File Detection

Utility function to determine if a given file is a Python source file, checking file extensions and shebang lines.

def isPythonFile(filename) -> bool:
    """
    Check if a file is a Python source file.
    
    Parameters:
    - filename: path to file to check
    
    Returns:
    bool: True if file is a Python source file, False otherwise
    """

Usage Example:

from pyflakes.api import isPythonFile

if isPythonFile("script.py"):
    print("This is a Python file")

if isPythonFile("#!/usr/bin/env python\nprint('hello')"):
    print("This has a Python shebang")

Source Code Iteration

Iterator for traversing and collecting Python source files from mixed path collections, handling both files and directories.

def iterSourceCode(paths: Iterable[Any]) -> Iterator[Any]:
    """
    Iterate over Python source code files in given paths.
    
    Parameters:
    - paths: Iterable[Any], collection of files and directories
    
    Yields:
    Iterator[Any]: Python source files found in paths
    """

Command Line Interface

Main entry point for command-line usage, handling argument parsing, output formatting, and exit codes.

def main(prog: str | None = ..., args: Sequence[Any] | None = ...) -> None:
    """
    Main command-line entry point.
    
    Parameters:
    - prog: str | None, program name for help text
    - args: Sequence[Any] | None, command line arguments (None uses sys.argv)
    
    Returns:
    None: Exits with appropriate code based on analysis results
    """

Constants

Python Detection Pattern

PYTHON_SHEBANG_REGEX: Pattern[bytes]

Regular expression pattern used for detecting Python files by shebang line, supporting various Python interpreter names and paths.

Module Exports

The module explicitly exports the following functions:

__all__ = ["check", "checkPath", "checkRecursive", "isPythonFile", "iterSourceCode", "main"]

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