Skip to content

PyrightServer

Base classes

Name Children Inherits
LSPServerInfo
anyenv.lsp_servers._base
Static configuration for an LSP server.

⋔ Inheritance diagram

graph TD
  3324114589["python_lsps.PyrightServer"]
  514706066["_base.LSPServerInfo"]
  2751680898["builtins.object"]
  514706066 --> 3324114589
  2751680898 --> 514706066

🛈 DocStrings

Bases: LSPServerInfo

Pyright with virtualenv detection and JSON diagnostic parsing.

Source code in src/anyenv/lsp_servers/_servers/python_lsps.py
@dataclass
class PyrightServer(LSPServerInfo):
    """Pyright with virtualenv detection and JSON diagnostic parsing."""

    async def resolve_initialization(self, root: str, fs: AsyncFileSystem) -> dict[str, Any]:
        """Detect virtualenv and set pythonPath."""
        init = await super().resolve_initialization(root, fs)

        for venv_path in get_python_venv_candidates(root):
            if os.name == "nt":
                python = posixpath.join(venv_path, "Scripts", "python.exe")
            else:
                python = posixpath.join(venv_path, "bin", "python")
            try:
                if await fs._exists(python):  # noqa: SLF001
                    init["pythonPath"] = python
                    break
            except Exception:  # noqa: BLE001
                pass

        return init

    def _parse_json_diagnostics(self, output: str) -> list[Diagnostic]:
        """Parse pyright JSON output."""
        import anyenv

        diagnostics: list[Diagnostic] = []

        try:
            # Find JSON object in output (may have warnings before it)
            json_start = output.find("{")
            if json_start == -1:
                return diagnostics
            data = anyenv.load_json(output[json_start:], return_type=dict)

            for diag in data.get("generalDiagnostics", []):
                range_info = diag.get("range", {})
                start = range_info.get("start", {})
                end = range_info.get("end", {})

                diagnostics.append(
                    Diagnostic(
                        file=diag.get("file", ""),
                        line=start.get("line", 0) + 1,  # pyright uses 0-indexed
                        column=start.get("character", 0) + 1,
                        end_line=end.get("line", start.get("line", 0)) + 1,
                        end_column=end.get("character", start.get("character", 0)) + 1,
                        severity=severity_from_string(diag.get("severity", "error")),
                        message=diag.get("message", ""),
                        code=diag.get("rule"),
                        source=self.id,
                    )
                )
        except anyenv.JsonLoadError:
            pass

        return diagnostics

resolve_initialization async

resolve_initialization(root: str, fs: AsyncFileSystem) -> dict[str, Any]

Detect virtualenv and set pythonPath.

Source code in src/anyenv/lsp_servers/_servers/python_lsps.py
async def resolve_initialization(self, root: str, fs: AsyncFileSystem) -> dict[str, Any]:
    """Detect virtualenv and set pythonPath."""
    init = await super().resolve_initialization(root, fs)

    for venv_path in get_python_venv_candidates(root):
        if os.name == "nt":
            python = posixpath.join(venv_path, "Scripts", "python.exe")
        else:
            python = posixpath.join(venv_path, "bin", "python")
        try:
            if await fs._exists(python):  # noqa: SLF001
                init["pythonPath"] = python
                break
        except Exception:  # noqa: BLE001
            pass

    return init

:fa-brands-github: Show source on GitHub