Skip to main content

Interact with the Lean theorem prover language server

Project description

leanclient

Interact with the Lean 4 language server in Python.

PyPI version last update license

leanclient is a thin Python wrapper around the native Lean 4 language server. It enables interaction with a Lean 4 language server instance running in a subprocess.

Check out the documentation for more information.

Key Features

  • Interact: Query and change lean files via the LSP.
  • Thin wrapper: Directly expose the Lean Language Server.
  • Fast: Typically more than 95% of time is spent waiting.
  • Parallel: Easy batch processing of files using all your cores.

Quickstart

The best way to get started is to check out this minimal example in Google Colab:

Open in Colab

Or try it locally:

  1. Setup a new lean project or use an existing one. See the colab notebook for a basic Ubuntu setup.

  2. Install the package:

pip install leanclient
# Or with uv:
uv pip install leanclient
  1. Example:
import leanclient as lc

# Start a new client, point it to your lean project root (where lakefile.toml is located).
PROJECT_PATH = "path/to/your/lean/project/root/"
client = lc.LeanLSPClient(PROJECT_PATH)

# Query a lean file in your project
file_path = "MyProject/Basic.lean"
diagnostics = client.get_diagnostics(file_path)
print(f"Found {len(diagnostics)} diagnostics")

# Use a SingleFileClient for simplified interaction with a single file.
sfc = client.create_file_client(file_path)

# Get symbols in the file (theorems, definitions, etc.)
symbols = sfc.get_document_symbols()
print(f"File contains {len(symbols)} symbols")

# Get hover information at a specific position
hover = sfc.get_hover(line=10, character=5)
if hover:
    print(f"Hover info: {hover['contents']}")

# Make a change to the document.
change = lc.DocumentContentChange(
    text="-- Adding a comment at the head of the file\n", 
    start=[0, 0], 
    end=[0, 0]
)
sfc.update_file(changes=[change])

# Verify the change (not written to disk, only in LSP memory)
content = sfc.get_file_content()
assert content.startswith("-- Adding a comment")

# Explore module hierarchy (requires .ilean files from built dependencies)
module = client.prepare_module_hierarchy(".lake/packages/mathlib/Mathlib/Init.lean")
imports = client.get_module_imports(module)
print(f"Module {module['name']} has {len(imports)} direct imports")

imported_by = client.get_module_imported_by(module)
print(f"Module is imported by {len(imported_by)} other modules")

Implemented LSP Interactions

See the documentation for more information on:

  • Opening and closing files.
  • Updating (adding/removing) code from an open file.
  • Diagnostic information: Errors, warnings and information.
  • Goals and term goal.
  • Hover information.
  • Document symbols (theorems, definitions, etc).
  • Semantic tokens, folding ranges, and document highlights.
  • Locations of definitions and type definitions.
  • Locations of declarations and references.
  • Completions, completion item resolve.
  • Getting code actions, resolving them, then applying the edits.
  • Get InfoTrees of theorems (includes rudimentary parsing).
  • Module hierarchy: Get module info, imports, and reverse dependencies.

Missing LSP Interactions

  • "Call hierarchy" is currently not reliable.

Might be implemented in the future:

  • workspace/symbol, workspace/didChangeWatchedFiles, workspace/applyEdit, ...
  • textDocument/prepareRename, textDocument/rename

Internal Lean methods:

  • $/lean/rpc/connect, $/lean/rpc/call, $/lean/rpc/release, $/lean/rpc/keepAlive
  • Interactive diagnostics
  • $/lean/staleDependency

Potential Features

  • Better Windows support
  • Choose between lean --server and lake serve
  • Automatic testing (lean env setup) for non Debian-based systems

Documentation

Read the documentation at leanclient.readthedocs.io.

Run make docs to build the documentation locally.

Benchmarks

See documentation for more information.

Testing

make install            # Installs python package and dev dependencies with uv
make test               # Run all tests, also installs fresh lean env if not found
make test-profile       # Run all tests with cProfile

Related Projects

Lean LSP Clients

Lean REPLs

License & Citation

MIT licensed. See LICENSE for more information.

Citing this repository is highly appreciated but not required by the license.

@software{leanclient2025,
  author = {Oliver Dressler},
  title = {{leanclient: Python client to interact with the lean4 language server}},
  url = {https://github.com/oOo0oOo/leanclient},
  month = {1},
  year = {2025}
}

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

leanclient-0.7.0.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

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

leanclient-0.7.0-py3-none-any.whl (36.6 kB view details)

Uploaded Python 3

File details

Details for the file leanclient-0.7.0.tar.gz.

File metadata

  • Download URL: leanclient-0.7.0.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.15

File hashes

Hashes for leanclient-0.7.0.tar.gz
Algorithm Hash digest
SHA256 d0677d4ee9f303556898b96281ce1de4d282cb4445b85733ce9fae3e16d97ffd
MD5 e0d8d26684c7b87616a47b53d59e0e6e
BLAKE2b-256 0fcd4e6cef2273426ab32caf23b305f7b1a7d42c494ec2c79883537d31dfa4ff

See more details on using hashes here.

File details

Details for the file leanclient-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: leanclient-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 36.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.15

File hashes

Hashes for leanclient-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 85dd7674ffd9f09003808d46268bdd82879af3c725e1a96e54d588ebf34838dc
MD5 0add1ecf7191057a62fc17a29d85aa70
BLAKE2b-256 6f80148c286225939ecbf4cf6bf9e8e20a36bb77d224ebd1c562964179f0986c

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