Development Setup
This guide explains how to set up a development environment for pyetm.
Prerequisites
- Python 3.12 or later
- Poetry for dependency management
- Git
Setup
1. Clone the Repository
2. Install Poetry
If you don't have Poetry installed:
3. Install Dependencies
This installs all dependencies including development tools.
4. Activate Virtual Environment
5. Configure Environment
Create a .env file:
Edit .env with your settings:
Development Workflow
Running Tests
# Run all tests
poetry run pytest
# Run with coverage
poetry run pytest --cov=pyetm --cov-report=term-missing
# Run specific test file
poetry run pytest tests/models/test_scenario.py
# Run specific test
poetry run pytest tests/models/test_scenario.py::test_create_scenario
Code Quality
Ruff (Linting & Formatting)
# Check for issues
poetry run ruff check .
# Fix issues automatically
poetry run ruff check --fix .
# Format code
poetry run ruff format .
mypy (Type Checking)
# Type check entire codebase
poetry run mypy src/pyetm
# Type check specific file
poetry run mypy src/pyetm/models/scenario.py
Pylint
Pre-commit Hooks
pyetm uses pre-commit hooks to ensure code quality:
Hooks run automatically on commit and check:
- Code formatting (Ruff)
- Type hints (mypy)
- Trailing whitespace
- TOML validity
- Merge conflicts
Documentation
Build Docs Locally
Build for Production
Project Structure
pyetm/
├── src/pyetm/ # Source code
│ ├── clients/ # HTTP client infrastructure
│ ├── config/ # Configuration management
│ ├── models/ # Domain models (Scenario, Session, etc.)
│ ├── services/ # Business logic and API services
│ └── utils/ # Utility functions
├── tests/ # Test suite
├── docs/ # Documentation source
├── examples/ # Example inputs
├── pyproject.toml # Project configuration
└── mkdocs.yml # Documentation configuration
Code Standards
Style Guidelines
- Line length: 100 characters maximum
- Docstrings: Google style for all public APIs
- Type hints: Required for all functions and methods
- Imports: Organized (stdlib, third-party, local)
Docstring Example
def create_scenario(area_code: str, end_year: int) -> Scenario:
"""Create a new ETM scenario.
Args:
area_code: Region code (e.g., "nl", "de").
end_year: Target year for the scenario.
Returns:
The created Scenario instance.
Raises:
ScenarioError: If scenario creation fails.
Example:
>>> scenario = create_scenario("nl", 2050)
>>> print(scenario.id)
123456
"""
...
Testing Standards
- Coverage: Maintain high test coverage (>80%)
- Unit tests: Test individual functions and methods
- Integration tests: Test API interactions with mocks
- Fixtures: Use pytest fixtures for common setup
Common Tasks
Adding a New Feature
- Create feature branch:
git checkout -b feature/my-feature - Write tests first (TDD)
- Implement feature
- Update documentation
- Run tests and linting
- Submit pull request
Fixing a Bug
- Create bug fix branch:
git checkout -b fix/bug-description - Write failing test that reproduces bug
- Fix the bug
- Verify test passes
- Submit pull request
Updating Dependencies
# Update all dependencies
poetry update
# Update specific dependency
poetry update pydantic
# Add new dependency
poetry add package-name
# Add dev dependency
poetry add --group dev package-name
Troubleshooting
Poetry Installation Issues
If you have issues installing dependencies:
Test Failures
If tests fail locally:
# Ensure environment is clean
poetry env remove python3.12
poetry install
# Run tests with verbose output
poetry run pytest -vv
Next Steps
- Testing Guide - Learn about testing practices
- Contributing Guide - Contribution workflow
- API Reference - Code documentation