You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Coding standards and conventions for Python projects. Covers type hints, docstrings, import ordering, line length, f-strings, pathlib, and modern Python idioms.
tags
python
coding-rules
style-guide
pep8
type-hints
role
coding-standard
type
rules
language
en
Python Coding Rules
1. Type Hints
Use type hints on all public functions and class attributes.
Use typing generics: list[str], dict[str, int], set[str] (Python 3.9+).
Use | for unions: str | None instead of Optional[str] (Python 3.10+).
Use Protocol for structural subtyping.
Use @dataclass or TypedDict instead of raw dict for structured data.
black src/ tests/
isort src/ tests/
ruff check src/ tests/
5. Docstrings
Use Google style docstrings with Args, Returns, Raises.
defauthenticate_user(username: str, password: str) ->User|None:
"""Authenticate a user by username and password. Args: username: The user's login name. password: The user's plain-text password. Returns: The authenticated User object, or None if authentication fails. Raises: DatabaseError: If the database connection is unavailable. """
6. Naming Conventions
Element
Convention
Example
Modules
snake_case
user_service.py
Classes
PascalCase
UserRepository
Functions
snake_case
get_user_by_id
Constants
UPPER_SNAKE_CASE
MAX_RETRY_COUNT
Private
_leading_underscore
_internal_helper
Variables
snake_case
user_list
7. Modern Python Idioms
Use match/case for complex branching (Python 3.10+).
Use := walrus operator for assignment expressions where it improves readability.
Use enumerate() instead of manual counters.
Use zip() for parallel iteration.
Use any()/all() for boolean aggregation.
Use next() with default instead of manual index access.