Skip to content

Repository files navigation

LSP Types

PyPI versionPythonTestsLicense: MIT

Publish the excellent work of Sublime LSP as a PyPI package.

lsp-types-splash

LSP Types is a Python package that aims to provide a fully typed interface to Language Server Protocol (LSP) interactions. It can be used to simply utilize the types, or to interact with an LSP server over stdio.

The library has minimal dependencies (tomli-w for TOML config serialization).

Installation

pip install lsp-types

Usage

Using the LSP types:

importlsp_types# Use the types

Using an LSP process through stdio:

Tip

Recommend using basedpyright for extended features.

fromlsp_types.processimportLSPProcess, ProcessLaunchInfoprocess_info=ProcessLaunchInfo(cmd=[
"pyright-langserver", "--stdio"
])
asyncwithLSPProcess(process_info) asprocess:
# Initialize the process
...
# Grab a typed listenerdiagnostics_listener=process.notify.on_publish_diagnostics(timeout=1.0)
# Send a notification (`await` is optional. It ensures messages have been drained)awaitprocess.notify.did_open_text_document(...)
# Wait for diagnostics to come indiagnostics=awaitdiagnostics_listener

LSPProcess.stop() is terminal — including the implicit stop() when the async with block exits. Calling start() on a stopped process raises RuntimeError instead of relaunching the server, and requests and notifications sent through it raise RuntimeError too (notifications are no longer dropped with a warning). The messages name the state they came from (LSP process has been stopped vs. LSP process has not been started). Construct a new LSPProcess when you need to restart a server.

LSPs

The following LSPs are available out of the box:

  • Pyright
  • Pyrefly
  • ty - Astral's fast Python type checker
  • Zuban - Rust-based type checker + LSP by the author of Jedi

Pyrefly CLI tools

This library drives Pyrefly's LSP server (pyrefly lsp), but Pyrefly also ships a broader standalone CLI worth knowing about (verified with Pyrefly 1.2.0):

CommandWhat it does
pyrefly initScaffold a pyrefly.toml (or [tool.pyrefly] in pyproject.toml), or migrate an existing mypy/pyright config to Pyrefly
pyrefly checkFull type check of a file or project
pyrefly snippet <CODE>Type-check an inline code snippet
pyrefly inferAutomatically add inferred type annotations to a file or directory
pyrefly coverageType-coverage reporting commands
pyrefly suppressAdd ignore comments for existing errors, or remove unused ignores
pyrefly stubgenGenerate .pyi stub files from Python source
pyrefly dump-configPrint Pyrefly's resolved configuration
pyrefly tspStart a TSP (Type Server Protocol) server (new in 1.2.x)

Run pyrefly <command> --help for details, or see the Pyrefly docs.

Feature Support Matrix

Legend

SymbolMeaning
Fully supported
⚠️Partial support (see notes)
Not supported
Not tested / Not exposed in API

Features by Backend

Last verified: basedpyright 1.39.9, Pyrefly 1.2.0, ty 0.0.61, Zuban 0.9.0

FeaturePyrightPyreflytyZubanNotes
Diagnostics⚠️ty requires files on disk
Hoverty shows type only, not variable name
Completion⚠️ty requires files on disk
Completion ResolutionPyrefly: no-op (returns item unchanged); ty: not supported (-32601)
Signature Help
Rename⚠️ty: requires files on disk
Semantic Tokens✅*✅***basedpyright recommended; **Pyrefly: legend not advertised (see docs)
Go to DefinitionNot exposed in Session API
Find ReferencesNot exposed in Session API
Code ActionsNot exposed in Session API
FormattingNot exposed in Session API

See Feature Verification Guide for methodology on maintaining this table.

For detailed documentation:

Pyright Example

fromlsp_typesimportSessionfromlsp_types.pyright.backendimportPyrightBackendasyncdeftest_pyright_session():
code="""\def greet(name: str) -> str: return 123"""session=awaitSession.create(PyrightBackend(), initial_code=code)
diagnostics=awaitsession.get_diagnostics()
assertdiagnostics!= []
code="""\def greet(name: str) -> str: return f"Hello, {name}""""awaitsession.update_code(code)
diagnostics=awaitsession.get_diagnostics()
assertdiagnostics== []
awaitsession.shutdown()

After shutdown(), a session's operational methods raise RuntimeError; its captured server and semantic-token metadata remain readable. Calling shutdown() while other operations are in flight is safe: it waits up to five seconds for them to finish, and if any are still running it stops the language server process instead of returning it to the pool, keeping stale operations out of the next session's protocol stream. (One narrow exception: cancelling an operation ends its in-flight accounting even if a notification write it already queued is still being flushed.)

Development

  • Requires Python 3.12+.
  • Requires uv for dev dependencies.

Generate latest types in one go:

make generate-latest-types

Download the latest json schema:

make download-schemas

Generate the types:

make generate-types

Copy the lsp_types/types.py file to your project.

NOTE: Do not import types that begin with __. These types are internal types and are not meant to be used.

TODOs

  • Support server request handlers.

Releases

Used by

Contributors

Languages