Hi, I'm working on creating a Cursor rule to ensure that generated code follows all our linting and coding standards. However, I'm still not able to get it to work the way I’d like. This would be a huge help to me and my team, as we currently spend a lot of time fixing mypy and other linting issues.
Here's the Cursor rule I've written so far—I'd really appreciate any feedback or suggestions on how to improve it to boost my productivity:
You are an expert in Python, FastAPI, and scalable API development.
## Git Usage
- use the following prefix for commit messages followed by a colon and a space
- "fix" for bug fixes
- "feat" for new features
- "docs" for documentation changes
- "style" for formatting changes
- "refactor" for code refactoring
- "test" for adding missing tests
- "chore" for chore tasks
- when determining the commit message prefix, pick the most relevant prefix from the list above
- use lower case for commit messages
- the commit message should also include a list of the changes made in the commit after the summary line if the changes are not self explanatory
## Python/FastAPI Coding Standards
**General:**
- Use FastAPI best practices for API endpoints, dependency injection, and response models.
- All new code and edits must pass `ruff` and `mypy` checks with the strict settings defined in `pyproject.toml`.
- Use only absolute imports; relative imports are banned.
- All functions, methods, and classes must have type annotations.
- Docstrings are required for all public modules, classes, and functions (Google style preferred).
- Line length must not exceed 88 characters.
- Use f-strings for string formatting.
- Avoid magic values; use named constants or enums.
- Do not use `assert` in production code (allowed in tests).
- Do not use lambda assignment.
- Do not include TODOs without an author.
**Linting & Typing:**
- Code must not introduce new ruff or mypy errors.
- Use `ruff` for linting and formatting; auto-fix where possible.
- All code must be type-checked with mypy using the strict settings in `pyproject.toml`.
**Testing:**
- Place all tests in the `tests/` directory, using the `test_*.py` naming convention.
- Use `pytest` and `pytest-asyncio` for all tests.
- Test functions and classes do not require docstrings, but including them is encouraged for clarity.
- Use fixtures for setup/teardown.
- Use descriptive test names and avoid magic values in tests.
- Mock external dependencies using `pytest-mock`, `AsyncMock`, or similar.
- Use `assert` for test validation.
- All code and tests must be 100% free of mypy and ruff errors, including negative/exception tests. If a test cannot be written without a type error, use a type-safe alternative or explain why.
**Examples:**
```python
# Example FastAPI endpoint
from fastapi import APIRouter, Depends
from pydantic import BaseModel
router = APIRouter()
class Item(BaseModel):
name: str
value: int
@router.post("/items/", response_model=Item)
async def create_item(item: Item) -> Item:
"""
Create a new item.
Args:
item (Item): The item to create.
Returns:
Item: The created item.
"""
return item
```
```python
# Example test
import pytest
from fastapi.testclient import TestClient
from llm_rate_limit_handler.main import app
client = TestClient(app)
def test_create_item() -> None:
"""Test creating an item endpoint."""
response = client.post("/items/", json={"name": "foo", "value": 42})
assert response.status_code == 200
assert response.json() == {"name": "foo", "value": 42}
```
def test_header_config_immutability() -> None:
"""Test that HeaderConfig is immutable (frozen=True)."""
header = HeaderConfig(name="test-header")
# Attempting to modify should raise an error
with pytest.raises(dataclasses.FrozenInstanceError):
object.__setattr__(header, "name", "new-name")