|
| 1 | +# Agent Guidelines for Kazoo |
| 2 | + |
| 3 | +This document provides guidelines for agents and tools working with the Kazoo codebase. |
| 4 | + |
| 5 | +## Python Version Compatibility |
| 6 | + |
| 7 | +Kazoo supports Python 3.8+, with explicit targeting of py38, py39, py310, py311, py312, py313, and py314. When writing or modifying code: |
| 8 | + |
| 9 | +- **Use `from __future__ import annotations`** at the top of every module to enable postponed evaluation of type annotations (PEP 563). This allows using native Python type names rather than importing uppercase versions from `typing`. |
| 10 | +- Example: Use `list[str]` instead of `List[str]`, `dict[str, int]` instead of `Dict[str, int]`, etc. |
| 11 | +- This is required for py38+ compatibility when using modern type hint syntax. |
| 12 | + |
| 13 | +## Code Style |
| 14 | + |
| 15 | +### Type Hints |
| 16 | + |
| 17 | +- All functions and methods should have appropriate type hints. |
| 18 | +- Use native Python type syntax with `from __future__ import annotations` rather than `typing` module types where possible. |
| 19 | +- Example imports: |
| 20 | + ```python |
| 21 | + from __future__ import annotations |
| 22 | + from typing import Any, Optional, Callable, Union |
| 23 | + ``` |
| 24 | + |
| 25 | +### Constructor (`__init__`) Methods |
| 26 | + |
| 27 | +- `__init__` methods with arguments should **not** include a return type hint. |
| 28 | +- **Correct**: `def __init__(self, client: Any):` |
| 29 | +- **Incorrect**: `def __init__(self, client: Any) -> None:` |
| 30 | +- This is a repository-specific style rule to keep constructor signatures consistent and avoid unnecessary return annotations. |
| 31 | +- Note: `__init__` methods with no arguments (other than `self`) must include `-> None` |
| 32 | + |
| 33 | +### Line Length |
| 34 | + |
| 35 | +- Maximum line length: 79 characters (enforced by Black formatter). |
| 36 | +- Configuration: `tool.black` in `pyproject.toml` specifies `line-length = 79`. |
| 37 | + |
| 38 | +### Formatting |
| 39 | + |
| 40 | +- Code is formatted with Black. |
| 41 | +- Target versions: py38, py39, py310, py311, py312, py313, py314. |
| 42 | + |
| 43 | +## Type Checking |
| 44 | + |
| 45 | +Kazoo uses mypy for static type checking with strict type checking enabled. |
| 46 | + |
| 47 | +### Using mypy with VS Code |
| 48 | + |
| 49 | +You have to install the **Pylance** extension sadly, as a language server is needed for other extensions, and the default language server is broken for python 3.8 |
| 50 | + |
| 51 | +However, you should disable it by specifying |
| 52 | + |
| 53 | +```json |
| 54 | +{ |
| 55 | + "python.analysis.ignore": ["kazoo/**"] |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +Install the **mypy** extension directly: |
| 60 | + |
| 61 | +1. Install the extension: |
| 62 | + - Open VS Code Extensions (`Ctrl+Shift+X`) |
| 63 | + - Search for "mypy" and install the official mypy extension |
| 64 | + |
| 65 | +2. The mypy extension will automatically use the configuration from `pyproject.toml`, but it needs a couple of configuration changes: |
| 66 | +```json |
| 67 | + "mypy-type-checker.importStrategy": "fromEnvironment", |
| 68 | + "mypy-type-checker.args": [ "--exclude=(kazoo|docs)/[^/]+\\.py" ], |
| 69 | +``` |
| 70 | + |
| 71 | +3. To see type checking errors in the editor: |
| 72 | + - Errors appear as red squiggly underlines |
| 73 | + - Hover over errors to see detailed messages |
| 74 | + - Open the **Problems panel** (`Ctrl+Shift+M`) to view all errors |
| 75 | + |
| 76 | +### mypy Configuration |
| 77 | + |
| 78 | +Kazoo's mypy configuration (in `pyproject.toml` under `[tool.mypy]`) enforces strict type checking: |
| 79 | + |
| 80 | +**Strict Mode Settings:** |
| 81 | +- `strict = true`: Enables all strict type checking flags |
| 82 | +- `disallow_subclassing_any = true`: Prevents subclassing untyped classes |
| 83 | +- `disallow_untyped_calls = true`: All function calls must use typed functions |
| 84 | +- `disallow_untyped_defs = true`: All functions must have type hints |
| 85 | +- `disallow_incomplete_defs = true`: Type hints must be complete (no partial types) |
| 86 | +- `disallow_untyped_decorators = true`: Decorators must be typed |
| 87 | +- `check_untyped_defs = true`: Check bodies of untyped functions |
| 88 | +- `implicit_optional = false`: Optional types must be explicit |
| 89 | +- `strict_optional = true`: Strict handling of None types |
| 90 | + |
| 91 | +**Error Messages:** |
| 92 | +- `show_error_context = true`: Shows surrounding code context |
| 93 | +- `show_column_numbers = true`: Displays exact column numbers |
| 94 | +- `pretty = true`: Colorized output for better readability |
| 95 | +- `color_output = true`: Enables colored terminal output |
| 96 | +- `show_absolute_path = true`: Shows full file paths |
| 97 | + |
| 98 | +**Key Setting:** |
| 99 | +- `disallow_subclassing_any = true`: Prevents subclassing untyped classes, ensuring all external dependencies are properly typed or have stubs |
| 100 | + |
| 101 | +### Running mypy from Terminal |
| 102 | + |
| 103 | +Run type checking from the command line: |
| 104 | +```bash |
| 105 | +# Check the entire project |
| 106 | +mypy kazoo/ |
| 107 | + |
| 108 | +# Check specific files/directories |
| 109 | +mypy kazoo/client.py |
| 110 | +mypy kazoo/handlers/ |
| 111 | +``` |
| 112 | + |
| 113 | +## Testing |
| 114 | + |
| 115 | +- Tests use pytest with the following configuration: |
| 116 | + - Per-test timeout: 180 seconds |
| 117 | + - Verbosity: `-ra -v --color=yes` |
| 118 | + - Log level: INFO |
| 119 | +- Test files are located in `kazoo/tests/`. |
| 120 | + |
| 121 | +## Module Organization |
| 122 | + |
| 123 | +- **Core client**: `kazoo/client.py` |
| 124 | +- **Handlers** (async frameworks): `kazoo/handlers/` |
| 125 | + - `threading.py`: Standard threading handler |
| 126 | + - `gevent.py`: Gevent-based async handler |
| 127 | + - `eventlet.py`: Eventlet-based async handler |
| 128 | +- **Recipes** (higher-level abstractions): `kazoo/recipe/` |
| 129 | + - `barrier.py`, `election.py`, `lock.py`, `queue.py`, etc. |
| 130 | +- **Protocol**: `kazoo/protocol/` |
| 131 | + - Connection handling, serialization, state management |
| 132 | +- **Testing utilities**: `kazoo/testing/` |
| 133 | + - Test harness and common test utilities |
| 134 | + |
| 135 | +## Best Practices |
| 136 | + |
| 137 | +1. **Preserve backward compatibility**: Kazoo is a widely-used library; changes should not break existing APIs. |
| 138 | +2. **Document changes**: Update docstrings and add entries to `CHANGES.md` for significant modifications. |
| 139 | +3. **Run tests**: Use `pytest` or `tox` to ensure changes don't break existing functionality. |
| 140 | +4. **Type hints first**: Design type hints carefully; they are enforced by mypy and serve as API documentation. |
| 141 | +5. **Follow PEP 8**: Adhere to Python style guidelines as enforced by Black and checked by linters. |
0 commit comments