Regenerate all MDC rules with fresh Exa data and December 2025 best practices

- Refreshed all 241 library rules with current best practices
- Updated Exa search results for all libraries with 2025 content
- Fixed --regenerate-all flag logic to properly process all libraries
- Removed duplicate astro entry in rules.json (merged tags)
- Added missing docker.mdc and vim.mdc files
- All rules now include modern patterns and anti-patterns
- Updated with framework-specific recommendations for 2025

Technical improvements:
- Fixed retry_failed_only logic in generate_mdc_files.py
- Improved progress tracking for regeneration scenarios
- Ensured proper handling of --refresh-exa flag
This commit is contained in:
sanjeed5
2025-12-17 22:40:28 +05:30
parent 331fb6ffac
commit 8fbf269085
244 changed files with 66701 additions and 52708 deletions
+176 -183
View File
@@ -1,224 +1,217 @@
---
description: This rule provides comprehensive best practices for developing with the Trio asynchronous I/O library in Python, covering code organization, performance, testing, and common pitfalls.
globs: *.py
description: This guide enforces best practices for using Trio, Python's structured concurrency library, ensuring robust, type-safe, and performant asynchronous applications.
globs: **/*.py
---
- Use `flake8-async` to check for issues in Trio code. `@file:flake8_async_rules.mdc`
- Utilize Trio's nursery feature for managing concurrent tasks to prevent orphaned tasks and improve error handling. Use `async with trio.open_nursery() as nursery: nursery.start_soon(my_task)`. See Trio documentation for advanced usage.
- Clearly distinguish between async and synchronous functions. Async functions should primarily be used for I/O-bound operations. CPU-bound tasks should run in separate processes or threads to avoid blocking the event loop.
- Leverage Trio's testing utilities (`trio.testing`) to ensure that your async code behaves as expected. Use tools like `trio.testing.MockClock` to control time in tests.
- Adhere to PEP 8 for consistent code formatting. Use a linter and formatter like `black` to ensure compliance.
- Always use absolute imports for better readability and maintainability: `import mypkg.sibling`. Use explicit relative imports (`from . import sibling`) only when necessary in complex package layouts.
- Avoid wildcard imports (`from <module> import *`).
- Place module-level dunder names (e.g., `__all__`, `__author__`, `__version__`) after the module docstring but before any import statements (except `from __future__` imports).
- Use 4 spaces for indentation.
- Limit lines to a maximum of 79 characters (or 99 if agreed upon within a team). For docstrings and comments, limit lines to 72 characters.
- Surround top-level function and class definitions with two blank lines. Method definitions inside a class are surrounded by a single blank line.
- Use blank lines in functions to indicate logical sections.
- Code in the core Python distribution should always use UTF-8.
- Follow the naming conventions as described in PEP 8. Class names should use CapWords. Function and variable names should be lowercase with underscores.
- Always use `self` for the first argument to instance methods and `cls` for the first argument to class methods.
- Use one leading underscore for non-public methods and instance variables. Use two leading underscores to invoke Python’s name mangling for avoiding name clashes with subclasses.
- Constants should be defined on a module level and written in all capital letters with underscores separating words.
- Always decide whether a class’s methods and instance variables should be public or non-public. If in doubt, choose non-public.
- Comparisons to singletons like `None` should always be done with `is` or `is not`, never the equality operators.
- Use `is not` operator rather than `not ... is`.
- When implementing ordering operations with rich comparisons, it is best to implement all six operations (`__eq__`, `__ne__`, `__lt__`, `__le__`, `__gt__`, `__ge__`).
- Always use a `def` statement instead of an assignment statement that binds a lambda expression directly to an identifier.
- Derive exceptions from `Exception` rather than `BaseException`. Design exception hierarchies based on the distinctions that code catching the exceptions is likely to need.
- Use exception chaining appropriately (`raise X from Y`).
- When catching exceptions, mention specific exceptions whenever possible instead of using a bare `except:` clause.
- Limit the `try` clause to the absolute minimum amount of code necessary.
- When a resource is local to a particular section of code, use a `with` statement to ensure it is cleaned up promptly and reliably after use.
- Be consistent in return statements. Either all `return` statements in a function should return an expression, or none of them should.
- Use `''.startswith()` and `''.endswith()` instead of string slicing to check for prefixes or suffixes.
- Object type comparisons should always use `isinstance()` instead of comparing types directly.
- For sequences (strings, lists, tuples), use the fact that empty sequences are false.
- Don’t write string literals that rely on significant trailing whitespace.
- Don’t compare boolean values to `True` or `False` using `==`.
- Function annotations should use PEP 484 syntax.
- Annotations for module-level variables, class and instance variables, and local variables should have a single space after the colon.
# trio Best Practices
## Code Organization and Structure
Trio is the definitive choice for new async-first Python projects in 2025, prioritizing usability and correctness through structured concurrency. Adhere to these guidelines for maintainable, high-performance code.
- **Directory Structure:**
- A typical project structure might look like this:
my_project/
├── src/
│ ├── __init__.py
│ ├── main.py # Entry point
│ ├── utils.py # Utility functions
│ ├── services/
│ │ ├── __init__.py
│ │ ├── http_service.py
│ │ └── db_service.py
│ └── models/
│ ├── __init__.py
│ └── user.py
├── tests/
│ ├── __init__.py
│ ├── test_main.py
│ ├── test_utils.py
│ ├── services/
│ │ ├── test_http_service.py
│ │ └── test_db_service.py
│ └── models/
│ └── test_user.py
├── README.md
├── pyproject.toml # Project configuration (poetry, pipenv)
└── .gitignore
- Use a `src` directory to hold the main application code. This helps separate application code from configuration and documentation.
- Keep tests in a separate `tests` directory, mirroring the structure of `src`.
## 1. Structured Concurrency with Nurseries
- **File Naming Conventions:**
- Use lowercase names for files and modules (e.g., `http_service.py`, `utils.py`).
- Test files should be prefixed with `test_` (e.g., `test_http_service.py`).
Always manage concurrent tasks within a `trio.open_nursery()`. This guarantees that all spawned tasks complete or are cancelled together, preventing hidden task leaks and simplifying error handling.
- **Module Organization:**
- Organize code into logical modules based on functionality (e.g., `services`, `models`, `utils`).
- Use `__init__.py` files to make directories importable as packages.
- Avoid circular dependencies between modules.
❌ **BAD: Fire-and-forget tasks**
```python
import trio
- **Component Architecture:**
- Consider using a layered architecture (e.g., presentation, business logic, data access) to separate concerns.
- Dependency Injection: Use dependency injection to make components more testable and reusable.
async def background_task():
await trio.sleep(100) # This task will run indefinitely, unmanaged
- **Code Splitting:**
- Break down large modules into smaller, more manageable files.
- Consider splitting modules based on functionality or responsibilities.
async def main():
# No nursery, no explicit management
trio.lowlevel.spawn_system_task(background_task)
print("Main task exiting, background task is orphaned.")
## Common Patterns and Anti-patterns
trio.run(main)
```
- **Design Patterns:**
- **Asynchronous Factory:** Use factories to create asynchronous resources (e.g., connections) to manage initialization efficiently.
python
async def create_db_connection():
conn = await connect_to_db()
return conn
async def main():
conn = await create_db_connection()
# Use the connection
- **Resource Pooling:** Implement resource pooling for database connections or network connections to reduce overhead.
✅ **GOOD: Tasks within a nursery**
```python
import trio
- **Recommended Approaches:**
- Use nurseries for structured concurrency.
- Use streams (`trio.Stream`) for communication between tasks.
- Use channels (`trio.QueueChannel`) for passing data between tasks.
async def managed_task(task_id: int):
print(f"Task {task_id} started.")
await trio.sleep(1)
print(f"Task {task_id} finished.")
- **Anti-patterns:**
- **Sleeping without Cancellation:** Avoid using `time.sleep()` directly, as it blocks the event loop and ignores cancellation. Use `await trio.sleep()` instead.
- **Long-Running Synchronous Operations:** Don't perform CPU-bound operations directly in async functions. Offload them to separate threads or processes.
- **Ignoring Cancellation:** Ensure that your async functions handle cancellation requests (`trio.Cancelled`).
- **Unstructured Concurrency:** Avoid spawning tasks without proper management (e.g., without using nurseries). This can lead to orphaned tasks and difficult debugging.
async def main():
async with trio.open_nursery() as nursery:
nursery.start_soon(managed_task, 1)
nursery.start_soon(managed_task, 2)
print("All tasks in nursery completed.")
- **State Management:**
- Immutable Data: Prefer immutable data structures to avoid race conditions and simplify reasoning about state.
- Task-Local Storage: Use `trio.TaskLocal` to store task-specific data.
- Thread-Safe Data Structures: If shared mutable state is necessary, use thread-safe data structures (e.g., `trio.Lock`, `trio.Semaphore`).
trio.run(main)
```
- **Error Handling:**
- Use `try...except` blocks to handle exceptions within async functions.
- Propagate exceptions appropriately to the nursery for proper error handling.
- Consider using exception groups to handle multiple exceptions that occur concurrently.
## 2. Explicit Cancellation and Timeouts
## Performance Considerations
Trio's cancellation model is robust. Use `cancel_scope` for explicit control over task lifetimes and `trio.Cancelled` for graceful cleanup.
- **Optimization Techniques:**
- Minimize context switching: Reduce unnecessary `await` calls.
- Use efficient data structures: Choose appropriate data structures for your specific use case.
- Avoid excessive copying: Use views or iterators when possible to avoid copying large data structures.
❌ **BAD: Ignoring cancellation or using raw `try...except`**
```python
import trio
- **Memory Management:**
- Release resources promptly: Use `with` statements or `try...finally` blocks to ensure that resources are released even if exceptions occur.
- Avoid circular references: Be mindful of potential circular references, which can prevent garbage collection.
async def long_running_operation():
try:
await trio.sleep(10)
print("Operation completed.")
except Exception: # Too broad, doesn't specifically handle cancellation
print("Operation failed somehow.")
## Security Best Practices
async def main():
with trio.move_on_after(0.1) as cancel_scope:
await long_running_operation()
if cancel_scope.cancelled_caught:
print("Operation was cancelled.")
- **Common Vulnerabilities:**
- **Race Conditions:** Be aware of potential race conditions when accessing shared mutable state.
- **Cancellation Errors:** Improper cancellation handling can lead to resource leaks or incorrect program behavior.
trio.run(main)
```
- **Input Validation:**
- Validate all external inputs to prevent injection attacks and other security vulnerabilities.
- Sanitize user inputs before using them in database queries or other sensitive operations.
✅ **GOOD: Handling `trio.Cancelled` for cleanup**
```python
import trio
- **Authentication and Authorization:**
- Use established authentication and authorization libraries.
- Implement proper access controls to protect sensitive data.
async def long_running_operation():
try:
print("Starting long operation...")
await trio.sleep(10)
print("Operation completed.")
except trio.Cancelled:
print("Operation was cancelled, performing cleanup.")
# Perform necessary cleanup here
raise # Re-raise to propagate cancellation
- **Data Protection:**
- Encrypt sensitive data at rest and in transit.
- Use secure communication protocols (e.g., HTTPS).
async def main():
async with trio.open_nursery() as nursery:
nursery.start_soon(long_running_operation)
await trio.sleep(0.1)
nursery.cancel_scope.cancel() # Explicitly cancel the nursery
- **Secure API Communication:**
- Validate all API requests and responses.
- Implement rate limiting to prevent abuse.
trio.run(main)
```
## Testing Approaches
## 3. I/O Hygiene: Never Block the Event Loop
- **Unit Testing:**
- Use `trio.testing` to write unit tests for async functions.
- Use `trio.testing.MockClock` to control time in tests.
- Mock external dependencies to isolate the code being tested.
Blocking calls (e.g., `time.sleep()`, synchronous file I/O, `requests.get()`) will starve the event loop and halt all other concurrent tasks. Use Trio-aware alternatives or offload to a thread.
- **Integration Testing:**
- Test the interaction between different components of your application.
- Use real or simulated external services to test the integration with external systems.
❌ **BAD: Blocking call in an `async def` function**
```python
import time
import trio
- **End-to-End Testing:**
- Test the entire application flow from the user interface to the database.
async def fetch_data_blocking():
print("Fetching data (blocking)...")
time.sleep(2) # Blocks the entire event loop!
print("Data fetched.")
return {"data": "example"}
- **Test Organization:**
- Organize tests in a directory structure that mirrors the structure of your application code.
- Write clear and concise test names that describe the behavior being tested.
async def main():
async with trio.open_nursery() as nursery:
nursery.start_soon(fetch_data_blocking)
nursery.start_soon(fetch_data_blocking) # These will run sequentially
- **Mocking and Stubbing:**
- Use mocking libraries like `unittest.mock` to replace external dependencies with mock objects.
- Use stubbing to provide predefined responses for external dependencies.
trio.run(main)
```
## Common Pitfalls and Gotchas
✅ **GOOD: Offloading blocking calls with `trio.to_thread.run_sync`**
```python
import time
import trio
- **Frequent Mistakes:**
- Blocking the event loop with synchronous operations.
- Ignoring cancellation requests.
- Using `time.sleep()` instead of `trio.sleep()`.
- Not handling exceptions properly.
def _blocking_fetch_data(): # This is a regular sync function
print("Fetching data (blocking in thread)...")
time.sleep(2)
print("Data fetched in thread.")
return {"data": "example"}
- **Edge Cases:**
- Handling timeouts and deadlines.
- Dealing with cancellation in complex workflows.
- Managing resources in the presence of exceptions and cancellations.
async def fetch_data_non_blocking():
return await trio.to_thread.run_sync(_blocking_fetch_data)
- **Version-Specific Issues:**
- Be aware of any known bugs or limitations in specific versions of Trio.
async def main():
async with trio.open_nursery() as nursery:
nursery.start_soon(fetch_data_non_blocking)
nursery.start_soon(fetch_data_non_blocking) # These will run concurrently
- **Compatibility Concerns:**
- Ensure compatibility between Trio and other libraries you are using.
trio.run(main)
```
For network I/O, use `trio.socket`, `trio-http-client`, or `httpx` (with `trio` backend).
- **Debugging Strategies:**
- Use debuggers like `pdb` or `ipdb` to step through your code and inspect variables.
- Use logging to track the execution flow of your application.
- Use Trio's built-in debugging tools to identify performance bottlenecks and other issues.
## 4. Type Safety and Linting
## Tooling and Environment
Mandate type hints for all `async def` functions, variables, and return types. Use `mypy --strict` and `flake8-async` (or Ruff with `ASYNC` rules) to catch common async-related errors early.
- **Recommended Development Tools:**
- VS Code with the Python extension.
- PyCharm.
- IPython or Jupyter Notebook for interactive development.
❌ **BAD: Missing type hints, potential runtime errors**
```python
import trio
- **Build Configuration:**
- Use a build system like `poetry` or `pipenv` to manage dependencies.
async def process_item(item): # 'item' has no type hint
await trio.sleep(0.1)
return item * 2
- **Linting and Formatting:**
- Use `flake8` and `black` to ensure consistent code style.
- Configure your editor to automatically format code on save.
async def main():
result = await process_item("text") # Will fail at runtime if 'item' is str
print(result)
- **Deployment:**
- Use a process manager like `systemd` or `supervisor` to manage your application.
trio.run(main)
```
- **CI/CD Integration:**
- Use CI/CD tools like GitHub Actions or GitLab CI to automate testing and deployment.
✅ **GOOD: Comprehensive type hints**
```python
import trio
async def process_item(item: int) -> int:
await trio.sleep(0.1)
return item * 2
async def main():
result = await process_item(5) # Type checker will catch incorrect usage
print(result)
trio.run(main)
```
Configure `ruff` to enable `ASYNC` rules (e.g., `select = ["E", "F", "ASYNC"]` in `pyproject.toml`).
## 5. Interoperability with AnyIO
When building libraries or applications that need to be framework-agnostic (e.g., compatible with FastAPI, which uses AnyIO), write your async code using AnyIO primitives. This keeps your codebase portable across Trio, asyncio, and Curio.
```python
# Using AnyIO for portable async code
import anyio
async def do_something_concurrently():
async with anyio.create_task_group() as tg:
tg.start_soon(anyio.sleep, 1)
tg.start_soon(anyio.sleep, 2)
print("AnyIO tasks completed.")
async def main():
await do_something_concurrently()
# Run with Trio backend
anyio.run(main, backend="trio")
```
## 6. Testing Approaches
Leverage `trio.testing` for reliable and deterministic tests of your asynchronous code.
```python
import trio
import trio.testing
import pytest
async def worker(value: int) -> int:
await trio.sleep(0.01)
return value * 2
@pytest.mark.trio
async def test_worker_doubles_value():
# Use trio.testing.wait_all_tasks_blocked() for deterministic execution
async with trio.open_nursery() as nursery:
send_channel, receive_channel = trio.open_memory_channel(0)
nursery.start_soon(lambda: nursery.start_soon(worker, 5)) # Example task
await trio.testing.wait_all_tasks_blocked() # Ensures tasks yield
# Assertions about task state or results
assert await worker(3) == 6
```