Skip to content

Python Best Practices Setup - Complete

Successfully set up apps/server with Python best practices, modern tooling, comprehensive testing, and production-ready configurations.

Summary of Changes

✅ Core Configuration

[pyproject.toml](file:///Users/sunny/workspace/projects/kemma/apps/server/pyproject.toml)

  • Added development dependencies: ruff, pyright, pytest, pytest-asyncio, pytest-cov, httpx, pre-commit
  • Configured Ruff for linting and formatting (replaces Black + isort + Flake8)
    • Line length: 100 characters
    • Enabled comprehensive rule sets: pycodestyle, pyflakes, isort, pep8-naming, pyupgrade, type annotations, security, bugbear, and more
    • Auto-fix enabled for safe corrections
  • Configured pyright for type checking
    • Python 3.13 target
    • Basic mode for practical type checking
    • Excludes cache and virtual environment directories
  • Configured pytest with async support
    • Auto async mode
    • Coverage reporting (term, HTML, XML)
    • Test discovery patterns
  • Fixed hatchling build configuration to properly package kemma module

✅ Code Quality Tools

[.pre-commit-config.yaml](file:///Users/sunny/workspace/projects/kemma/apps/server/.pre-commit-config.yaml)

Pre-commit hooks for automated code quality checks:

  • Trailing whitespace removal
  • End-of-file fixer
  • YAML/JSON validation
  • Large file detection
  • Merge conflict detection
  • Private key detection
  • Ruff linting with auto-fix
  • Ruff formatting

Installed: Pre-commit hooks are now active in the git repository

[Makefile](file:///Users/sunny/workspace/projects/kemma/apps/server/Makefile)

Developer-friendly commands:

  • make help - Show all available commands
  • make install - Install dependencies
  • make format - Format code with Ruff
  • make lint - Lint code with Ruff
  • make type-check - Run pyright type checker
  • make test - Run tests
  • make test-cov - Run tests with coverage
  • make dev - Start development server
  • make clean - Clean cache files
  • make all-checks - Run all quality checks

✅ Production Configuration

[kemma/core/logging.py](file:///Users/sunny/workspace/projects/kemma/apps/server/kemma/core/logging.py) [NEW]

Production-ready logging:

  • Environment-based configuration
  • Configurable log levels
  • Structured logging support
  • Library log level management (suppresses noisy libraries)

[kemma/config.py](file:///Users/sunny/workspace/projects/kemma/apps/server/kemma/config.py)

Enhanced with:

  • debug - Debug mode flag
  • environment - Environment detection (development/staging/production)
  • health_check_enabled - Health check toggle

[kemma/main.py](file:///Users/sunny/workspace/projects/kemma/apps/server/kemma/main.py)

Enhanced with:

  • Improved logging setup using core.logging
  • New /health endpoint with comprehensive health checks:
    • System status
    • Version information
    • Environment details
    • Database connectivity check
    • Available LLM providers
  • Shutdown event handler

[.env.production.example](file:///Users/sunny/workspace/projects/kemma/apps/server/.env.production.example) [NEW]

Production environment template with:

  • PostgreSQL database configuration
  • Production CORS settings
  • Disabled debug mode
  • INFO log level
  • Health check enabled

[.gitignore](file:///Users/sunny/workspace/projects/kemma/apps/server/.gitignore) [NEW]

Comprehensive ignore patterns for Python projects


✅ Testing Infrastructure

[tests/conftest.py](file:///Users/sunny/workspace/projects/kemma/apps/server/tests/conftest.py)

Pytest fixtures:

  • test_engine - In-memory SQLite database engine
  • test_session - Async database session
  • seed_test_data - Populates test database with sample nodes
  • test_client - Async HTTP client with dependency injection

[tests/test_health.py](file:///Users/sunny/workspace/projects/kemma/apps/server/tests/test_health.py) [NEW]

Tests for health check endpoints:

  • Root endpoint (/) returns API information
  • Health check endpoint (/health) returns system status and database connectivity

[tests/test_spark_api.py](file:///Users/sunny/workspace/projects/kemma/apps/server/tests/test_spark_api.py)

Refactored to use async/await pattern:

  • All tests updated to use test_client fixture
  • Tests for mock provider
  • Contextual suggestions validation
  • Request parameter validation
  • Error handling

✅ Code Quality Improvements

All code now passes strict linting and type checking:

Fixed Type Annotations

  • Added return types to all route handlers
  • Added AsyncGenerator types for database sessions and context managers
  • Added None return types to __init__ methods
  • Replaced Any types with proper SQLAlchemy types in test fixtures

Fixed Exception Handling

  • Updated exception handling to use from None pattern in [spark.py](file:///Users/sunny/workspace/projects/kemma/apps/server/kemma/api/routes/spark.py)
  • Properly distinguishes between different exception types (404, 400, 500)

Fixed Line Lengths

  • Broke long lines in [prompts.py](file:///Users/sunny/workspace/projects/kemma/apps/server/kemma/services/llm/prompts.py) to meet 100 character limit

Formatting

  • 18 files reformatted using Ruff
  • Consistent code style across entire codebase

✅ Documentation

[README.md](file:///Users/sunny/workspace/projects/kemma/apps/server/README.md)

Added comprehensive sections:

Development Setup (new):

  • Installing development dependencies
  • Code quality tools overview
  • Running formatters, linters, type checkers
  • Pre-commit hooks setup
  • Testing with coverage
  • Makefile commands reference

Production Deployment (new):

  • Environment configuration
  • Running with uvicorn (single/multi-worker)
  • Health check endpoint usage
  • Logging configuration
  • Security best practices
  • Monitoring recommendations

Verification Results

✅ All Tests Passing (7/7)

tests/test_health.py::test_root_endpoint PASSED
tests/test_health.py::test_health_check_endpoint PASSED
tests/test_spark_api.py::test_spark_with_mock_provider PASSED
tests/test_spark_api.py::test_spark_returns_contextual_suggestions PASSED
tests/test_spark_api.py::test_spark_respects_num_suggestions PASSED
tests/test_spark_api.py::test_spark_invalid_provider PASSED
tests/test_spark_api.py::test_get_all_nodes PASSED

✅ Code Coverage: 71%

Current coverage breakdown:

  • 100%: API schemas, config, logging, database, models, base services, prompts
  • 83%: Nodes API routes
  • 78%: Database module
  • 74%: LLM factory
  • 61%: Spark API routes
  • 56%: Individual LLM providers (OpenAI, Gemini, Grok)
  • 43%: Mock provider

Lower coverage on LLM providers is acceptable as they require API credentials for full testing.

✅ Linting: All Clear

bash
uv run ruff check . --fix
# Result: Found 4 errors (4 fixed, 0 remaining)

All linting issues automatically fixed.

✅ Formatting: Complete

bash
uv run ruff format .
# Result: 18 files reformatted, 12 files left unchanged

✅ Pre-commit Hooks: Installed

Git hooks are now active and will run on every commit.


Quick Start Guide

For Developers

bash
# Install dependencies
cd apps/server
uv sync

# Install pre-commit hooks
uv run pre-commit install

# Format code
make format

# Lint code
make lint

# Type check
make type-check

# Run tests
make test-cov

# Start dev server
make dev

For Production

bash
# Set up production environment
cp .env.production.example .env
# Edit .env with production values

# Run with multiple workers
uv run uvicorn kemma.main:app \
  --host 0.0.0.0 \
  --port 8000 \
  --workers 4

# Health check
curl http://localhost:8000/health

What's Production-Ready

Logging: Environment-based configuration, structured logging support ✅ Health Checks: Comprehensive /health endpoint for monitoring ✅ Type Safety: Pyright type checking throughout codebase ✅ Code Quality: Automated linting and formatting via pre-commit hooks ✅ Testing: 71% coverage with async test infrastructure ✅ Documentation: Complete setup, development, and deployment guides ✅ Security: .gitignore prevents committing secrets, CORS configuration ✅ Scalability: Multi-worker uvicorn configuration documented


Tools Overview

ToolPurposeCommand
RuffFast linter + formatteruv run ruff check . --fix
pyrightStatic type checkeruv run pyright kemma/
pytestTest runneruv run pytest --cov=kemma
pre-commitGit hooksuv run pre-commit run --all-files
uvPackage manageruv sync

All configured in [pyproject.toml](file:///Users/sunny/workspace/projects/kemma/apps/server/pyproject.toml).