LSP Types
Publish the excellent work of Sublime LSP as a PyPI package.
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:
import lsp_types
# Use the types
Using an LSP process through stdio:
[!TIP] Recommend using basedpyright for extended features.
from lsp_types.process import LSPProcess, ProcessLaunchInfo
process_info = ProcessLaunchInfo(cmd=[
"pyright-langserver", "--stdio"
])
async with LSPProcess(process_info) as process:
# Initialize the process
...
# Grab a typed listener
diagnostics_listener = process.notify.on_publish_diagnostics(timeout=1.0)
# Send a notification (`await` is optional. It ensures messages have been drained)
await process.notify.did_open_text_document(...)
# Wait for diagnostics to come in
diagnostics = await diagnostics_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):
| Command | What it does |
|---|---|
pyrefly init |
Scaffold a pyrefly.toml (or [tool.pyrefly] in pyproject.toml), or migrate an existing mypy/pyright config to Pyrefly |
pyrefly check |
Full type check of a file or project |
pyrefly snippet <CODE> |
Type-check an inline code snippet |
pyrefly infer |
Automatically add inferred type annotations to a file or directory |
pyrefly coverage |
Type-coverage reporting commands |
pyrefly suppress |
Add ignore comments for existing errors, or remove unused ignores |
pyrefly stubgen |
Generate .pyi stub files from Python source |
pyrefly dump-config |
Print Pyrefly's resolved configuration |
pyrefly tsp |
Start 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
| Symbol | Meaning |
|---|---|
| :white_check_mark: | Fully supported |
| :warning: | Partial support (see notes) |
| :x: | Not supported |
| :grey_question: | 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
| Feature | Pyright | Pyrefly | ty | Zuban | Notes |
|---|---|---|---|---|---|
| Diagnostics | :white_check_mark: | :white_check_mark: | :warning: | :white_check_mark: | ty requires files on disk |
| Hover | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | ty shows type only, not variable name |
| Completion | :white_check_mark: | :white_check_mark: | :warning: | :white_check_mark: | ty requires files on disk |
| Completion Resolution | :white_check_mark: | :x: | :x: | :white_check_mark: | Pyrefly: no-op (returns item unchanged); ty: not supported (-32601) |
| Signature Help | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | |
| Rename | :white_check_mark: | :white_check_mark: | :warning: | :white_check_mark: | ty: requires files on disk |
| Semantic Tokens | :white_check_mark:* | :white_check_mark:** | :white_check_mark: | :white_check_mark: | *basedpyright recommended; **Pyrefly: legend not advertised (see docs) |
| Go to Definition | :grey_question: | :grey_question: | :grey_question: | :grey_question: | Not exposed in Session API |
| Find References | :grey_question: | :grey_question: | :grey_question: | :grey_question: | Not exposed in Session API |
| Code Actions | :grey_question: | :grey_question: | :grey_question: | :grey_question: | Not exposed in Session API |
| Formatting | :grey_question: | :grey_question: | :grey_question: | :grey_question: | Not exposed in Session API |
See Feature Verification Guide for methodology on maintaining this table.
For detailed documentation:
- Semantic Tokens Reference - Token types and modifiers for Monaco/editor integration
- Pyrefly Known Limitations
- ty Known Limitations
- Zuban Known Limitations
Pyright Example
from lsp_types import Session
from lsp_types.pyright.backend import PyrightBackend
async def test_pyright_session():
code = """\
def greet(name: str) -> str:
return 123
"""
session = await Session.create(PyrightBackend(), initial_code=code)
diagnostics = await session.get_diagnostics()
assert diagnostics != []
code = """\
def greet(name: str) -> str:
return f"Hello, {name}"
"""
await session.update_code(code)
diagnostics = await session.get_diagnostics()
assert diagnostics == []
await session.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
uvfor 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.
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 lsp_types-0.22.0.tar.gz.
File metadata
- Download URL: lsp_types-0.22.0.tar.gz
- Upload date:
- Size: 87.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d2e45b10247a4b40dbb70188965d6616540311b5b88175a681414d4569a3a44a
|
|
| MD5 |
6da4713bb0b3381f67c1fa7d0f03f525
|
|
| BLAKE2b-256 |
291452c16b1aa5ec9484b169e8b81f703adf2d69f7070ac131bacfd98e2f2a9c
|
Provenance
The following attestation bundles were made for lsp_types-0.22.0.tar.gz:
Publisher:
publish.yml on Mazyod/lsp-python-types
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lsp_types-0.22.0.tar.gz -
Subject digest:
d2e45b10247a4b40dbb70188965d6616540311b5b88175a681414d4569a3a44a - Sigstore transparency entry: 2321838560
- Sigstore integration time:
-
Permalink:
Mazyod/lsp-python-types@d3729998ceba2ffc656b1769c8295067d309ae1d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Mazyod
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d3729998ceba2ffc656b1769c8295067d309ae1d -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file lsp_types-0.22.0-py3-none-any.whl.
File metadata
- Download URL: lsp_types-0.22.0-py3-none-any.whl
- Upload date:
- Size: 95.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a900d7580043f5692f092beacc78c506ddb117622208ac1fb60ffea549d72a8
|
|
| MD5 |
b3e822cde420e2d794293570ee622f87
|
|
| BLAKE2b-256 |
c9e5ed8ea93079583289afef0492f1b0f2a65f87f23f6c163f1c41d2722269b0
|
Provenance
The following attestation bundles were made for lsp_types-0.22.0-py3-none-any.whl:
Publisher:
publish.yml on Mazyod/lsp-python-types
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lsp_types-0.22.0-py3-none-any.whl -
Subject digest:
6a900d7580043f5692f092beacc78c506ddb117622208ac1fb60ffea549d72a8 - Sigstore transparency entry: 2321838645
- Sigstore integration time:
-
Permalink:
Mazyod/lsp-python-types@d3729998ceba2ffc656b1769c8295067d309ae1d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Mazyod
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d3729998ceba2ffc656b1769c8295067d309ae1d -
Trigger Event:
workflow_dispatch
-
Statement type: