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).
pip install lsp-typesUsing the LSP types:
import lsp_types
# Use the typesUsing 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_listenerThe following LSPs are available out of the box:
| Symbol | Meaning |
|---|---|
| ✅ | Fully supported |
| Partial support (see notes) | |
| ❌ | Not supported |
| ❔ | Not tested / Not exposed in API |
Last verified: basedpyright 1.36.2, Pyrefly 0.48.2, ty 0.0.12
| Feature | Pyright | Pyrefly | ty | Notes |
|---|---|---|---|---|
| Diagnostics | ✅ | ✅ | ty requires files on disk | |
| Hover | ✅ | ✅ | ✅ | ty shows type only, not variable name |
| Completion | ✅ | ✅ | ty requires files on disk | |
| Completion Resolution | ✅ | ❌ | ✅ | Pyrefly: not yet supported |
| Signature Help | ✅ | ✅ | ✅ | |
| Rename | ✅ | Pyrefly: disabled for external files; ty: requires files on disk | ||
| Semantic Tokens | ✅* | ✅ | ✅ | *basedpyright recommended for extended features |
| Go to Definition | ❔ | ❔ | ❔ | Not exposed in Session API |
| Find References | ❔ | ❔ | ❔ | Not exposed in Session API |
| Code Actions | ❔ | ❔ | ❔ | Not exposed in Session API |
| Formatting | ❔ | ❔ | ❔ | Not exposed in Session API |
See Feature Verification Guide for methodology on maintaining this table.
For detailed backend limitations:
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()- Requires Python 3.12+.
- Requires
uvfor dev dependencies.
Generate latest types in one go:
make generate-latest-typesDownload the latest json schema:
make download-schemasGenerate the types:
make generate-typesCopy 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.
- Support server request handlers.
