Coding Style - nshaibu/volnux GitHub Wiki
Python Code Style Guide
Overview
This document defines the coding standards for our Python project. All contributors must adhere to these guidelines to ensure code quality, maintainability, and consistency across the codebase.
Code Formatting
Black Formatter
All Python code must be formatted using Black with default settings.
# Format a single file
black your_file.py
# Format entire project
black .
# Check formatting without modifying files
black --check .
Configuration: Black uses its default configuration (88 character line length). Do not override these defaults.
Type Annotations
Static Type Checking
We use mypy for static type analysis. All code must pass mypy checks with strict mode enabled.
# Run mypy on the project
mypy .
Type Annotation Requirements
All functions and methods must have complete type annotations, including:
- All parameters
- Return types
- Class attributes
- Module-level variables (when appropriate)
Examples
from typing import List, Dict, Optional, Tuple
def process_data(items: List[str], threshold: int = 10) -> Dict[str, int]:
"""Process items and return frequency counts."""
...
class DataProcessor:
cache_size: int
def __init__(self, cache_size: int = 100) -> None:
"""Initialize the data processor."""
self.cache_size = cache_size
async def fetch_data(self, url: str, timeout: Optional[float] = None) -> bytes:
"""Fetch data from the given URL."""
...
Type Hints Best Practices
- Use
typingmodule types for complex structures - Use
Optional[T]for values that can beNone - Use
Union[T1, T2]sparingly; prefer more specific types - Use
typing.Protocolfor structural subtyping - Use
typing.TypeVarfor generic types
Documentation
Docstring Requirements
Every function and method must have a docstring following the format below.
Docstring Format
def function_name(param1: str, param2: int) -> Tuple[List[str], List[Exception]]:
"""Brief one-line summary of what the function does.
Optional longer description providing more context about the function's
behavior, algorithms used, or important implementation details.
Args:
param1: Description of first parameter
param2: Description of second parameter
Returns:
Description of return value
Raises:
SpecificException: When this specific error occurs
AnotherException: When this other error occurs
Exception: For any other critical failures
"""
Docstring Sections
- Summary (Required): One-line description
- Description (Optional): Extended explanation if needed
- Args (Required if function has parameters): List all parameters
- Returns (Required if function returns a value): Describe the return value
- Raises (Required if function can raise exceptions): List all exceptions
Complete Example
from typing import Tuple, List
async def execute_tasks(
tasks: List[Task],
timeout: float = 30.0,
max_retries: int = 3
) -> Tuple[List[Result], List[Exception]]:
"""Execute the tasks asynchronously.
This function processes a list of tasks concurrently and collects
both successful results and any errors that occurred during execution.
Args:
tasks: List of Task objects to execute
timeout: Maximum execution time in seconds
max_retries: Number of retry attempts for failed tasks
Returns:
Tuple of (results, errors) from task execution
Raises:
ExecutionTimeoutError: If execution exceeds timeout
ExecutionError: If execution fails due to runtime errors
Exception: If execution fails critically
"""
...
Class Docstrings
Classes must also include docstrings:
class TaskExecutor:
"""Manages asynchronous task execution with retry logic.
This class provides a robust task execution framework with support
for timeouts, retries, and error handling.
Attributes:
max_workers: Maximum number of concurrent workers
default_timeout: Default timeout for task execution
"""
max_workers: int
default_timeout: float
def __init__(self, max_workers: int = 10, default_timeout: float = 30.0) -> None:
"""Initialize the task executor.
Args:
max_workers: Maximum number of concurrent workers
default_timeout: Default timeout in seconds
"""
self.max_workers = max_workers
self.default_timeout = default_timeout
Module Docstrings
Every module should have a docstring at the top:
"""Task execution utilities for asynchronous processing.
This module provides classes and functions for executing tasks
concurrently with support for timeouts, retries, and error handling.
"""
from typing import List
...
Code Organization
Import Order
Organize imports in the following order (with blank lines between groups):
- Standard library imports
- Third-party library imports
- Local application imports
import os
import sys
from typing import List, Dict
import numpy as np
import pandas as pd
from myproject.core import Engine
from myproject.utils import helpers
Use isort to automatically organize imports:
isort .
File Structure
Organize files in the following order:
- Module docstring
- Imports
- Constants
- Type definitions
- Classes
- Functions
Naming Conventions
- Functions and variables:
snake_case - Classes:
PascalCase - Constants:
UPPER_SNAKE_CASE - Private members:
_leading_underscore - Protected members:
_single_leading_underscore - Type variables:
PascalCasewithTprefix:TValue,TKey
Pre-commit Checks
Before committing code, ensure:
- ✅ Code is formatted with Black:
black . - ✅ Imports are sorted:
isort . - ✅ Type checks pass:
mypy . - ✅ All functions have type annotations
- ✅ All functions have complete docstrings
- ✅ Tests pass:
pytest