Skip to main content

Pure-Python implementation of the Starlark configuration language.

Project description

Starlark in Python

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 for 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.1.1.tar.gz (213.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.1.1-py3-none-any.whl (86.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: starlark-0.1.1.tar.gz
  • Upload date:
  • Size: 213.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.1.1.tar.gz
Algorithm Hash digest
SHA256 3e7318aa2622db123eeacc234ed4f3d5b0e94c36490d16388616c099fd1ac2d3
MD5 1e5b3f384099097adc5ddf517009a1e8
BLAKE2b-256 a8cc5409fee957477d500b12b15a8e4f29dfdf3b3a4a8734d5d025a323b30680

See more details on using hashes here.

File details

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

File metadata

  • Download URL: starlark-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 86.3 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.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5351a065bbb8c957a598cb05a985dd05f84ece4ed8b23871323fee54b12155f2
MD5 d38fb052b31ae63078f7a4e5ddf51b2c
BLAKE2b-256 556821a68568a4367738138f8813fdf8b5ddd53efc1ed09df27be19d31ce3d08

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