Python Language Server Protocol implementation providing code intelligence features for Python development
npx @tessl/cli install tessl/pypi-python-lsp-server@1.13.0A comprehensive Python implementation of the Language Server Protocol that provides advanced code intelligence features for Python development. The server offers completion, definitions, hover information, references, signature help, symbols, linting, formatting, and extensive plugin architecture for extending functionality across multiple editors and IDEs.
pip install python-lsp-serverpylsp command-line interfaceimport pylsp
from pylsp import hookimpl, hookspec
from pylsp.python_lsp import PythonLSPServerFor plugin development:
from pylsp import hookimpl
from pylsp.config.config import Config
from pylsp.workspace import Workspace, Document# Start server with stdio
pylsp
# Start TCP server
pylsp --tcp --host 127.0.0.1 --port 2087
# Start WebSocket server
pylsp --ws --port 2087
# Enable verbose logging
pylsp -v
# Use custom log configuration
pylsp --log-config /path/to/log_config.jsonfrom pylsp.python_lsp import (
PythonLSPServer,
start_io_lang_server,
start_tcp_lang_server,
start_ws_lang_server
)
import sys
# Start stdio server
stdin, stdout = sys.stdin.buffer, sys.stdout.buffer
start_io_lang_server(stdin, stdout, False, PythonLSPServer)
# Start TCP server
start_tcp_lang_server("127.0.0.1", 2087, False, PythonLSPServer)
# Start WebSocket server
start_ws_lang_server(2087, False, PythonLSPServer)from pylsp import hookimpl
@hookimpl
def pylsp_lint(config, workspace, document, is_saved):
"""Provide custom linting diagnostics."""
return [{
"range": {
"start": {"line": 0, "character": 0},
"end": {"line": 0, "character": 10}
},
"message": "Custom diagnostic message",
"severity": 1 # Error
}]
@hookimpl
def pylsp_completions(config, workspace, document, position, ignored_names):
"""Provide custom completions."""
return [{
"label": "custom_function",
"kind": 3, # Function
"detail": "Custom completion",
"documentation": "A custom completion item"
}]The python-lsp-server is built on a modular plugin architecture using the Pluggy framework:
This architecture enables the server to provide consistent Python language intelligence while supporting extensive customization and third-party plugin development.
Core server functionality including startup, configuration, and lifecycle management. Provides multiple server modes (stdio, TCP, WebSocket) and comprehensive CLI interface.
def start_io_lang_server(rfile, wfile, check_parent_process, handler_class): ...
def start_tcp_lang_server(bind_addr, port, check_parent_process, handler_class): ...
def start_ws_lang_server(port, check_parent_process, handler_class): ...
class PythonLSPServer:
def __init__(self, rx, tx, check_parent_process=False, consumer=None, *, endpoint_cls=None): ...
def start(self): ...
def capabilities(self): ...Comprehensive plugin system using Pluggy hooks for extending server functionality. Provides 40+ hook specifications covering all LSP features and server lifecycle events.
@hookimpl
def pylsp_lint(config, workspace, document, is_saved): ...
@hookimpl
def pylsp_completions(config, workspace, document, position, ignored_names): ...
@hookimpl
def pylsp_hover(config, workspace, document, position): ...
@hookimpl
def pylsp_definitions(config, workspace, document, position): ...Document and workspace management with Jedi integration, configuration handling, and LSP workspace features including diagnostics publishing and progress reporting.
class Workspace:
def get_document(self, doc_uri): ...
def put_document(self, doc_uri, source, version=None): ...
def update_document(self, doc_uri, change, version=None): ...
def publish_diagnostics(self, doc_uri, diagnostics, doc_version=None): ...
def apply_edit(self, edit): ...
class Document:
def jedi_script(self, position=None, use_document_path=False): ...
def word_at_position(self, position): ...
def offset_at_position(self, position): ...Hierarchical configuration system supporting workspace-level, plugin-specific, and document-specific settings with integration to external configuration files.
class Config:
def settings(self, document_path=None): ...
def plugin_settings(self, plugin, document_path=None): ...
def update(self, settings): ...
def find_parents(self, path, names): ...Comprehensive utility functions for LSP operations, text manipulation, URI handling, and Python-specific operations like path-to-module conversion.
def debounce(interval_s, keyed_by=None): ...
def find_parents(root, path, names): ...
def format_docstring(contents, markup_kind, signatures=None, signature_config=None): ...
def to_fs_path(uri): ...
def from_fs_path(path): ...