CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pep257

Python docstring style checker for PEP 257 compliance

72

1.04x
Overview
Eval results
Files

error-checking.mddocs/

Error Checking and Validation

The error checking system provides the main API for validating Python files against PEP 257 docstring standards. It includes the primary check() function for programmatic use and the PEP257Checker class for detailed source code analysis.

Capabilities

Primary Check Function

The main entry point for checking files against PEP 257 compliance. Returns a generator of error objects for violations found in the specified files.

def check(filenames, select=None, ignore=None):
    """
    Generate PEP 257 errors that exist in filenames iterable.
    
    Parameters:
    - filenames: iterable of str, file paths to check
    - select: list of str, error codes to check for (mutually exclusive with ignore)
    - ignore: list of str, error codes to ignore (mutually exclusive with select)
    
    Returns:
    generator: Error objects for each violation found
    
    Raises:
    IllegalConfiguration: if both select and ignore are provided
    """

Usage examples:

import pep257

# Check single file
errors = list(pep257.check(['my_module.py']))

# Check multiple files  
errors = list(pep257.check(['module1.py', 'module2.py']))

# Check with specific error codes
errors = list(pep257.check(['my_module.py'], select=['D100', 'D101', 'D102']))

# Check while ignoring specific errors
errors = list(pep257.check(['my_module.py'], ignore=['D203', 'D213']))

Source Code Checker

The PEP257Checker class provides detailed analysis of Python source code strings, applying all validation rules and returning comprehensive error reports.

class PEP257Checker:
    """
    Checker for PEP 257 docstring compliance.
    
    Implements all error categories:
    - D10x: Missing docstrings
    - D20x: Whitespace issues  
    - D30x: Docstring formatting
    - D40x: Docstring content issues
    """
    
    def check_source(self, source, filename):
        """
        Check source code string for docstring violations.
        
        Parameters:
        - source: str, Python source code to analyze
        - filename: str, filename for error reporting
        
        Returns:
        generator: Error objects for violations found
        """
    
    @property
    def checks(self):
        """
        Get all available check methods.
        
        Returns:
        list: Check methods sorted by terminal priority
        """

Usage examples:

from pep257 import PEP257Checker

checker = PEP257Checker()

# Check source code string
source_code = '''
def hello():
    print("Hello, world!")
'''

errors = list(checker.check_source(source_code, 'example.py'))
for error in errors:
    print(f"{error.filename}:{error.line} {error.code}: {error.message}")

Error Objects

Error objects represent individual docstring violations with detailed context and formatting options.

class Error:
    """
    Represents a docstring style error.
    
    Attributes:
    - code: str, error code (e.g., 'D100')
    - short_desc: str, brief error description
    - context: str, contextual information template
    - parameters: tuple, parameters for context formatting
    - definition: Definition, code definition where error occurred
    - explanation: str, detailed explanation of the error
    """
    
    # Class attributes for output formatting
    explain = False  # Show detailed explanations
    source = False   # Show source code context
    
    def set_context(self, definition, explanation):
        """
        Set the context for this error.
        
        Parameters:
        - definition: Definition, code definition object
        - explanation: str, detailed explanation
        """
    
    @property
    def filename(self):
        """str: Filename where error occurred."""
    
    @property  
    def line(self):
        """int: Line number where error occurred."""
    
    @property
    def message(self):
        """str: Formatted error message."""
    
    @property
    def lines(self):
        """str: Source code lines with line numbers."""

Error Registry System

The error registry organizes all available error types into hierarchical groups and provides utilities for working with error codes.

class ErrorRegistry:
    """Registry for all error types and error groups."""
    
    groups = []  # List of ErrorGroup instances
    
    @classmethod
    def create_group(cls, prefix, name):
        """
        Create a new error group.
        
        Parameters:
        - prefix: str, error code prefix (e.g., 'D1')
        - name: str, descriptive group name
        
        Returns:
        ErrorGroup: New error group instance
        """
    
    @classmethod
    def get_error_codes(cls):
        """
        Get all registered error codes.
        
        Returns:
        generator: All available error codes
        """
    
    @classmethod
    def to_rst(cls):
        """
        Generate RST table of all errors.
        
        Returns:
        str: Formatted RST table
        """

class ErrorGroup:
    """Represents a group of related errors."""
    
    def __init__(self, prefix, name):
        """
        Initialize error group.
        
        Parameters:
        - prefix: str, error code prefix
        - name: str, group name
        """
    
    def create_error(self, error_code, error_desc, error_context=None):
        """
        Create a new error type in this group.
        
        Parameters:
        - error_code: str, unique error code
        - error_desc: str, error description
        - error_context: str, optional context template
        
        Returns:
        type: New Error subclass
        """

Pre-defined Error Groups

# Missing Docstrings
D1xx = ErrorRegistry.create_group('D1', 'Missing Docstrings')
D100 = D1xx.create_error('D100', 'Missing docstring in public module')
D101 = D1xx.create_error('D101', 'Missing docstring in public class')
D102 = D1xx.create_error('D102', 'Missing docstring in public method')
D103 = D1xx.create_error('D103', 'Missing docstring in public function')
D104 = D1xx.create_error('D104', 'Missing docstring in public package')
D105 = D1xx.create_error('D105', 'Missing docstring in magic method')

# Whitespace Issues
D2xx = ErrorRegistry.create_group('D2', 'Whitespace Issues')

# Quote Issues  
D3xx = ErrorRegistry.create_group('D3', 'Quotes Issues')

# Content Issues
D4xx = ErrorRegistry.create_group('D4', 'Docstring Content Issues')

Command Line Interface

def main():
    """Main entry point for command-line interface."""

def run_pep257():
    """
    Main runner function with configuration parsing.
    
    Returns:
    int: Exit code (0=no violations, 1=violations, 2=invalid options)
    """

Conventions

conventions = {
    'pep257': set  # Set of error codes for PEP 257 convention
}

The pep257 convention includes all error codes except D203 (which conflicts with D211).

Utility Functions

def setup_stream_handlers(conf):
    """
    Setup logging stream handlers according to configuration.
    
    Parameters:
    - conf: RunConfiguration, logging configuration
    """

Install with Tessl CLI

npx tessl i tessl/pypi-pep257

docs

configuration.md

error-checking.md

index.md

parsing.md

tile.json