Code style
Code Style¶
Python Version¶
- Target: Python 3.13+
- Use modern type syntax:
list[str]instead ofList[str],str | Noneinstead ofOptional[str].
Imports¶
Import order (enforced by ruff/isort):
- Standard library
- Third-party packages
- First-party (
pdf_remote_converter)
# Standard library
import os
from pathlib import Path
# Third-party
import httpx
from pydantic import BaseSettings
# First-party
from pdf_remote_converter.config import Config
Type Annotations¶
- Required for all function parameters and return types.
- Use
from __future__ import annotationsfor modern syntax.
from __future__ import annotations
def convert(input_path: Path, output_path: Path) -> ConversionResult:
...
Naming Conventions¶
| Type | Convention | Example |
|---|---|---|
| Class | PascalCase | CloudConvertProvider |
| Function | snake_case | convert_with_failover |
| Private Function | _snake_case |
_compute_file_hash |
| Constant | UPPER_SNAKE_CASE | PROVIDER_PREFERENCE |
| Variable | snake_case | api_key |
Error Handling¶
- Use custom exceptions from
pdf_remote_converter.exceptions. - Preserve exception chains with
raise ... from e.
try:
response = httpx.get("https://api.example.com")
response.raise_for_status()
except httpx.HTTPStatusError as e:
raise ProviderUnavailableError("API request failed") from e
Formatting¶
- Line Length: 160 characters.
- Docstrings: Google style.
- Code Blocks: Same line length as code.
Example Docstring¶
class CloudConvertProvider:
"""CloudConvert provider for document conversion.
Supports DOCX, XLSX, and PPTX formats. Uses the CloudConvert API for conversion.
Attributes:
name: Provider name.
supported_formats: Set of supported file extensions.
monthly_quota: Total quota for the provider.
quota_remaining: Remaining quota for the provider.
"""
Tools¶
| Task | Command |
|---|---|
| Lint Check | uv run ruff check src tests |
| Lint Fix | mise lint |
| Format Check | uv run ruff format --check src tests |
| Format Fix | mise format |
| Type Check | mise typecheck |
| Markdown Format | mise format-md |
| Spell Check | mise spell |
Markdown is linted and formatted with rumdl
(config: .config/rumdl.toml); Python import and member ordering is handled by
pyreorder (config: .config/pyreorder.toml).
Related¶
- Testing - Writing tests that follow code style.
- Contributing - Submitting code that meets style guidelines.