Python coder for Hydra: generates idiomatic Python code from Hydra type and term modules
Project description
Hydra-Python
This package contains the Python coder DSL sources: Haskell modules that describe
how to translate Hydra modules into Python source code. The runnable Python head
(hand-written primitives, DSL runtime, pyproject.toml, test runner) lives in
heads/python/.
The generated Python kernel lives in
dist/python/hydra-kernel/.
Hydra is a type-aware data transformation toolkit which aims to be highly flexible and portable. It has its roots in graph databases and type theory, and provides APIs in Haskell, Java, Python, Scala, and Lisp. See the main Hydra README for more details.
Getting started
Hydra-Python requires Python 3.12 or later.
Install uv:
curl -LsSf https://astral.sh/uv/install.sh | sh
Create the Python virtual environment in the Python head directory:
cd heads/python
uv venv --python 3.12
source .venv/bin/activate
Install the dependencies:
uv sync
Documentation
For comprehensive documentation about Hydra's architecture and usage, see:
- Concepts - Core concepts and type system
- Implementation - Implementation guide
- Code Organization -
The
packages/,heads/,dist/layout - Testing - Common test suite documentation
- Developer Recipes - Step-by-step guides
- Syncing Hydra-Python - Regenerating Python from Haskell
Testing
Hydra-Python has two types of tests: the common test suite (shared across all Hydra implementations) and Python-specific tests. See the Testing wiki page for comprehensive documentation.
Common test suite
The common test suite (hydra.test.testSuite) ensures parity across all Hydra implementations.
Passing all common test suite cases is the criterion for a true Hydra implementation.
To run all tests (from heads/python/):
cd heads/python && pytest
To run only the common test suite:
cd heads/python && pytest src/test/python/test_suite_runner.py
The test suite is generated from Hydra DSL sources and includes:
- Primitive function tests (lists, strings, math, etc.)
- Case conversion tests (camelCase, snake_case, etc.)
- Type inference tests
- Type checking tests
- Evaluation tests
- JSON coder tests
- Rewriting and hoisting tests
Python-specific tests
Python-specific tests validate implementation details and Python-specific functionality.
These are located in heads/python/src/test/python/ alongside the common test suite runner.
To run a specific test file:
cd heads/python && pytest src/test/python/test_grammar.py
To match a specific test by name:
cd heads/python && pytest -k test_grammar
To see printed outputs, use the -s flag:
cd heads/python && pytest -s
Code organization
In 0.15, Hydra's Python code is split across three locations (see Code organization wiki page for the full picture):
-
This package (
packages/hydra-python/src/main/haskell/) — the Python coder DSL sources (written in Haskell):Hydra/Sources/Python/containsSyntax,Language,Coder,Serde,Names,Utils,Environment, andTestingmodules. -
Python head (
heads/python/src/main/python/) — hand-written Python runtimehydra/lib/— primitive function implementationshydra/dsl/— DSL utilities (FrozenDict, Maybe, ...)hydra/sources/libraries.py— primitive registrationpyproject.tomllives inheads/python/
-
Generated Python kernel (
dist/python/hydra-kernel/src/main/python/)hydra/core.py— core types (Term, Type, Literal, ...)hydra/graph.py,hydra/packaging.py— graph and packaging structureshydra/coders.py— type adapters and coder frameworkhydra/reduction.py,hydra/rewriting.py,hydra/hoisting.py— term transformationshydra/inference.py,hydra/checking.py— type inference and checking- Generated from the kernel DSL sources using the Python coder
-
Generated Python test suite (
dist/python/hydra-kernel/src/test/python/)- Common tests ensuring parity with Haskell, Java, Scala, and Lisp
Generate Python code
Python code is generated from the Haskell head. See the Hydra-Haskell README for background on code generation.
The recommended way to regenerate all Python code is the sync script (from the repo root):
bin/sync-python.sh
(equivalent to bin/sync.sh --hosts python --targets python)
This will:
- Generate the kernel modules into
dist/python/hydra-kernel/src/main/python - Generate the kernel tests into
dist/python/hydra-kernel/src/test/python - Run the pytest suite
For manual generation, enter GHCi from heads/haskell/:
cd heads/haskell && stack ghci
And run in the REPL:
import Hydra.Generation
import Hydra.Sources.All
-- Generate the kernel
writePython "../../dist/python/hydra-kernel/src/main/python" kernelModules kernelModules
-- Generate the test suite
let allModules = mainModules ++ testModules
writePython "../../dist/python/hydra-kernel/src/test/python" allModules baseTestModules
Validate generated code
find dist/python/hydra-kernel/src -name "*.py" -exec python3 -m py_compile {} +
Formatting, linting, and type checking
Install Ruff, pyright, and pytest, e.g. on macOS:
brew install ruff
brew install pyright
brew install pytest
All of these commands run from the heads/python/ directory
(files/directories can also be specified as arguments).
Formatting
Format the hand-written Python code:
ruff format
Linting
Run the linter:
ruff check
Fix fixable linting errors (e.g. removing unused imports):
ruff check --fix
Static type checking
Run the type checker:
pyright
Numeric types
Hydra's decimal type is implemented as Python decimal.Decimal with the
default 28-digit context precision.
Operations exceeding this precision round per the active context;
users requiring higher precision should adjust decimal.getcontext().prec
before performing arithmetic.
This differs from Haskell Scientific and Java BigDecimal
(which are effectively unbounded for exact operations)
but matches Python's standard decimal behavior.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hydra_python-0.15.0.tar.gz.
File metadata
- Download URL: hydra_python-0.15.0.tar.gz
- Upload date:
- Size: 53.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Bluefin","version":"43","id":"Deinonychus","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
52f04e3419af16dd18a58b9b49976121874818f1c41e050c36896e845b26ab91
|
|
| MD5 |
3a3d26289d21b3536831c18b6a55ca15
|
|
| BLAKE2b-256 |
7bb7ce5ecc02510be2b67086e04fe9e57700bc439794bb971885194da64228f7
|
File details
Details for the file hydra_python-0.15.0-py3-none-any.whl.
File metadata
- Download URL: hydra_python-0.15.0-py3-none-any.whl
- Upload date:
- Size: 55.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.8 {"installer":{"name":"uv","version":"0.11.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Bluefin","version":"43","id":"Deinonychus","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cfc8bcd25ef96721d8df2608678cdcf1fc1ee650e26b7622995c440efacfc59a
|
|
| MD5 |
ff4703d6b2dbd687196c5fbd82841464
|
|
| BLAKE2b-256 |
8515ce899e8efcd4c67e83a92390795bd43ba5cbbe51f0a832af8c09da3d8f29
|