|
| 1 | +--- |
| 2 | +description: |
| 3 | +globs: |
| 4 | +alwaysApply: false |
| 5 | +--- |
| 6 | +# Best Practices Guide |
| 7 | + |
| 8 | +This document outlines the core best practices and patterns used in our codebase. |
| 9 | + |
| 10 | +## Type Hints |
| 11 | + |
| 12 | +1. **Always Use Type Hints** |
| 13 | + - Every function parameter must be typed |
| 14 | + - Every function return must be typed |
| 15 | + - Use type hints for all variables where type is not obvious |
| 16 | + |
| 17 | +2. **StrEnum** |
| 18 | + - Import StrEnum from pipelex.types |
| 19 | + ```python |
| 20 | + from pipelex.types import StrEnum |
| 21 | + |
| 22 | + class ModelType(StrEnum): |
| 23 | + GPT4 = "gpt-4" |
| 24 | + GPT35 = "gpt-3.5-turbo" |
| 25 | + ``` |
| 26 | + |
| 27 | +## Factory Pattern |
| 28 | + |
| 29 | +1. **Use Factory Pattern for Object Creation** |
| 30 | + - Create factories when dealing with multiple implementations |
| 31 | + |
| 32 | +## Documentation |
| 33 | + |
| 34 | +1. **Docstring Format** |
| 35 | + - Quick description of the function/class |
| 36 | + - List args and their types |
| 37 | + - Document return values |
| 38 | + - Example: |
| 39 | + ```python |
| 40 | + def process_image(image_path: str, size: Tuple[int, int]) -> bytes: |
| 41 | + """Process and resize an image. |
| 42 | + |
| 43 | + Args: |
| 44 | + image_path: Path to the source image |
| 45 | + size: Tuple of (width, height) for resizing |
| 46 | + |
| 47 | + Returns: |
| 48 | + Processed image as bytes |
| 49 | + """ |
| 50 | + pass |
| 51 | + ``` |
| 52 | + |
| 53 | +2. **Class Documentation** |
| 54 | + - Document class purpose and behavior |
| 55 | + - Include examples if complex |
| 56 | + ```python |
| 57 | + class ImageProcessor: |
| 58 | + """Handles image processing operations. |
| 59 | + |
| 60 | + Provides methods for resizing, converting, and optimizing images. |
| 61 | + """ |
| 62 | + ``` |
| 63 | + |
| 64 | +## Custom Exceptions |
| 65 | + |
| 66 | +1. **Graceful Error Handling** |
| 67 | + - Use try/except blocks with specific exceptions |
| 68 | + - Convert third-party exceptions to custom ones |
| 69 | + ```python |
| 70 | + try: |
| 71 | + from fal_client import AsyncClient as FalAsyncClient |
| 72 | + except ImportError as exc: |
| 73 | + raise MissingDependencyError( |
| 74 | + "fal-client", "fal", |
| 75 | + "The fal-client SDK is required to use FAL models." |
| 76 | + ) from exc |
| 77 | + ``` |
0 commit comments