Python Style Guide
The project follows PEP 8 as the base style guide and uses Black as the automatic code formatter.Black Configuration
- Line length: 100 characters
- Target version: Python 3.10+
- Excludes:
.git,.venv,venv,__pycache__,migrations
Naming Conventions
Files and Directories
| Type | Convention | Example |
|---|---|---|
| Python modules | snake_case | user_service.py |
| Directories | snake_case | wood_types/ |
| Classes | PascalCase | ColorService |
Variables and Functions
| Type | Convention | Example |
|---|---|---|
| Variables | snake_case | user_name |
| Functions | snake_case | get_all_colors() |
| Constants | UPPER_SNAKE_CASE | MAX_PAGE_SIZE |
| Classes | PascalCase | ColorModel |
| Private methods | _snake_case | _validate_input() |
Database Models
Model classes use PascalCase, table names use plural snake_case:app/models/color.py:6
Import Organization
Imports should be organized in three groups, separated by blank lines:- Python standard library
- Third-party libraries
- Local application imports
app/catalogs/colors/routes.py:1-10
Documentation Standards
Docstrings
Use Google Style docstrings for all modules, classes, and functions:Type Hints
Always use type hints for function parameters and return values:app/catalogs/colors/services.py:17-24 for examples.
Flask Patterns
Blueprint Structure
Each module defines a Blueprint in its__init__.py:
app/catalogs/colors/__init__.py:1-12
Route Handlers
Route handlers follow these conventions:app/catalogs/colors/routes.py:25-48
POST/Redirect/GET Pattern
Always redirect after a successful POST to prevent duplicate submissions:SQLAlchemy Conventions
Service Layer Pattern
Business logic lives in Service classes with static methods:app/catalogs/colors/services.py:27-61
Case-Insensitive Queries
Usefunc.lower() for case-insensitive string comparisons:
Error Handling
Always wrap commits in try-except blocks:WTForms Conventions
Form Classes
Form classes extendFlaskForm and include field-level validation:
app/catalogs/colors/forms.py:1-20
CSRF Protection
All forms must include CSRF token:app/templates/colors/create.html:9
Exception Handling
Use custom exceptions for business logic errors:app/exceptions.py:12-62
Template Conventions
Template Inheritance
All templates extendbase.html:
Flash Messages
Display flash messages with categories:URL Patterns
| Action | Method | URL Pattern | Example |
|---|---|---|---|
| List | GET | /{resource}/ | /colors/ |
| Create form | GET | /{resource}/create | /colors/create |
| Create | POST | /{resource}/create | /colors/create |
| Edit form | GET | /{resource}/{id}/edit | /colors/1/edit |
| Update | POST | /{resource}/{id}/edit | /colors/1/edit |
| Delete | POST | /{resource}/{id}/delete | /colors/1/delete |
Code Review Checklist
- Code follows PEP 8 (formatted with Black)
- All functions have docstrings
- Type hints are used
- Names follow naming conventions
- Exceptions are handled correctly
- Forms use
FlaskFormwith validators - Templates include CSRF token via
form.hidden_tag() - Templates display form errors
- PRG pattern applied after POST
- No unnecessary commented code
- Imports are ordered correctly
- Database commits wrapped in try-except
- String comparisons are case-insensitive where appropriate