python.mdc•3.96 kB
---
description:
globs: *.py
alwaysApply: false
---
At the start of your every response, say "APPLIED PYTHON RULE".
# Python Language Rule
## Key Principles
- Write concise, technical Python code with accurate examples.
- Use functional programming; prefer functions over classes where possible.
- Prefer iteration and modularization over code duplication.
- Use descriptive variable names with auxiliary verbs (e.g., is_active, has_permission).
- Use lowercase_with_underscores for variables, functions, and file names.
- Follow PEP 8 style guidelines.
## Python Language Guidelines
- Use `def` for functions, `async def` for asynchronous operations.
- Use type hints for all function signatures and variables where helpful.
- Use f-strings for string formatting.
- Prefer list/dict comprehensions over loops when readable.
- Use `with` statements for resource management (files, connections).
- Use `*args` and `**kwargs` appropriately for flexible function signatures.
## Code Style (Ruff Configuration)
Follow these ruff settings for consistent style:
- Line length: 120 characters
- Target Python version: 3.9+
- Enable: pycodestyle errors/warnings (E, W), pyflakes (F), isort (I), flake8-comprehensions (C), flake8-bugbear (B)
- Known first-party imports: ["src"]
- Combine imports as single statements and force sort within sections
- Allow unused imports in `__init__.py` files
## Error Handling
- Handle errors and edge cases at the start of functions (guard clauses).
- Use early returns for error conditions; avoid deep nesting and unnecessary `else`.
- Place the happy path last in the function.
- Use specific exception types rather than bare `except:`.
- Implement proper exception handling with meaningful error messages.
- Use `try`/`except`/`finally` blocks appropriately.
- **Keep try-except blocks minimal** - cover only the specific operations that can fail.
## Code Structure
- **Put constants (strings, string templates, etc.) at the top of the file** after imports.
- Import standard library modules first, then third-party, then local imports.
- **Caller should come before callee** - define functions before they are called.
- Use docstrings for functions and classes following PEP 257.
- Keep functions small and focused on a single responsibility.
- Use meaningful function and variable names that explain intent.
- Avoid global variables; pass data through function parameters.
## Python Conventions
- Use `if __name__ == "__main__":` for script entry points.
- Prefer `is` and `is not` for `None` comparisons.
- Use `enumerate()` instead of manual counter variables.
- Use `zip()` for parallel iteration.
- Prefer `pathlib` over `os.path` for file operations.
- Use context managers for resource cleanup.
## Testing Guidelines
- Create comprehensive unit tests for all modules using the `pytest` framework.
- Place test files directly in the test directory without mirroring subdirectories (e.g., `src/main.py` → `test/test_main.py`).
- Use descriptive test method names that clearly indicate what is being tested.
- Include `__init__.py` files in test directories to make them proper Python packages.
- Mock external dependencies and side effects using `unittest.mock.patch` and `MagicMock`.
- Test both happy path and error conditions, including edge cases and exception handling.
- Use pytest fixtures for test setup and shared test data.
- Ensure tests are isolated and don't depend on external state or other tests.
- Include tests for CLI interfaces using `click.testing.CliRunner` with pytest fixtures.
- Verify function behavior, return types, and side effects in tests using assert statements.
- Use `pytest.mark.parametrize` for testing multiple input combinations efficiently.
- Add integration tests with `pytest.mark.integration` for complex workflows and API interactions.
- Use `conftest.py` for shared fixtures and test configuration.
- Organize tests in classes when testing related functionality together.