|
| 1 | +# LSP Backend Integration Notes |
| 2 | + |
| 3 | +This document captures frictions and enhancement opportunities discovered while integrating new LSP backends into lsp-python-types. |
| 4 | + |
| 5 | +## ty Backend Integration (January 2026) |
| 6 | + |
| 7 | +### Frictions Encountered |
| 8 | + |
| 9 | +#### 1. Virtual Document Support |
| 10 | + |
| 11 | +**Issue**: ty requires files to exist on disk before it can provide diagnostics, completion, and other features. Pyright and Pyrefly work with "virtual documents" opened via `didOpen` without requiring the file to exist on disk. |
| 12 | + |
| 13 | +**Impact**: Tests that don't write files to disk fail for ty. The following parametrized tests required `xfail` markers for ty: |
| 14 | +- `test_session_diagnostics` |
| 15 | +- `test_session_rename` |
| 16 | +- `test_session_completion` |
| 17 | +- `test_session_recycling_with_diagnostics` |
| 18 | +- `test_session_warmup_on_recycle` (in test_pool.py) |
| 19 | + |
| 20 | +**Workaround**: ty-specific tests must write files to disk using `tmp_path`: |
| 21 | +```python |
| 22 | +(tmp_path / "new.py").write_text(code) |
| 23 | +session = await Session.create(backend, base_path=tmp_path, initial_code=code) |
| 24 | +``` |
| 25 | + |
| 26 | +**Potential Enhancement**: Consider adding a `requires_file_on_disk` property to the `LSPBackend` protocol, allowing the Session class to automatically write files when needed. |
| 27 | + |
| 28 | +#### 2. `workspace/didChangeConfiguration` Not Supported |
| 29 | + |
| 30 | +**Issue**: ty logs `Received notification workspace/didChangeConfiguration which does not have a handler.` The Session class sends this notification after initialization to apply workspace settings. |
| 31 | + |
| 32 | +**Impact**: Runtime configuration changes via `didChangeConfiguration` don't work with ty. However, configuration written to `ty.toml` is respected. |
| 33 | + |
| 34 | +**Current Behavior**: The notification is sent but ignored by ty. No functional impact since config file is written first. |
| 35 | + |
| 36 | +**Potential Enhancement**: Add an optional `supports_did_change_configuration` flag to backends so the Session can skip sending this notification when unsupported. |
| 37 | + |
| 38 | +#### 3. Nested Configuration Structure |
| 39 | + |
| 40 | +**Issue**: ty uses nested TOML sections (`[environment]`, `[src]`, `[rules]`) unlike Pyrefly's flat structure. This required implementing recursive key conversion. |
| 41 | + |
| 42 | +**Solution**: Created `_convert_keys_to_kebab()` function in `lsp_types/ty/backend.py`: |
| 43 | +```python |
| 44 | +def _convert_keys_to_kebab(obj: t.Mapping[str, t.Any]) -> dict[str, t.Any]: |
| 45 | + """Recursively convert dict keys from snake_case to kebab-case.""" |
| 46 | + result: dict[str, t.Any] = {} |
| 47 | + for key, value in obj.items(): |
| 48 | + kebab_key = key.replace("_", "-") |
| 49 | + if isinstance(value, dict): |
| 50 | + result[kebab_key] = _convert_keys_to_kebab(value) |
| 51 | + elif isinstance(value, list): |
| 52 | + result[kebab_key] = [ |
| 53 | + _convert_keys_to_kebab(v) if isinstance(v, dict) else v |
| 54 | + for v in value |
| 55 | + ] |
| 56 | + else: |
| 57 | + result[kebab_key] = value |
| 58 | + return result |
| 59 | +``` |
| 60 | + |
| 61 | +**Potential Enhancement**: Extract this utility to a shared module (`lsp_types/utils.py`) since Pyrefly also uses TOML with kebab-case keys (though currently with flat structure). |
| 62 | + |
| 63 | +#### 4. Hover Information Format Differences |
| 64 | + |
| 65 | +**Issue**: ty's hover response shows just the type (`str`) rather than `variable_name: type` format used by Pyright and Pyrefly. |
| 66 | + |
| 67 | +**Impact**: Test assertions checking for variable names in hover text fail for ty. |
| 68 | + |
| 69 | +**Workaround**: Added backend-specific assertion in `test_session_hover`: |
| 70 | +```python |
| 71 | +if backend_name != "ty": |
| 72 | + assert "result" in hover_text |
| 73 | +assert "str" in hover_text |
| 74 | +``` |
| 75 | + |
| 76 | +#### 5. No CLI Flags for LSP Server |
| 77 | + |
| 78 | +**Issue**: Unlike Pyrefly which accepts `--verbose`, `--threads`, and `--indexing-mode` CLI flags, ty's `server` command accepts no configuration flags. |
| 79 | + |
| 80 | +**Impact**: All configuration must be done via `ty.toml`. This is actually simpler than Pyrefly's hybrid approach. |
| 81 | + |
| 82 | +**Solution**: `create_process_launch_info()` simply returns `["ty", "server"]` without any conditional flag building. |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +## Enhancement Opportunities |
| 87 | + |
| 88 | +### 1. Shared TOML Key Conversion Utility |
| 89 | + |
| 90 | +Both Pyrefly and ty use TOML with kebab-case keys but Python code uses snake_case. Consider creating: |
| 91 | + |
| 92 | +```python |
| 93 | +# lsp_types/utils.py |
| 94 | +def snake_to_kebab_recursive(obj: Mapping[str, Any]) -> dict[str, Any]: |
| 95 | + """Recursively convert dict keys from snake_case to kebab-case.""" |
| 96 | + # ... implementation |
| 97 | +``` |
| 98 | + |
| 99 | +Then refactor both backends to use this shared utility. |
| 100 | + |
| 101 | +### 2. Backend Capability Flags |
| 102 | + |
| 103 | +Add optional flags to the `LSPBackend` protocol for: |
| 104 | +- `requires_file_on_disk: bool` - Whether backend needs files to exist on disk |
| 105 | +- `supports_did_change_configuration: bool` - Whether backend handles `workspace/didChangeConfiguration` |
| 106 | + |
| 107 | +This would allow the Session class to adapt its behavior automatically. |
| 108 | + |
| 109 | +### 3. Common LSP Capabilities Base |
| 110 | + |
| 111 | +Create a helper function for shared capabilities: |
| 112 | + |
| 113 | +```python |
| 114 | +def get_base_python_capabilities() -> types.ClientCapabilities: |
| 115 | + """Common LSP capabilities for Python type checkers.""" |
| 116 | + return { |
| 117 | + "textDocument": { |
| 118 | + "publishDiagnostics": {...}, |
| 119 | + "hover": {...}, |
| 120 | + "signatureHelp": {}, |
| 121 | + } |
| 122 | + } |
| 123 | +``` |
| 124 | + |
| 125 | +Backends could extend this base instead of duplicating the boilerplate. |
| 126 | + |
| 127 | +### 4. Backend Registry Pattern |
| 128 | + |
| 129 | +For easier discovery and testing: |
| 130 | + |
| 131 | +```python |
| 132 | +_BACKENDS: dict[str, type[LSPBackend]] = {} |
| 133 | + |
| 134 | +def register_backend(name: str): |
| 135 | + def decorator(cls): |
| 136 | + _BACKENDS[name] = cls |
| 137 | + return cls |
| 138 | + return decorator |
| 139 | + |
| 140 | +@register_backend("ty") |
| 141 | +class TyBackend(LSPBackend): |
| 142 | + ... |
| 143 | +``` |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +## Summary |
| 148 | + |
| 149 | +The ty backend integration revealed that different LSP servers have varying requirements around file handling and configuration. The current abstraction works but could benefit from: |
| 150 | + |
| 151 | +1. Optional capability flags on backends |
| 152 | +2. Shared utilities for common patterns (TOML conversion, base capabilities) |
| 153 | +3. Better documentation of backend-specific behaviors |
| 154 | + |
| 155 | +The core `LSPBackend` protocol and `Session` class work well across all three backends (Pyright, Pyrefly, ty) with minimal backend-specific handling needed in tests. |
0 commit comments