This document defines the engineering standards, coding conventions, and best practices for the project.
We follow PEP 8 standards with specific rules for our hexagonal architecture.
- Snake Case (
snake_case): Used for all file and directory names. - Descriptive Suffixes: Files must indicate their architectural role.
user_usecase.py(Not justuser.py)user_repository.pyuser_dto.pyuser_entity.py
- Pascal Case (
PascalCase): Used for class names. - Role Suffixes: Class names must match the filename (without underscores).
UserUseCaseUserRepositoryUserEntity
- Snake Case (
snake_case): Used for variables, functions, and methods. - Constants:
UPPER_CASE_WITH_UNDERSCORES. - Private Members: Use
_prefix for internal methods/attributes (e.g.,_validate_internal_state).
- Database Session: Use
session(avoiddb,conn). - Entities (ORM): Use
_entitysuffix or clear context (e.g.,user_entity). - Domain Models: Clean names (e.g.,
user). - DTOs: Explicit suffix if needed, or clear context (e.g.,
user_create_dto).
- Strict Typing: All function signatures must have type hints.
- Return Types: Always specify return types, including
-> None.
def get_user(self, user_id: int) -> Optional[User]:
...- Google Style: Use Google-style docstrings for all public classes and methods.
- Content: Include
Args,Returns, andRaisessections.
def calculate_total(self, price: float, tax: float) -> float:
"""
Calculates the total price including tax.
Args:
price: The base price.
tax: The tax rate.
Returns:
float: The final total price.
"""
...- INFO: High-level flow events (e.g., "User creation started").
- WARNING: Recoverable issues or unexpected inputs (e.g., "Login failed").
- ERROR: Unhandled exceptions or external service failures.
- Structured Logging: Logs should be structured (JSON preferred in production).
- Context: Always include relevant context (IDs, operation names).
logger.info("User created", extra={"user_id": 123})- Domain Exceptions: Base class for business logic errors.
- Infrastructure Exceptions: Base class for technical errors (DB, Network).
- No Empty Excepts: Never use bare
except:. Always catch specific exceptions. - Propagation: Do not catch exceptions just to log them and re-raise immediately without adding context.