/** * Python language adapter backed by Pyright (ADR-13). * * Wraps `pyright-langserver` spawned as a subprocess, speaking LSP * over stdio. Implements the `LanguageAdapter` interface from * src/types.ts per ADR-03 (adapters are plugins) and ADR-07 * (getTypeInfo is a required capability). * * Divergences from TypeScriptAdapter documented in ADR-13: * - Pyright does NOT implement textDocument/implementation; * usedByTypes is computed via source-file declaration parsing * + optional pass-1 Protocol cache for full-indexing runs. * - Pyright hover omits class declaration headers; class * signatures are built from source, other kinds use hover text. * - Pyright emits uniform LSP kinds for Protocol / ABC / * dataclass; Protocol→interface remap requires source-line * inspection + three-mechanism Protocol detection. * - Type aliases (all three Python syntactic forms) collapse to * LSP kind 13 (Variable); hover `(type)` prefix drives remap. * * See `docs/adr/pyright-probe-findings.md` for primary-source LSP * behavior capture that motivated these divergences. */ import { type Diagnostic, type LanguageAdapter, type LanguageCode, type Reference, type Symbol as AtlasSymbol, type SymbolId, type TypeInfo } from "../types.js"; export interface PyrightAdapterOptions { /** * Override the path to the pyright-langserver entry point. If omitted, * resolved from the consuming project's node_modules. */ cliPath?: string; /** Upper bound on a single LSP request. */ requestTimeoutMs?: number; } /** Extensions indexed in v0.1 per ADR-13's "File extensions" decision. */ export declare const PYTHON_EXTENSIONS: readonly string[]; export declare class PyrightAdapter implements LanguageAdapter { readonly language: LanguageCode; readonly extensions: readonly string[]; private readonly client; private readonly options; private rootPath; private readonly openFiles; private readonly diagnosticsByUri; private readonly diagnosticsListeners; /** * Pass-1 Protocol cache — populated by `buildProtocolCache()` during * full-indexing runs (ADR-13 §Two-pass indexing). Single-symbol * operations do NOT populate this; getTypeInfo falls back to * mechanisms (a)+(b) only when the cache is empty, which is the * documented degraded mode. */ private readonly protocolCache; constructor(options?: PyrightAdapterOptions); initialize(rootPath: string): Promise; private warmupProject; private ensureOpenSync; private ensureOpen; shutdown(): Promise; listSymbols(filePath: string): Promise; /** * Turn a single LSP DocumentSymbol into an Atlas Symbol, applying the * ADR-13 kind remaps (Protocol→interface, type-alias→type, * constant→variable-with-hint). Returns null for kinds we discard * (parameters under methods, instance variables). */ private mapDocumentSymbol; getSymbolDetails(id: SymbolId): Promise; findReferences(id: SymbolId): Promise; getDiagnostics(filePath: string): Promise; getDocstring(id: SymbolId): Promise; getTypeInfo(id: SymbolId): Promise; /** * Pass-1: populate the Protocol cache by walking every opened file, * identifying classes whose declaration header inherits from a * Protocol, and recording their SymbolId → true. * * Intended to run during full-indexing (ADR-13 §Two-pass indexing). * `contextatlas index` orchestrator should call this after warmup, * before any getTypeInfo calls. Query-time paths can skip it and * rely on the (a)+(b) degraded mode. */ buildProtocolCache(): Promise; private cacheMarksProtocolByName; private resolveFile; private toAbs; private toAtlasSymbol; private symbolId; private referenceId; } export interface ParsedClassDeclaration { /** * Base class names in source order, with generic brackets stripped. * Dotted names (`typing.Protocol`) preserved as single tokens. */ bases: string[]; /** * Human-readable class signature derived from the declaration. Empty * when the class has no bases (`class Foo:` → `class Foo`). */ signature: string | null; } /** * Extract the `class Name(...)` declaration starting at `startLine` * (0-indexed). Collects text from the `class` keyword through the * matching `)` (or the end of the `class Name:` form if no `(` is * present), spanning newlines with paren-depth tracking. * * Returns "" when the start line doesn't contain `class `. */ export declare function extractClassDeclaration(sourceText: string, startLine: number, maxLines?: number): string; /** * Parse the output of {@link extractClassDeclaration} into bases and * a synthetic signature. Implements ADR-13's six parser rules. */ export declare function parseClassDeclaration(header: string): ParsedClassDeclaration; /** * Split a parenthesized base list (the text between `(` and `)`) into * its constituent base names per ADR-13 parser rules 3-6: * - Strip balanced `[...]` spans (drops generics) * - Split on commas at depth 0 * - Drop kwarg tokens (`name=...`) * - Strip trailing `[...]` from each surviving token * - Reject tokens that aren't valid identifiers (or dotted identifiers) */ export declare function splitBaseList(inner: string): string[]; export interface ImportAliases { /** * Map from local name → original name. `from typing import Protocol * as Interface` produces `Interface → Protocol`. `import typing` * produces no entry (module imports don't alias the symbol). * * Used to resolve aliased Protocol references in class base lists. */ nameToOriginal: Map; /** * Set of qualified Protocol references that should be treated as * Protocols. Populated when the file imports `typing.Protocol` or * `typing_extensions.Protocol` in any form. */ protocolAliases: Set; } /** * Parse the top-of-file import statements for Protocol aliases. * Recognizes: * from typing import Protocol * from typing import Protocol as Interface * from typing_extensions import Protocol * import typing * import typing as t * * Only scans top-level imports — nested / conditional imports ignored. * Stops at the first non-import, non-blank, non-comment line. */ export declare function parseImportAliases(sourceText: string): ImportAliases; export declare function isProtocolBase(baseName: string, imports: ImportAliases): boolean; /** * Scan source text for classes whose declaration includes `targetName` * as a base (matching the common-surface patterns). Returns the set of * derived class names. Used by PyrightAdapter.getTypeInfo to compute * usedByTypes in lieu of textDocument/implementation. * * Matching is name-equality; dotted-prefix stripping applies so * `typing.Protocol` in a base list matches `Protocol` queries. */ export declare function findClassesExtending(sourceText: string, targetName: string): string[]; /** * Walk source text for classes whose declaration inherits from a * Protocol (per mechanisms a+b). Used by PyrightAdapter.buildProtocolCache * during pass-1 of a full-indexing run. */ export declare function findClassesMatchingProtocol(sourceText: string, imports: ImportAliases): string[]; /** * Detect a module-level type-alias line per ADR-13's three syntactic * forms: * UserIdV1 = str * UserIdV2: TypeAlias = str * type UserIdV3 = str (PEP 695) * * The Pyright LSP reports all three as kind 13 (Variable); we remap to * `type` when the source matches one of these patterns. */ export declare function isTypeAliasLine(line: string, name: string): boolean; /** * Extract the `def name(...)` line for a function/method, spanning * multi-line signatures via paren tracking. Returns the single-line * normalized signature (e.g. `def greet(name: str) -> str`). */ export declare function extractCallableSignatureLine(sourceText: string, startLine: number, maxLines?: number): string | null; /** * Parse a Python module-level docstring from source text per PEP 257. * * Returns the docstring text (PEP 257 dedented) if the file's first * statement (after shebang, encoding declaration, comment lines, blank * lines, and `from __future__ import` statements) is a triple-quoted * string. Otherwise returns null. * * Module-level docstring keyed via the synthetic SymbolId * `sym:py::` per v0.3-SCOPE Stream B item 2. */ export declare function parsePythonModuleDocstring(sourceText: string): string | null; /** * Parse a Python class/function/method-level docstring from source * text given the 1-indexed line number of the declaration. * * `declLine` is expected to point at `class Name(...)` / * `def name(...)` / `async def name(...)` (matches LSP * `selectionRange.start.line + 1` convention from pyright's * documentSymbol response). Decorators above this line are skipped * naturally since parsing starts AT the declaration line. * * Multi-line signatures (long parameter lists spanning many lines) * are handled via paren-nesting tracking to find the colon ending the * signature. * * Returns the docstring text (PEP 257 dedented) or null when no * docstring is present. */ export declare function parsePythonBodyDocstring(sourceText: string, declLine: number): string | null; //# sourceMappingURL=pyright.d.ts.map