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
+281 -186
View File
@@ -1,199 +1,294 @@
---
description: This rule provides comprehensive best practices for developing Flask applications, covering code structure, security, performance, and testing.
globs: *.py
description: This guide provides definitive, opinionated best practices for building robust, scalable, and maintainable Flask applications, emphasizing modern patterns and common pitfalls.
globs: **/*.py
---
- ## Code Organization and Structure:
- **Directory Structure Best Practices:**
- Follow a modular and organized project structure. A common structure is:
project_root/
├── app/
│ ├── __init__.py
│ ├── models.py
│ ├── views.py # Or controllers.py
│ ├── forms.py
│ ├── utils.py # Helper functions
│ ├── api/
│ │ ├── __init__.py
│ │ ├── routes.py
│ ├── templates/
│ │ └── ...
│ ├── static/
│ │ └── ...
├── tests/
│ ├── __init__.py
│ ├── conftest.py # Fixtures for tests
│ ├── test_models.py
│ ├── test_views.py
├── migrations/
│ └── ... # Alembic migrations
├── venv/ # Virtual environment
├── .env # Environment variables (use with caution, not for sensitive data in production)
├── config.py # Application configuration
├── requirements.txt or pyproject.toml # Dependencies
├── run.py # Application entry point
- Use Blueprints to organize routes and views into logical modules. Blueprints promote reusability and maintainability.
- **File Naming Conventions:**
- Use descriptive and consistent file names.
- Examples: `models.py`, `views.py`, `forms.py`, `utils.py`, `routes.py`, `test_*.py`.
- Maintain consistency throughout the project.
- **Module Organization:**
- Group related functionality into modules. For instance, database models in `models.py`, user authentication logic in `auth.py`, and utility functions in `utils.py`.
- Use `__init__.py` files to make directories packages, allowing you to import modules within the directory using relative paths.
- **Component Architecture:**
- Design components with clear responsibilities and interfaces.
- Consider using a layered architecture (e.g., presentation, business logic, data access) to separate concerns.
- Use dependency injection to decouple components.
- **Code Splitting Strategies:**
- Decompose large modules into smaller, more manageable files.
- Extract reusable code into separate modules or packages.
- Employ lazy loading for modules that are not immediately needed.
# Flask Best Practices
- ## Common Patterns and Anti-patterns:
- **Design Patterns Specific to Flask:**
- **Application Factory:** Use the application factory pattern to create Flask application instances. This allows for different configurations for different environments (development, testing, production).
python
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
Flask is a powerful microframework. To leverage it effectively for scalable, maintainable applications, adhere to these modern best practices.
# Initialize extensions (e.g., db, mail) here
db.init_app(app)
mail.init_app(app)
## 1. Code Organization: Application Factories & Blueprints
# Register blueprints
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
Always structure your Flask application as a package using an **application factory** and **Blueprints**. This pattern is crucial for testability, multiple environments, and modularity.
return app
- **Blueprints:** Organize application functionality into reusable blueprints.
python
from flask import Blueprint
### 1.1 Application Factory
bp = Blueprint('my_blueprint', __name__, url_prefix='/my_blueprint')
Use an application factory to create your Flask app instance. This isolates configuration and extensions, preventing global state issues.
@bp.route('/route')
def my_route():
return 'Hello from my_blueprint'
- **Recommended Approaches for Common Tasks:**
- **Database Interactions:** Use Flask-SQLAlchemy or another ORM for database interactions. Define models to represent database tables.
- **Form Handling:** Use Flask-WTF for form handling. This provides CSRF protection and simplifies form validation.
- **Authentication:** Use Flask-Login for user authentication. It provides utilities for managing user sessions and protecting routes.
- **API Development:** Use Flask-RESTful or Flask-API for building RESTful APIs. Consider using Marshmallow for serializing and deserializing data.
- **Anti-patterns and Code Smells to Avoid:**
- **Global State:** Avoid using global variables to store application state. Use the `g` object or session variables instead.
- **Tight Coupling:** Design components with loose coupling to improve maintainability and testability.
- **Fat Models/Views:** Keep models and views focused on their primary responsibilities. Move complex business logic to separate modules.
- **Hardcoding Configuration:** Avoid hardcoding configuration values. Use environment variables or a configuration file.
- **State Management Best Practices:**
- Use the Flask `session` object to store user-specific data across requests.
- For application-wide state, consider using a database or a caching mechanism.
- Avoid storing sensitive data in the session without proper encryption.
- **Error Handling Patterns:**
- Use `try...except` blocks to handle exceptions gracefully.
- Implement custom error handlers for specific exceptions. Return appropriate HTTP status codes and error messages.
- Use logging to record errors and warnings.
- Use Flask's `abort()` function to raise HTTP exceptions.
❌ **BAD**: Global `app` instance
```python
# app.py
from flask import Flask
app = Flask(__name__) # Global app instance
# ... configure app, register routes directly
```
- ## Performance Considerations:
- **Optimization Techniques:**
- **Caching:** Implement caching to reduce database queries and improve response times. Use Flask-Caching or Redis.
- **Database Optimization:** Optimize database queries and use indexes to improve performance.
- **Profiling:** Use a profiler to identify performance bottlenecks in your code.
- **Memory Management:**
- Avoid memory leaks by properly closing database connections and releasing resources.
- Use generators to process large datasets efficiently.
- **Rendering Optimization:**
- Minimize the number of database queries in templates.
- Use template caching to reduce rendering time.
- **Bundle Size Optimization:**
- For larger front-end applications, use a bundler like Webpack or Parcel to optimize JavaScript and CSS files. Minify and compress assets.
- **Lazy Loading Strategies:**
- Implement lazy loading for images and other assets to improve initial page load time.
- Use code splitting to load only the necessary JavaScript code for each page.
✅ **GOOD**: Application factory
```python
# myapp/__init__.py
from flask import Flask
from .config import Config
- ## Security Best Practices:
- **Common Vulnerabilities and How to Prevent Them:**
- **Cross-Site Scripting (XSS):** Prevent XSS by escaping user input in templates. Use Jinja2's autoescaping feature.
- **SQL Injection:** Prevent SQL injection by using parameterized queries or an ORM.
- **Cross-Site Request Forgery (CSRF):** Protect against CSRF attacks by using Flask-WTF, which provides CSRF protection.
- **Authentication and Authorization Issues:** Implement secure authentication and authorization mechanisms. Use strong passwords and protect user credentials.
- **Input Validation:**
- Validate all user input to prevent malicious data from entering your application.
- Use Flask-WTF for form validation.
- **Authentication and Authorization Patterns:**
- Use Flask-Login for user authentication.
- Implement role-based access control (RBAC) to restrict access to certain resources.
- Use JWT (JSON Web Tokens) for API authentication.
- **Data Protection Strategies:**
- Encrypt sensitive data at rest and in transit.
- Use HTTPS to secure communication between the client and the server.
- Store passwords securely using a strong hashing algorithm (e.g., bcrypt).
- **Secure API Communication:**
- Use HTTPS for all API communication.
- Implement authentication and authorization for API endpoints.
- Validate API requests and responses.
- Use rate limiting to prevent abuse.
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)
- ## Testing Approaches:
- **Unit Testing Strategies:**
- Write unit tests to verify the functionality of individual components.
- Use pytest or unittest for writing and running tests.
- Mock external dependencies to isolate components during testing.
- **Integration Testing:**
- Write integration tests to verify the interaction between different components.
- Test the integration between the application and the database.
- **End-to-End Testing:**
- Write end-to-end tests to simulate user interactions with the application.
- Use Selenium or Cypress for end-to-end testing.
- **Test Organization:**
- Organize tests into separate directories based on functionality.
- Use descriptive test names.
- Follow the Arrange-Act-Assert pattern in your tests.
- **Mocking and Stubbing:**
- Use mocking and stubbing to isolate components during testing.
- Use the `unittest.mock` module or a third-party mocking library like `mock`.
# Initialize extensions here, e.g., db.init_app(app)
# Register blueprints here, e.g., app.register_blueprint(main_bp)
- ## Common Pitfalls and Gotchas:
- **Frequent Mistakes Developers Make:**
- **Not using a virtual environment:** Always use a virtual environment to isolate project dependencies.
- **Not handling exceptions properly:** Handle exceptions gracefully to prevent application crashes.
- **Exposing sensitive data:** Avoid exposing sensitive data in logs or error messages.
- **Edge Cases to Be Aware Of:**
- **Handling Unicode correctly:** Be aware of Unicode encoding issues when working with text data.
- **Dealing with time zones:** Use a consistent time zone throughout the application.
- **Version-Specific Issues:**
- Be aware of compatibility issues when upgrading Flask or its dependencies.
- Consult the Flask documentation for version-specific information.
- **Compatibility Concerns:**
- Ensure that your application is compatible with different browsers and operating systems.
- Test your application on different devices.
- **Debugging Strategies:**
- Use the Flask debugger to identify and fix errors.
- Use logging to record errors and warnings.
- Use a profiler to identify performance bottlenecks.
return app
- ## Tooling and Environment:
- **Recommended Development Tools:**
- **Virtual Environment Manager:** `virtualenv`, `venv`, or `conda`
- **Package Manager:** `pip` or `pipenv` or `poetry`
- **IDE/Text Editor:** VS Code, PyCharm, Sublime Text
- **Debugger:** `pdb` or `ipdb`
- **Profiler:** `cProfile`
- **Build Configuration:**
- Use a `requirements.txt` or `pyproject.toml` file to specify project dependencies.
- Use a build system like `setuptools` or `poetry` to package your application.
- **Linting and Formatting:**
- Use a linter like `flake8` or `pylint` to enforce code style guidelines.
- Use a formatter like `black` or `autopep8` to automatically format your code.
- **Deployment Best Practices:**
- Use a production-ready WSGI server like Gunicorn or uWSGI.
- Use a reverse proxy like Nginx or Apache to serve static files and handle SSL termination.
- Deploy your application to a cloud platform like AWS, Google Cloud, or Azure.
- **CI/CD Integration:**
- Use a CI/CD pipeline to automate testing, building, and deployment.
- Use tools like Jenkins, Travis CI, or GitHub Actions.
# run.py (for development/entrypoint)
from myapp import create_app
app = create_app()
if __name__ == '__main__':
app.run(debug=True)
```
### 1.2 Blueprints for Modularity
Group related routes, models, and services into Blueprints. This keeps your application organized and promotes reusability.
```python
# myapp/auth/routes.py
from flask import Blueprint, render_template, request, redirect, url_for
from . import auth_bp # Import the blueprint instance
@auth_bp.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
# ... authentication logic
return redirect(url_for('main.index'))
return render_template('auth/login.html')
# myapp/auth/__init__.py
from flask import Blueprint
auth_bp = Blueprint('auth', __name__, template_folder='templates')
from . import routes # Import routes to register them with the blueprint
# myapp/__init__.py (inside create_app)
from myapp.auth import auth_bp
app.register_blueprint(auth_bp, url_prefix='/auth')
```
## 2. Configuration Management
Manage configuration external to your code. Prioritize environment variables for sensitive data and dynamic settings.
✅ **GOOD**: Environment variables and `Config` class
```python
# myapp/config.py
import os
class Config:
SECRET_KEY = os.environ.get('SECRET_KEY') or 'a-very-secret-key-fallback'
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'sqlite:///site.db'
DEBUG = os.environ.get('FLASK_DEBUG') == '1'
# .env (for local development, not committed to VCS)
SECRET_KEY=my-super-secret-key
DATABASE_URL=postgresql://user:password@host:port/dbname
FLASK_DEBUG=1
```
## 3. Performance Considerations
Build fast backends by leveraging asynchronous patterns and efficient deployment.
### 3.1 Asynchronous View Functions
For I/O-bound tasks (database calls, external API requests), use `async def` views with an ASGI server.
❌ **BAD**: Blocking I/O in sync view
```python
# app.py
from flask import Flask, jsonify
import time
app = Flask(__name__)
@app.route('/sync_data')
def sync_data():
time.sleep(2) # Simulates blocking I/O
return jsonify({"message": "Data fetched synchronously"})
```
✅ **GOOD**: Non-blocking I/O with `async def`
```python
# myapp/api/routes.py
from flask import Blueprint, jsonify
import asyncio # For async operations
# For actual async DB/HTTP, use aiohttp, asyncpg, etc.
api_bp = Blueprint('api', __name__)
@api_bp.route('/async_data')
async def async_data():
await asyncio.sleep(2) # Simulates non-blocking I/O
return jsonify({"message": "Data fetched asynchronously"})
# Deploy with an ASGI server like Uvicorn or Hypercorn:
# uvicorn myapp:create_app --factory --port 8000
```
### 3.2 WSGI/ASGI Server
Never use Flask's built-in development server in production. Use a robust WSGI (Gunicorn, uWSGI) or ASGI (Uvicorn, Hypercorn) server.
```bash
# For WSGI (sync Flask apps)
gunicorn -w 4 'myapp:create_app()' --bind 0.0.0.0:8000 --timeout 60
# For ASGI (async Flask apps, requires Flask 2.0+ and an ASGI server)
uvicorn myapp:create_app --factory --host 0.0.0.0 --port 8000 --workers 4
```
## 4. Security Best Practices
Security is paramount. Implement these measures from the start.
### 4.1 Protect Secrets
Store all sensitive information (API keys, database credentials) in environment variables or a secure secret management system. Never hardcode them.
❌ **BAD**: Hardcoded secret
```python
# app.py
DB_PASSWORD = "my_hardcoded_password"
```
✅ **GOOD**: Environment variable
```python
# myapp/config.py
import os
DB_PASSWORD = os.environ.get('DB_PASSWORD')
if not DB_PASSWORD:
raise ValueError("No DB_PASSWORD set for production")
```
### 4.2 Input Validation
Validate all user input rigorously. Use a library like WTForms or Pydantic for forms and API payloads.
```python
# myapp/forms.py
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField
from wtforms.validators import DataRequired, Email, Length
class LoginForm(FlaskForm):
email = StringField('Email', validators=[DataRequired(), Email()])
password = PasswordField('Password', validators=[DataRequired(), Length(min=8)])
# myapp/auth/routes.py
from .forms import LoginForm
@auth_bp.route('/login', methods=['POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
# Process valid data
email = form.email.data
password = form.password.data
# ...
else:
# Handle invalid input, e.g., flash errors
pass
```
### 4.3 CSRF Protection
Enable CSRF protection for all forms that modify state. Use Flask-WTF which integrates WTForms with CSRF.
```python
# myapp/__init__.py (inside create_app)
from flask_wtf.csrf import CSRFProtect
csrf = CSRFProtect()
csrf.init_app(app)
# In your forms, Flask-WTF automatically adds a hidden CSRF token field.
# In templates, ensure you render it:
# <form method="POST">
# {{ form.csrf_token }}
# ...
# </form>
```
## 5. Error Handling
Provide graceful error handling and detailed logging.
### 5.1 Custom Error Pages
Register custom error handlers for common HTTP errors.
```python
# myapp/errors/handlers.py
from flask import Blueprint, render_template
errors_bp = Blueprint('errors', __name__)
@errors_bp.app_errorhandler(404)
def not_found_error(error):
return render_template('errors/404.html'), 404
@errors_bp.app_errorhandler(500)
def internal_error(error):
# Log the error for debugging
return render_template('errors/500.html'), 500
# myapp/__init__.py (inside create_app)
from myapp.errors import errors_bp
app.register_blueprint(errors_bp)
```
## 6. API Design: RESTful Principles & Type Hints
Design APIs following RESTful principles, return JSON, and use type hints.
### 6.1 RESTful Endpoints & JSON Responses
Use appropriate HTTP methods and return JSON for API endpoints.
❌ **BAD**: Inconsistent methods, HTML response for API
```python
@api_bp.route('/users/get_all', methods=['POST']) # Should be GET
def get_users_html():
return "<h1>All Users</h1>" # Should be JSON
```
✅ **GOOD**: RESTful, JSON response
```python
from flask import jsonify, request
@api_bp.route('/users', methods=['GET'])
def get_users():
users = [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]
return jsonify(users)
@api_bp.route('/users', methods=['POST'])
def create_user():
data = request.get_json()
# ... create user with data
return jsonify({"message": "User created", "id": 3}), 201
```
### 6.2 Type Hints
Mandatory for all new Python code. Improves readability, maintainability, and enables static analysis.
❌ **BAD**: Untyped function
```python
def calculate_total(price, quantity):
return price * quantity
```
✅ **GOOD**: Type-hinted function
```python
def calculate_total(price: float, quantity: int) -> float:
return price * quantity
```
## 7. Pallets Python Styleguide
Adhere to the [Pallets Python Styleguide](https://palletsprojects.com/governance/sourcecode/python-styleguide/) for consistent code formatting and structure. This aligns with PEP 8 but includes Flask-specific conventions. Use linters (e.g., Black, Flake8) and static analysis (e.g., MyPy) to enforce this automatically.