Skip to main content

Pure-Python implementation of the Starlark configuration language.

Project description

Starlark in Python

PyPI package version badge. Python 3.11, 3.12, 3.13, 3.14 supported. PyPI download statistics badge.

This project provides a pure-Python implementation of the Starlark configuration language. It was ported by AI from the Java reference implementation that ships with Bazel.

Status

Conformance test files are passing with the exception of 4 xfails. Those are all documented divergences from the Java reference (UTF-16 string indexing, 32-bit range() bounds, Bazel-specific mutablestruct test helper).

Goals

  • Pure Python
    • Therefore usable in a cross-platform zipapp
  • No dependencies
  • Simple implementation (a tree-walking interpreter)
  • Safe to run untrusted code
  • Passes the conformance suite from Bazel (copied verbatim in conformance/)

Non-goals

  • Performance
  • Supporting old Python versions (3.11+ is currently required)

Quick start

import starlark

# Evaluate an expression.
starlark.eval("1 + 2 * 3")  # 7

# Run a Starlark file.
m = starlark.exec_file('''
def fact(n):
    result = 1
    for i in range(1, n + 1):
        result *= i
    return result

z = fact(5)
''')

m.globals["z"]  # 120

API

See docs/.

CLI

The package installs a starlark-python console script. (We picked a suffixed name so it doesn't shadow starlark from go.starlark.net, which we use for cross-validation.) It can also be run as a zipapp:

poe zipapp                                 # builds ./starlark-python.pyz (~560K)
./starlark-python.pyz -c "1 + 2 * 3"       # 7
./starlark-python.pyz path/to/script.star

load() and the host API

The runtime does not load files itself; the host supplies a Loader callable. eval/loader.py ships a simple file-based loader you can plug in:

from starlark.eval.loader import FileLoader
import starlark

loader = FileLoader(exec_file=starlark.exec_file, search_paths=[".", "lib"])
starlark.exec_file(open("main.star").read(), loader=loader)

load("foo.star", "bar") then resolves foo.star against loader.

Security

Starlark in Python is new and has not been extensively reviewed and tested.

Starlark is a sandboxed language. A .star program cannot read or write files, open sockets, spawn processes, or reach any Python object the host did not explicitly hand it. Optional opt-in resource limits (max_steps, max_allocs) bound CPU and memory for hosts that accept untrusted input.

What we defend against, what we don't defend against, and the public limits API are documented in security/threat-model.md. In shortp: we mitigate DoS-style malicious values; defending against deliberately misconfigured (but otherwise valid) values is a host responsibility, same as with JSON or TOML.

Documented divergences from the Java reference

These are intentional:

  • Integers are Python int. Arbitrary precision; no overflow. The Java reference uses a StarlarkInt union of int32/int64/BigInteger.
  • Strings are indexed by Unicode code point. The Java reference indexes by UTF-16 code unit, which produces surprising results for non-BMP characters. The spec leaves this implementation-defined.
  • No 32-bit range checks for range(), * repeat, etc. The Java reference rejects allocations whose length doesn't fit in a signed 32-bit int. We instead cap container allocations at 16M elements with a less specific error message.

The conformance suite includes a handful of tests that depend on the Java reference's exact error wording for these checks; they are listed inXFAIL_FILES in tests/test_conformance.py.

Layout

conformance/      `.star` conformance tests, copied from Bazel.
src/starlark/     The actual port.
    eval/         Value model, evaluator, builtins, methods, loader.
    syntax/       Lexer, parser, AST, resolver.
    tests/        Pytest suite (unit + conformance).
    HISTORY.md    Original 14-phase plan + append-only journal.
    cmd.py        CLI entry point.

Development

Install Poe the Poet to run the tasks (uv tool install poethepoet, pipx install poethepoet).

uv sync           # Install deps
poe test          # ~400 tests, ~2s
poe lint          # Ruff
poe typecheck     # Pyright
poe zipapp        # Build ./starlark-python.pyz

tests/test_cross_validation.py runs a curated set of programs under both this interpreter and the starlark-go CLI and asserts they produce identical output. To enable, install the Go implementation and make sure it's on PATH:

go install go.starlark.net/cmd/starlark@latest

License

Apache 2.0. See LICENSE.

This is a derivative work: the lexer, parser, resolver, evaluator, and value model are ported from the Java reference implementation maintained by The Bazel Authors as part of bazelbuild/bazel. The conformance test files under conformance/ are copied verbatim from that project. docs/spec.md is fetched verbatim from bazelbuild/starlark for reference.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

starlark-0.2.0.tar.gz (231.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

starlark-0.2.0-py3-none-any.whl (92.5 kB view details)

Uploaded Python 3

File details

Details for the file starlark-0.2.0.tar.gz.

File metadata

  • Download URL: starlark-0.2.0.tar.gz
  • Upload date:
  • Size: 231.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.9 {"installer":{"name":"uv","version":"0.11.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for starlark-0.2.0.tar.gz
Algorithm Hash digest
SHA256 998e67a80a717344d601acb4e9de392f111b82e78c58e6a7bfeb0534c50f3f54
MD5 46c59d4ecbb1456a70c97fc32f54040b
BLAKE2b-256 395d153396eecab3d698fbaece5be2e049607b7eb8eb5bdf8955d58751f3e18f

See more details on using hashes here.

File details

Details for the file starlark-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: starlark-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 92.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.9 {"installer":{"name":"uv","version":"0.11.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for starlark-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9008f2d7e2d340ab8572a6323e59f8d829ba167798f97cea5059a1047ef46871
MD5 742c8afefa551268d693f6e1dbc6b159
BLAKE2b-256 c68d2bea44bb7f597cee433235cfd7af02c1680e9c2a5e2621c05acea444d526

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page