Skip to main content
DeerFlow follows consistent code style guidelines to ensure maintainability and readability across the codebase.

Python Code Style

Style Tools

We use Ruff for both linting and formatting:
  • Linter: Catches code quality issues, unused imports, and potential bugs
  • Formatter: Ensures consistent code formatting across the project

Ruff Configuration

Configuration is defined in backend/ruff.toml:

Configuration Details

Line Length: 240 characters
  • Longer than PEP 8’s 79 characters
  • Allows for more readable complex expressions
  • Reduces need for line continuations
  • Modern wide-screen displays accommodate longer lines
Target Version: Python 3.12+
  • Uses latest Python features
  • Ruff suggests modern syntax improvements
Lint Rules:
  • E - pycodestyle error rules
  • F - Pyflakes rules (undefined names, unused imports)
  • I - isort import sorting rules
  • UP - pyupgrade rules (syntax modernization)
Quote Style: Double quotes (")
  • Consistent with JSON and YAML
  • More readable for strings containing apostrophes
Indent Style: Spaces (4 spaces per level)
  • Standard Python indentation
  • More consistent across editors

Running Ruff

From the backend/ directory:

Pre-commit Integration

Run Ruff automatically before committing:
Install pre-commit hooks:

Python Style Guidelines

Type Hints

Always use type hints for function parameters and return values:
For complex types, use typing module:

Imports

Order imports using isort rules (automatically handled by Ruff):
  1. Standard library imports
  2. Third-party imports
  3. Local application imports
Use absolute imports for project modules:

Naming Conventions

Modules and packages: lowercase_with_underscores
Classes: PascalCase
Functions and variables: lowercase_with_underscores
Constants: UPPERCASE_WITH_UNDERSCORES
Private attributes: Prefix with single underscore

Docstrings

Use docstrings for modules, classes, and functions:
For simple functions, a one-line docstring is sufficient:

Comments

Use comments sparingly - prefer self-documenting code:
Use comments for complex logic:

Function Length

Keep functions focused - aim for functions under 50 lines:

Error Handling

Use specific exceptions:
Provide context in error messages:

String Formatting

Use f-strings for string formatting:

Dictionary and List Handling

Use dict.get() with defaults:
Use comprehensions for simple transformations:

Context Managers

Use context managers for resource management:

Data Classes

Use Pydantic models for configuration and data validation:

Frontend Code Style

TypeScript Guidelines

The frontend uses TypeScript with ESLint and Prettier:

TypeScript Style Highlights

  • Interfaces over types for object shapes
  • Explicit return types for functions
  • Functional components with hooks
  • Named exports for components
  • Async/await over promises

Documentation Standards

Code Documentation Policy

CRITICAL: Always update documentation after code changes When making code changes:
  1. Update README.md for user-facing changes
  2. Update CLAUDE.md for development/architecture changes
  3. Update inline code documentation
  4. Update API documentation if endpoints change
  5. Add migration notes for breaking changes

Documentation Format

  • Use clear, concise language
  • Include code examples
  • Explain why, not just what
  • Keep documentation in sync with code
  • Use proper Markdown formatting

Linting CI Integration

Linting runs automatically in CI:
Fix linting issues before pushing:

Configuration Files Reference

Backend Configuration

backend/ruff.toml - Ruff linter and formatter configuration
backend/pyproject.toml - Python project metadata and dependencies

Style Checklist

Before committing code:
  • Run make format to auto-fix style issues
  • Run make lint to check for remaining issues
  • Run make test to ensure tests pass
  • All functions have type hints
  • Complex logic has explanatory comments
  • Public functions have docstrings
  • Imports are organized (automatic with Ruff)
  • No unused imports or variables
  • Documentation updated if needed

Resources