|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +This is a Python implementation of the Pipeline pattern, distributed as `thecodecrate-pipeline`. The package allows composing sequential stages into reusable, immutable pipelines for processing data. It's inspired by the PHP League Pipeline package. |
| 8 | + |
| 9 | +## Development Commands |
| 10 | + |
| 11 | +### Environment Setup |
| 12 | +```bash |
| 13 | +# The project uses uv for dependency management |
| 14 | +# Dependencies are managed in pyproject.toml under [dependency-groups] |
| 15 | +uv sync |
| 16 | +``` |
| 17 | + |
| 18 | +### Testing |
| 19 | +```bash |
| 20 | +# Run all tests |
| 21 | +pytest |
| 22 | + |
| 23 | +# Run tests with coverage |
| 24 | +pytest --cov |
| 25 | + |
| 26 | +# Run a specific test file |
| 27 | +pytest tests/test_pipeline.py |
| 28 | + |
| 29 | +# Run a specific test function |
| 30 | +pytest tests/test_pipeline.py::test_function_name |
| 31 | +``` |
| 32 | + |
| 33 | +### Code Quality |
| 34 | +```bash |
| 35 | +# Format code with ruff |
| 36 | +ruff format . |
| 37 | + |
| 38 | +# Lint with ruff |
| 39 | +ruff check . |
| 40 | + |
| 41 | +# Fix auto-fixable linting issues |
| 42 | +ruff check --fix . |
| 43 | + |
| 44 | +# Format with black (line length: 79) |
| 45 | +black . |
| 46 | + |
| 47 | +# Type checking is configured with strict mode in pyrightconfig.json |
| 48 | +# Type check manually: pyright (if installed) |
| 49 | +``` |
| 50 | + |
| 51 | +### Documentation |
| 52 | +```bash |
| 53 | +# Build documentation locally |
| 54 | +mkdocs serve |
| 55 | + |
| 56 | +# Documentation is built with mkdocs-material and auto-generates API docs |
| 57 | +# from docstrings using mkdocstrings-python |
| 58 | +``` |
| 59 | + |
| 60 | +### Version Management |
| 61 | +```bash |
| 62 | +# Bump version (uses bumpver) |
| 63 | +bumpver update --patch # 1.26.0 -> 1.26.1 |
| 64 | +bumpver update --minor # 1.26.0 -> 1.27.0 |
| 65 | +bumpver update --major # 1.26.0 -> 2.0.0 |
| 66 | + |
| 67 | +# Note: bumpver automatically commits and tags, but does NOT push |
| 68 | +``` |
| 69 | + |
| 70 | +## Architecture |
| 71 | + |
| 72 | +### Core Concepts |
| 73 | + |
| 74 | +The codebase implements a pipeline pattern with three main abstractions: |
| 75 | + |
| 76 | +1. **Stage**: A callable unit that transforms input to output (`StageInterface[T_in, T_out]`) |
| 77 | +2. **Pipeline**: An immutable chain of stages (`PipelineInterface[T_in, T_out]`) |
| 78 | +3. **Processor**: Controls how stages are executed (`ProcessorInterface[T_in, T_out]`) |
| 79 | + |
| 80 | +### Directory Structure |
| 81 | + |
| 82 | +``` |
| 83 | +src/ |
| 84 | +├── _lib/ # Internal implementation |
| 85 | +│ ├── pipeline/ # Core pipeline implementation |
| 86 | +│ │ ├── pipeline.py # Main Pipeline class |
| 87 | +│ │ ├── pipeline_factory.py # Factory for building pipelines |
| 88 | +│ │ ├── processor.py # Base Processor class |
| 89 | +│ │ ├── stage.py # Base Stage class |
| 90 | +│ │ ├── processors/ # Built-in processors |
| 91 | +│ │ │ ├── chained_processor.py |
| 92 | +│ │ │ └── ... |
| 93 | +│ │ └── traits/ # Mixins (Clonable, ActAsFactory) |
| 94 | +│ └── processors/ # Additional processor implementations |
| 95 | +│ ├── chained_processor/ # Processor that chains stages |
| 96 | +│ └── interruptible_processor/ # Processor with interruption support |
| 97 | +└── thecodecrate_pipeline/ # Public API package |
| 98 | + ├── __init__.py # Re-exports from _lib |
| 99 | + ├── processors/ # Public processor exports |
| 100 | + └── types/ # Public type exports |
| 101 | +``` |
| 102 | + |
| 103 | +### Key Design Patterns |
| 104 | + |
| 105 | +**Immutability**: Pipelines use copy-on-write semantics. The `pipe()` method creates a new pipeline instance with the added stage, preserving the original pipeline. |
| 106 | + |
| 107 | +**Traits System**: The codebase uses a trait-like pattern with mixins: |
| 108 | +- `Clonable`: Provides shallow cloning capability |
| 109 | +- `ActAsFactory`: Enables objects to act as factories for creating instances |
| 110 | + |
| 111 | +**Interface Segregation**: Each core concept has an interface (`*Interface`) and implementation, enabling custom implementations while maintaining type safety. |
| 112 | + |
| 113 | +**Async-First**: All processing is async (`async def process(...)`). The processor handles both sync and async callables transparently using `inspect.isawaitable()`. |
| 114 | + |
| 115 | +### Type System |
| 116 | + |
| 117 | +The codebase uses generic type variables for type safety: |
| 118 | +- `T_in`: Input type to a stage or pipeline |
| 119 | +- `T_out`: Output type from a stage or pipeline (defaults to `T_in`) |
| 120 | + |
| 121 | +Stages can transform types: |
| 122 | +```python |
| 123 | +Pipeline[int, str] # Takes int, returns str |
| 124 | +StageInterface[int, int] # Takes int, returns int |
| 125 | +``` |
| 126 | + |
| 127 | +### Processing Flow |
| 128 | + |
| 129 | +1. A Pipeline is created with stages (either via `.pipe()` or declaratively) |
| 130 | +2. When `.process(payload)` is called, the pipeline: |
| 131 | + - Instantiates stages if needed (converts classes to instances) |
| 132 | + - Delegates to the processor's `.process()` method |
| 133 | + - The processor iterates through stages, passing output to next stage |
| 134 | +3. Processors can customize execution (e.g., ChainedProcessor for error handling) |
| 135 | + |
| 136 | +### Stream Processing |
| 137 | + |
| 138 | +Pipelines support processing `AsyncIterator` streams, allowing real-time data transformation where each stage can yield results consumed immediately by the next stage. |
| 139 | + |
| 140 | +### PipelineFactory |
| 141 | + |
| 142 | +Because pipelines are immutable, `PipelineFactory` provides mutable stage collection during composition. It builds the final immutable pipeline via `.build()`. |
| 143 | + |
| 144 | +## Testing Notes |
| 145 | + |
| 146 | +- Test files use stub classes in `tests/stubs/` for consistent test fixtures |
| 147 | +- Tests are async-aware (configured via `pytest.ini` with `pytest-asyncio`) |
| 148 | +- Mock stages implement `StageInterface` for type safety |
| 149 | + |
| 150 | +## Python Version |
| 151 | + |
| 152 | +Requires Python 3.13+ (specified in pyproject.toml). |
| 153 | + |
| 154 | +## Package Distribution |
| 155 | + |
| 156 | +Built with `hatchling`. The wheel includes both `thecodecrate_pipeline` (public API) and `_api` packages (internal). The public package re-exports symbols from `_lib`. |
0 commit comments