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
+253 -222
View File
@@ -1,276 +1,307 @@
---
description: Comprehensive guide for using Flake8 effectively in Python projects, covering code style, error prevention, security, testing, and optimization. It outlines best practices, patterns, and common pitfalls to help developers maintain high code quality and adherence to standards.
globs: *.py
description: This guide provides definitive, actionable best practices for configuring and using flake8 to enforce Python code quality, style, and catch common errors.
globs: **/*.py
---
# Flake8 Best Practices: A Comprehensive Guide
# flake8 Best Practices
`flake8` is your essential Python linter, combining `pycodestyle`, `pyflakes`, and `mccabe` complexity checks with a powerful plugin ecosystem. It ensures your codebase adheres to PEP 8, catches common bugs, and maintains high quality. This guide outlines the definitive approach to using `flake8` effectively.
## 1. Configuration: The Single Source of Truth
This document provides a detailed guide on effectively using Flake8 in Python projects to maintain high code quality, enforce coding standards, and prevent common errors. It covers various aspects, including code organization, common patterns, performance considerations, security best practices, testing approaches, common pitfalls, and tooling.
Always configure `flake8` at the repository root using `pyproject.toml`. This centralizes tooling configuration and ensures consistent behavior across all environments. Avoid `.flake8` or `setup.cfg` for new projects.
## 1. Code Organization and Structure
**✅ GOOD: `pyproject.toml`**
### Directory Structure Best Practices
```toml
# pyproject.toml
[tool.flake8]
max-line-length = 88 # Black's default, ensures compatibility
extend-ignore = [
"E203", # Black compatibility: whitespace before ':'
"W503", # Black compatibility: line break before binary operator
"E501", # Ignore line length for specific files/legacy (use sparingly)
]
exclude = [
".git",
"__pycache__",
"build",
"dist",
"venv",
".venv",
"*.egg-info",
"docs", # Exclude documentation build artifacts
"migrations", # Django migrations often have long lines
]
max-complexity = 10 # Enforce reasonable function complexity
per-file-ignores = [
"src/legacy_module.py:E501,F401", # Ignore specific errors in legacy files
"tests/*:S101", # Allow assert for tests (flake8-bandit)
]
# Enable plugins (install them first: pip install flake8-bandit flake8-bugbear flake8-mypy)
enable-extensions = ["B", "B9", "S", "M"] # Bugbear, Bandit, Mypy
```
**❌ BAD: `.flake8` (Outdated)**
```ini
# .flake8 (Avoid this for new projects)
[flake8]
max-line-length = 88
ignore = E203,W503
exclude = .git,__pycache__,build,dist,venv,.venv,*.egg-info,docs,migrations
max-complexity = 10
```
## 2. Core Checks: What `flake8` Enforces
`flake8` combines `pycodestyle` (PEP 8 style), `pyflakes` (common bugs), and `mccabe` (cyclomatic complexity).
### a. `pycodestyle` (E/W codes) - Style Enforcement
**E501: Line Too Long**
Keep lines concise for readability. Pair `flake8` with `black` or `ruff format` for automatic formatting.
**❌ BAD**
```python
def calculate_complex_value(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10):
return (param1 + param2) * (param3 - param4) / (param5 + param6) - (param7 * param8) + param9 - param10 # E501
```
**✅ GOOD**
```python
def calculate_complex_value(
param1, param2, param3, param4, param5,
param6, param7, param8, param9, param10
):
intermediate_sum = (param1 + param2) * (param3 - param4)
intermediate_diff = (param5 + param6) - (param7 * param8)
return intermediate_sum / intermediate_diff + param9 - param10
```
### b. `pyflakes` (F codes) - Bug Detection
**F401: Unused Import**
Remove unused imports to keep your code clean and prevent circular dependencies.
**❌ BAD**
```python
import os # F401
import sys
def greet(name):
print(f"Hello, {name}!")
```
**✅ GOOD**
```python
import sys
def greet(name):
print(f"Hello, {name}!")
```
### c. `mccabe` (C901) - Cyclomatic Complexity
High cyclomatic complexity indicates a function is doing too much. Refactor complex functions into smaller, focused units. Aim for `max-complexity = 10`.
**❌ BAD**
```python
def process_data(data): # C901
if data is None:
return None
if not isinstance(data, list):
return []
processed = []
for item in data:
if isinstance(item, dict):
if "value" in item and item["value"] > 0:
processed.append(item["value"] * 2)
elif "default" in item:
processed.append(item["default"])
elif isinstance(item, int) and item % 2 == 0:
processed.append(item / 2)
else:
processed.append(item)
return processed
```
**✅ GOOD**
- **Flat is better than nested (but not *too* flat):** Aim for a balance. A deeply nested structure can be hard to navigate, but a completely flat structure becomes unwieldy for larger projects.
- **Package-oriented:** Organize your code into packages (directories with `__init__.py` files) representing logical components of your application.
- **Separate concerns:** Keep source code, tests, documentation, and other assets in separate top-level directories (e.g., `src`, `tests`, `docs`).
```python
def _process_dict_item(item):
if "value" in item and item["value"] > 0:
return item["value"] * 2
if "default" in item:
return item["default"]
return None # Or raise an error, or return original item
Example:
def _process_int_item(item):
if item % 2 == 0:
return item / 2
return item
def process_data(data):
if data is None:
return None
if not isinstance(data, list):
return []
my_project/
├── src/
│ ├── my_package/
│ │ ├── __init__.py
│ │ ├── module1.py
│ │ ├── module2.py
│ ├── another_package/
│ │ ├── __init__.py
│ │ ├── ...
├── tests/
│ ├── __init__.py
│ ├── test_module1.py
│ ├── test_module2.py
├── docs/
│ ├── ...
├── .flake8
├── pyproject.toml
└── README.md
processed = []
for item in data:
if isinstance(item, dict):
result = _process_dict_item(item)
if result is not None:
processed.append(result)
elif isinstance(item, int):
processed.append(_process_int_item(item))
else:
processed.append(item)
return processed
```
## 3. Essential Plugins: Extend `flake8`'s Power
### File Naming Conventions
Always install and enable these plugins for a robust linting setup.
- **Lowercase with underscores:** Use lowercase letters and underscores for module and package names (e.g., `my_module.py`, `my_package/`).
- **Descriptive names:** Choose names that clearly indicate the purpose of the file or module.
- **Test files:** Prefix test files with `test_` (e.g., `test_my_module.py`). This is a common convention recognized by testing frameworks like `pytest`. Use underscores instead of hyphens.
* **`flake8-bandit` (S codes)**: Security linter. Catches common security issues.
* **`flake8-bugbear` (B/B9 codes)**: Catches common "bear" bugs and design problems.
* **`flake8-mypy` (M codes)**: Integrates `mypy` type checking into `flake8` output.
### Module Organization Best Practices
**Installation:**
- **Single responsibility principle:** Each module should have a clear, single purpose.
- **Import order:** Follow a consistent import order:
1. Standard library imports
2. Third-party imports
3. Local application/package imports
```bash
pip install flake8 flake8-bandit flake8-bugbear flake8-mypy
```
Separate each group with a blank line.
- **Avoid circular dependencies:** Design your modules to minimize or eliminate circular dependencies.
- **Explicit relative imports:** Within packages, use explicit relative imports (`from . import module`) instead of implicit relative imports (which are deprecated in Python 3). These can be enforced with plugins like `flake8-import-order`.
**Configuration (already in `pyproject.toml` example):**
### Component Architecture Recommendations
```toml
[tool.flake8]
enable-extensions = ["B", "B9", "S", "M"]
```
- **Layered architecture:** Consider a layered architecture (e.g., presentation layer, business logic layer, data access layer) to separate concerns and improve maintainability. This can enhance testability.
- **Microservices (for larger projects):** For very large projects, explore a microservices architecture, where each service is a self-contained unit with its own codebase and deployment pipeline. This requires more overhead but offers better scalability and fault isolation.
- **Dependency Injection:** Use dependency injection to decouple components and facilitate testing.
**Example: `flake8-bandit` (S101 - `assert` statement)**
### Code Splitting Strategies
**❌ BAD**
- **Function size:** Keep functions short and focused. If a function becomes too long, consider splitting it into smaller, more manageable functions.
- **Class size:** Similarly, keep classes focused. A class should represent a single concept or responsibility.
- **Module splitting:** If a module becomes too large, consider splitting it into multiple modules based on logical groupings of functionality.
- **Package splitting:** For larger projects, split packages into subpackages to improve organization.
- **Lazy loading:** Defer the loading of modules or components until they are actually needed to improve startup time.
```python
def check_password(password):
assert password is not None, "Password cannot be None" # S101
# ... security checks ...
```
## 2. Common Patterns and Anti-patterns
**✅ GOOD**
### Design Patterns Specific to Flake8 (Indirectly)
```python
def check_password(password):
if password is None:
raise ValueError("Password cannot be None")
# ... security checks ...
```
Flake8 doesn't directly enforce design patterns, but it encourages practices that support good design:
## 4. Workflow Integration: Lint Early, Lint Often
- **Factory Pattern:** Using factories to create objects can improve code flexibility and testability. Flake8 won't directly enforce it, but a well-designed factory would adhere to Flake8's style guidelines.
- **Strategy Pattern:** Employing strategy patterns for algorithms can make code more modular and easier to maintain. Flake8’s advice on function length is key.
- **SOLID Principles:** Flake8 aids the application of SOLID principles by promoting clean, well-structured code that is easier to extend, maintain, and test. It improves readability.
Integrate `flake8` into your development workflow to catch issues as early as possible.
### Recommended Approaches for Common Tasks
### a. Local Development (IDE Integration)
- **Configuration:** Use a `.flake8` or `setup.cfg` file to configure Flake8 settings (e.g., ignored rules, maximum line length).
- **Ignoring specific errors:** Use `# noqa` comments at the end of lines to ignore specific errors. Be specific with error codes (e.g., `# noqa: E501`) rather than using a blanket `# noqa`.
- **Integrating with pre-commit hooks:** Use pre-commit hooks to automatically run Flake8 before committing code.
- **Using plugins:** Leverage plugins to extend Flake8's capabilities (e.g., `flake8-bugbear`, `flake8-docstrings`).
Configure your IDE (e.g., Cursor, VS Code) to run `flake8` on file save. This provides immediate feedback.
### Anti-patterns and Code Smells
### b. Pre-commit Hooks
- **Long functions/methods:** Functions and methods that are excessively long and complex.
- **Duplicated code:** Code that is repeated in multiple places.
- **Magic numbers:** Hardcoded numerical values that lack explanation.
- **Global variables:** Excessive use of global variables can lead to unpredictable behavior.
- **Deeply nested code:** Excessive indentation can make code hard to read and understand.
- **Ignoring errors indiscriminately:** Using blanket `# noqa` comments without specifying error codes.
Use `pre-commit` to automatically run `flake8` (and formatters like `black` or `ruff format`) before every commit. This ensures no unlinted code ever reaches your repository.
### State Management Best Practices
**`pyproject.toml` (or `.pre-commit-config.yaml`)**
- **Minimize mutable state:** Reduce the use of mutable state to avoid unexpected side effects. Favour immutable data structures.
- **Clear ownership:** Ensure clear ownership of state. Avoid shared mutable state as much as possible.
- **State management libraries:** For complex state management, consider using libraries like Redux or MobX (if applicable in your context, e.g., for GUI applications).
```yaml
# .pre-commit-config.yaml
repos:
- repo: https://github.com/PyCQA/flake8
rev: 7.0.0 # Use the latest stable version
hooks:
- id: flake8
args: ["--config=pyproject.toml"] # Ensure flake8 uses your pyproject.toml
```
### Error Handling Patterns
### c. CI/CD Pipelines
- **Specific exceptions:** Catch specific exceptions rather than using a broad `except Exception:`.
- **Context managers:** Use `try...finally` or `with` statements (context managers) to ensure resources are properly released, even if exceptions occur.
- **Logging:** Log exceptions and errors to help with debugging.
- **Raise exceptions appropriately:** Raise exceptions when appropriate to signal errors to calling code.
- **Avoid swallowing exceptions:** Don't catch exceptions and do nothing. At least log the error.
Include `flake8` as a mandatory step in your CI pipeline. If `flake8` reports any errors, the build *must* fail. This acts as a quality gate.
## 3. Performance Considerations
## 5. Ignoring Errors: Be Specific, Not Lazy
### Optimization Techniques Specific to Flake8 (Indirectly)
Only ignore errors when absolutely necessary, and always be specific.
Flake8 does not directly optimize code, but it encourages practices that lead to better performance:
**❌ BAD: Blanket Ignore**
- **Avoid unnecessary loops:** Optimize loops to minimize the number of iterations and operations performed.
- **Use efficient data structures:** Choose the appropriate data structure for the task at hand (e.g., `set` for membership testing, `dict` for lookups).
- **Minimize object creation:** Creating objects can be expensive. Reuse objects where possible.
- **Use generators and iterators:** Use generators and iterators to process large datasets in a memory-efficient way.
- **List comprehensions & Generator expressions:** Use these to simplify code and increase its execution speed, where appropriate.
```python
import os, sys # noqa
```
### Memory Management Considerations
**✅ GOOD: Specific Ignore**
- **Avoid memory leaks:** Be careful not to create memory leaks by holding onto objects longer than necessary.
- **Use weak references:** Use weak references to avoid creating circular references that prevent garbage collection.
- **Profile your code:** Use profiling tools to identify memory bottlenecks.
```python
import os # noqa: F401,E402
import sys
```
### Bundle Size Optimization (If Applicable - e.g., web applications)
**✅ GOOD: File-level Ignore (in `pyproject.toml`)**
- **Dead code elimination:** Remove unused code to reduce bundle size.
- **Code splitting:** Split your code into smaller chunks that can be loaded on demand.
- **Minification:** Minify your code to reduce file size.
- **Compression:** Use compression techniques (e.g., gzip) to reduce the size of files served to the client.
```toml
[tool.flake8]
per-file-ignores = [
"src/legacy_module.py:E501,F401",
]
```
### Lazy Loading Strategies
## 6. Type Hints: Leverage `flake8-mypy`
- **Import-time optimization:** Defer the loading of modules or components until they are actually needed.
- **Route-based code splitting:** Load only the code required for the current route (if applicable in your context).
While `mypy` is the primary type checker, `flake8-mypy` integrates type checking results into your `flake8` output, providing a unified view of code quality.
## 4. Security Best Practices
**❌ BAD**
### Common Vulnerabilities and Prevention
```python
def add(a, b): # No type hints
return a + b
```
- **SQL Injection:** Prevent SQL injection by using parameterized queries or ORMs.
- **Cross-Site Scripting (XSS):** Prevent XSS by escaping user input before displaying it in HTML.
- **Cross-Site Request Forgery (CSRF):** Implement CSRF protection mechanisms (e.g., CSRF tokens).
- **Authentication and Authorization:** Implement secure authentication and authorization mechanisms to protect sensitive data. Don't store passwords in plain text; use hashing algorithms like bcrypt.
- **Input Validation:** Validate all user input to prevent malicious data from entering your application.
**✅ GOOD**
### Input Validation Best Practices
```python
def add(a: int, b: int) -> int: # M501 (flake8-mypy will check these)
return a + b
```
- **Whitelist validation:** Validate input against a whitelist of allowed values.
- **Regular expressions:** Use regular expressions to validate input formats.
- **Data type validation:** Ensure that input data is of the expected type.
- **Length validation:** Limit the length of input strings to prevent buffer overflows.
## 7. Virtual Environments: Isolate Dependencies
### Authentication and Authorization Patterns
Always install `flake8` and its plugins within a project-specific virtual environment. This prevents dependency conflicts and ensures consistent behavior.
- **OAuth 2.0:** Use OAuth 2.0 for delegated authorization.
- **JSON Web Tokens (JWT):** Use JWTs for authentication and authorization.
- **Role-based access control (RBAC):** Implement RBAC to control access to resources based on user roles.
```bash
python -m venv .venv
source .venv/bin/activate
pip install flake8 flake8-bandit flake8-bugbear flake8-mypy
```
### Data Protection Strategies
## 8. Code Organization & Structure
- **Encryption:** Encrypt sensitive data at rest and in transit.
- **Hashing:** Use hashing to store passwords securely.
- **Data masking:** Mask sensitive data when displaying it to users.
- **Data anonymization:** Anonymize data to protect user privacy.
`flake8` indirectly encourages good structure by enforcing PEP 8 and catching issues.
### Secure API Communication
* **Separate Concerns**: Run `flake8` on `src/` and `tests/` directories. Exclude `docs/` or `migrations/` as needed.
* **Import Order**: While `flake8` itself doesn't enforce strict import order, `isort` (or `ruff format`) does. Pair `flake8` with `isort` for this.
* Standard library
* Third-party
* Local application
* **Avoid Circular Dependencies**: `pyflakes` (part of `flake8`) will warn about these.
- **HTTPS:** Use HTTPS for all API communication.
- **API Keys:** Securely manage API keys.
- **Rate Limiting:** Implement rate limiting to prevent abuse.
- **Input sanitization**: Sanitize data being passed to the API to prevent injection vulnerabilities.
## 9. Common Pitfalls & Gotchas
## 5. Testing Approaches
* **Outdated Configuration**: Always use `pyproject.toml`.
* **Ignoring too much**: A long `ignore` list hides real issues. Fix the code, don't ignore the linter.
* **Not running `flake8` everywhere**: Ensure `flake8` runs on all relevant Python files (`src`, `tests`).
* **Mixing `flake8` with `black` without `E203, W503` ignores**: This will lead to conflicts. Always configure `flake8` for `black` compatibility.
### Unit Testing Strategies
- **Test-driven development (TDD):** Write tests before writing code.
- **Isolate units:** Isolate units of code during testing to ensure that tests are focused and reliable.
- **Use mocks and stubs:** Use mocks and stubs to replace dependencies during testing.
- **Assertion libraries:** Use assertion libraries to write clear and concise tests.
### Integration Testing Approaches
- **Test interactions between components:** Test the interactions between different components of your application.
- **Use real dependencies (when appropriate):** Use real dependencies during integration testing to ensure that components work together correctly.
- **Test data persistence:** Test data persistence to ensure that data is stored and retrieved correctly.
### End-to-End Testing Recommendations
- **Simulate user behavior:** Simulate user behavior to test the entire application flow.
- **Use automated testing tools:** Use automated testing tools (e.g., Selenium, Cypress) to automate end-to-end tests.
- **Test cross-browser compatibility:** Test your application in different browsers to ensure cross-browser compatibility.
### Test Organization Best Practices
- **Separate test code from production code:** Keep test code in a separate directory from production code.
- **Organize tests by module/component:** Organize tests into modules and components that mirror the structure of your production code.
- **Use descriptive test names:** Use descriptive test names that clearly indicate what the test is verifying.
### Mocking and Stubbing Techniques
- **Use mocking libraries:** Use mocking libraries (e.g., `unittest.mock`, `pytest-mock`) to create mocks and stubs.
- **Mock external dependencies:** Mock external dependencies to isolate units of code during testing.
- **Stub return values:** Stub return values to control the behavior of mocked objects.
## 6. Common Pitfalls and Gotchas
### Frequent Mistakes
- **Ignoring Flake8 warnings:** Ignoring Flake8 warnings can lead to code quality issues and potential errors.
- **Over-reliance on `# noqa`:** Overusing `# noqa` comments can mask underlying problems in your code.
- **Inconsistent coding style:** Inconsistent coding style can make code harder to read and understand.
- **Not configuring Flake8:** Not configuring Flake8 can lead to suboptimal results.
### Edge Cases
- **Complex regular expressions:** Complex regular expressions can be difficult to understand and maintain.
- **Dynamic code generation:** Dynamic code generation can make it difficult for Flake8 to analyze code.
- **Large files:** Large files can slow down Flake8 analysis.
### Version-Specific Issues
- **Plugin compatibility:** Ensure that plugins are compatible with the version of Flake8 you are using.
- **Configuration changes:** Be aware of configuration changes between different versions of Flake8.
### Compatibility Concerns
- **Python version compatibility:** Ensure that your code is compatible with the Python versions you are targeting.
- **Dependency conflicts:** Be aware of potential dependency conflicts between Flake8 and other libraries.
### Debugging Strategies
- **Read Flake8 error messages:** Understand the meaning of Flake8 error messages to identify the root cause of problems.
- **Use a debugger:** Use a debugger to step through your code and identify errors.
- **Simplify your code:** Simplify your code to make it easier to debug.
- **Write unit tests:** Write unit tests to isolate and test individual units of code.
## 7. Tooling and Environment
### Recommended Development Tools
- **Text editors/IDEs:** VS Code, PyCharm, Sublime Text
- **Version control:** Git
- **Virtual environments:** `venv`, `virtualenv`, `conda`
- **Build tools:** `setuptools`, `poetry`, `pipenv`
### Build Configuration Best Practices
- **Use a `pyproject.toml` file:** Use a `pyproject.toml` file to specify project metadata, dependencies, and build settings.
- **Specify dependencies explicitly:** Specify dependencies explicitly to ensure that your project is reproducible.
- **Use version pinning:** Use version pinning to lock down the versions of your dependencies.
### Linting and Formatting
- **Flake8:** Use Flake8 for linting and style checking.
- **Black:** Use Black for code formatting.
- **isort:** Use isort for import sorting.
- **Pre-commit hooks:** Use pre-commit hooks to automatically run linters and formatters before committing code.
### Deployment Best Practices
- **Containerization:** Use Docker to containerize your application.
- **Cloud platforms:** Deploy your application to a cloud platform (e.g., AWS, Azure, Google Cloud).
- **Continuous integration/continuous deployment (CI/CD):** Use CI/CD pipelines to automate the deployment process.
### CI/CD Integration Strategies
- **Integrate Flake8 into your CI/CD pipeline:** Run Flake8 as part of your CI/CD pipeline to automatically check code quality.
- **Fail the build on Flake8 errors:** Configure your CI/CD pipeline to fail the build if Flake8 detects errors.
- **Use automated testing:** Run automated tests as part of your CI/CD pipeline to ensure that your application is working correctly.
By following these guidelines, your team will maintain a consistent, high-quality Python codebase, reducing technical debt and improving collaboration.