diff --git a/docs/development/adding-language-support.md b/docs/development/adding-language-support.md index 57598be1a3..ec1c13b9b1 100644 --- a/docs/development/adding-language-support.md +++ b/docs/development/adding-language-support.md @@ -33,13 +33,13 @@ Every module under `server/code/domain/extract/` is either in a stage of `STAGE_ ## 1. Choose the lane's shape -| Shape | Examples | You provide | -| ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| Grammar lane | `lang_typescript.py`, `lang_rust.py`, `lang_go.py`, `lang_java.py` | A tree-sitter grammar dependency in `pyproject.toml`; symbols and call sites from the tree; chunks and edges come from the shared chunker and edge walker. | -| Tree-less lane | `lang_proto.py`, `lang_gotemplate.py`, `lang_hcl.py`, `lang_k8s.py`, `lang_jsonnet.py`, `lang_lua.py`, `lang_graphql.py` (`LANE_UNIT_LANGS`) | A hand-written scanner; `ParsedFile.tree` stays `None`; chunks from `lane_units.span_chunks`, edges from `reference_sites` + `SITE_RULES` bound by `lane_units.bind_sites`. | -| Include-only lane | `tla`, `flatbuffers`, `cmake`, `doc` (`_LANE_INCLUDE_LANGS`) | Symbols and includes resolved by the simple lane resolver. | -| Embedded language | SQL inside Go and Java (`go_sql_units`, `java_sql_units`), shell islands in Dockerfiles and Makefiles | Units joined into the host file's parse (`_join_island_symbols`), usually behind a kill switch setting. | -| Chunk-only | `.css`, text files (`lang_text.py`) | Nothing but detection: the file is chunked and searchable with no symbols. | +| Shape | Examples | You provide | +| ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| Grammar lane | `lang_typescript.py`, `lang_rust.py`, `lang_go.py`, `lang_java.py` | A tree-sitter grammar dependency in `pyproject.toml`; symbols and call sites from the tree; chunks and edges come from the shared chunker and edge walker. | +| Tree-less lane | `lang_proto.py`, `lang_thrift.py`, `lang_gotemplate.py`, `lang_hcl.py`, `lang_k8s.py`, `lang_jsonnet.py`, `lang_lua.py`, `lang_graphql.py` (`LANE_UNIT_LANGS`) | A hand-written scanner; `ParsedFile.tree` stays `None`; chunks from `lane_units.span_chunks`, edges from `reference_sites` + `SITE_RULES` bound by `lane_units.bind_sites`. | +| Include-only lane | `tla`, `flatbuffers`, `cmake`, `doc` (`_LANE_INCLUDE_LANGS`) | Symbols and includes resolved by the simple lane resolver. | +| Embedded language | SQL inside Go and Java (`go_sql_units`, `java_sql_units`), shell islands in Dockerfiles and Makefiles | Units joined into the host file's parse (`_join_island_symbols`), usually behind a kill switch setting. | +| Chunk-only | `.css`, text files (`lang_text.py`) | Nothing but detection: the file is chunked and searchable with no symbols. | A grammar lane's grammar is **not** in `STAMPED_GRAMMARS`, so upgrading it moves no stamp. The lane owns re-deriving its rows. diff --git a/docs/features/code-index/overview.md b/docs/features/code-index/overview.md index f223bd1957..2b0db21d15 100644 --- a/docs/features/code-index/overview.md +++ b/docs/features/code-index/overview.md @@ -137,6 +137,7 @@ still gets file-level impact. | Shell (`.sh`, `.bash`, `.bats`, shebang, rc files) | yes | calls (commands), `source` includes | — | | Prisma (`.prisma`) | yes | model relations | — | | Protocol Buffers (`.proto`), FlatBuffers (`.fbs`) | yes | imports, type uses | — | +| Thrift IDL (`.thrift`) | yes | includes, type uses, service `extends` | — | | Go templates and Helm charts (`.tpl`, `.tmpl`, `.gotmpl`, templated manifests) | yes | template calls | — | | Terraform / HCL (`.tf`, `.tfvars`, `.hcl`), Kubernetes manifests | yes | references (`reads`) | — | | Jsonnet (`.jsonnet`, `.libsonnet`) | yes | calls, imports | — | diff --git a/docs/use-cases/code-intelligence-by-language.md b/docs/use-cases/code-intelligence-by-language.md index 414b69b208..3094915605 100644 --- a/docs/use-cases/code-intelligence-by-language.md +++ b/docs/use-cases/code-intelligence-by-language.md @@ -64,7 +64,7 @@ files ([switches](../features/code-index/overview.md#language-support)). ## Infrastructure as code Terraform and HCL, Kubernetes manifests, Helm charts and Go templates, Jsonnet, CMake, -Protocol Buffers and shell scripts are indexed with their references, includes and calls, +Protocol Buffers, Thrift IDL and shell scripts are indexed with their references, includes and calls, so an agent follows a Helm `include` to its `define`, or a Terraform reference to what it names. Dockerfile, Makefile, Jenkinsfile and CI YAML jobs become symbols with `LOOM_CODE_HOST_ISLANDS` on, after a backfill of those files when the namespace is already diff --git a/server/code/app/index/ingest_parse.py b/server/code/app/index/ingest_parse.py index 9767547a71..93e7dc20eb 100644 --- a/server/code/app/index/ingest_parse.py +++ b/server/code/app/index/ingest_parse.py @@ -276,6 +276,12 @@ def _parse_java(path: str, source: bytes) -> ParsedFile: return pf +def _parse_thrift(path: str, source: bytes) -> ParsedFile: + from server.code.domain.extract.lang_thrift import parse_thrift + + return parse_thrift(path, source) + + def _parse_proto(path: str, source: bytes) -> ParsedFile: from server.code.domain.extract.lang_proto import parse_proto @@ -404,6 +410,7 @@ def _parse_doc(path: str, source: bytes) -> ParsedFile: "astro": _parse_astro, "angular_template": _parse_angular_template, "proto": _parse_proto, + "thrift": _parse_thrift, "gotemplate": _parse_gotemplate, "hcl": _parse_hcl, "k8s": _parse_k8s, @@ -423,7 +430,7 @@ def _parse_doc(path: str, source: bytes) -> ParsedFile: # The lanes without a tree-sitter grammar — chunks from their symbol spans, edges from # their reference sites (`lane_units`) LANE_UNIT_LANGS: frozenset[str] = frozenset( - {"proto", "gotemplate", "hcl", "k8s", "jsonnet", "lua", "graphql"} + {"proto", "thrift", "gotemplate", "hcl", "k8s", "jsonnet", "lua", "graphql"} ) @@ -445,6 +452,10 @@ def _lane_module(lang: str) -> Any: from server.code.domain.extract import lang_lua return lang_lua + if lang == "thrift": + from server.code.domain.extract import lang_thrift + + return lang_thrift if lang == "graphql": from server.code.domain.extract import lang_graphql diff --git a/server/code/domain/extract/lang_detect.py b/server/code/domain/extract/lang_detect.py index cfe8704705..f4235e3f93 100644 --- a/server/code/domain/extract/lang_detect.py +++ b/server/code/domain/extract/lang_detect.py @@ -273,6 +273,7 @@ def detect_shell(path: str, source: bytes) -> str: ".rego": "config", ".mdc": "doc", ".proto": "proto", + ".thrift": "thrift", ".graphql": "graphql", ".gql": "graphql", ".tpl": "gotemplate", diff --git a/server/code/domain/extract/lang_text.py b/server/code/domain/extract/lang_text.py index 8adf2a6eff..0e13b6b289 100644 --- a/server/code/domain/extract/lang_text.py +++ b/server/code/domain/extract/lang_text.py @@ -71,7 +71,6 @@ ".purs", ".re", ".hx", - ".thrift", ".avsc", ".capnp", ".asm", diff --git a/server/code/domain/extract/lang_thrift.py b/server/code/domain/extract/lang_thrift.py new file mode 100644 index 0000000000..5c7cab5349 --- /dev/null +++ b/server/code/domain/extract/lang_thrift.py @@ -0,0 +1,433 @@ +"""Thrift IDL: structs, unions, exceptions, enums, typedefs, services and their functions. + +A `.thrift` file is the contract between the services a repository builds (the generated +code is excluded as generated); reading it gives the index the API's own names. The +language is small and regular, so a token scanner with brace tracking reads it without a +tree-sitter grammar, in the shape of `lang_proto.py`: + + * `struct X { … }`, `union X { … }` and `exception X { … }` → a `struct` symbol; + `enum X { … }` → `enum`; `typedef T X` → `type`; `service S [extends B] { … }` → + `interface`; each function in a service → a `function` symbol `::S.f` whose + signature is the function as written. Qualified names follow the module path. + * reference sites: a field's, an argument's, a return's or a `throws` entry's named + type (`field_type`), and a service's `extends` (`extends`), bound by + `lane_units.bind_sites`; base types (`i32`, `string`, …) and the element types of + `list<>` / `set<>` / `map<>` are read through, so `list` names `User`. + A type qualified by an include (`shared.User`) binds by its last part. + * `include "shared.thrift"` is an include specifier; Thrift resolves it against the + including file's directory, then the `-I` paths, unknown to the index, so the + candidates are the file's directory and its nearest ancestors, then the root + (`lane_units.ancestor_candidates`), settled against the namespace's live paths. + +`ParsedFile.tree` stays None: chunks come from `lane_units.span_chunks`, edges from the +sites. Outside the extractor stamp with the other lanes. +""" + +from __future__ import annotations + +import re +from dataclasses import dataclass, field +from typing import Any + +from server.code.domain.extract.lane_units import SiteRule, ancestor_candidates +from server.code.domain.extract.lang_python import module_scope +from server.code.domain.extract.treesitter import ParsedFile, Symbol + +LANG_THRIFT = "thrift" + +KIND_STRUCT = "struct" +KIND_ENUM = "enum" +KIND_TYPEDEF = "type" +KIND_SERVICE = "interface" +KIND_FUNCTION = "function" + +SITE_FIELD_TYPE = "field_type" +SITE_EXTENDS = "extends" + +SITE_RULES: dict[str, SiteRule] = { + SITE_FIELD_TYPE: SiteRule("uses_type", frozenset({KIND_STRUCT, KIND_ENUM, KIND_TYPEDEF})), + SITE_EXTENDS: SiteRule("inherits", frozenset({KIND_SERVICE})), +} + +# how `lane_units.span_chunks` cuts a Thrift file +CHUNK_OPTIONS: dict[str, Any] = { + "comment_leaders": ("//", "#", "/*", "*"), + "comment_blocks": (("/*", "*/"),), + "header_prefixes": ("namespace", "include", "cpp_include"), +} + +BASE_TYPES: frozenset[str] = frozenset( + {"bool", "byte", "i8", "i16", "i32", "i64", "double", "string", "binary", "uuid", "void"} +) +_CONTAINERS: frozenset[str] = frozenset({"list", "set", "map"}) +_STRUCT_KEYWORDS: frozenset[str] = frozenset({"struct", "union", "exception"}) +_FIELD_REQ: frozenset[str] = frozenset({"required", "optional"}) +_XSD_FIELD_FLAGS: frozenset[str] = frozenset({"xsd_optional", "xsd_nillable"}) +_PAIRS: dict[str, str] = {"(": ")", "[": "]", "{": "}", "<": ">"} + +# An unclosed string is one token to the end of its line; an unclosed block comment runs +# to the end of the file, as Thrift's lexer reads them. +_TOKEN_RE = re.compile( + r"//[^\n]*|#[^\n]*|/\*.*?(?:\*/|\Z)|\"(?:[^\"\\\n]|\\.)*+\"?|'(?:[^'\\\n]|\\.)*+'?" + r"|[A-Za-z_][A-Za-z0-9_.]*|[+-]?(?:\d|\.\d)[\w.+-]*|[{}()<>,;:=\[\]]|\S", + re.S, +) + + +@dataclass +class _Tok: + text: str + line: int + + +@dataclass +class ThriftUnits: + symbols: list[Symbol] = field(default_factory=list) + sites: list[tuple[int, str, str]] = field(default_factory=list) + includes: list[str] = field(default_factory=list) + + +def _tokens(text: str) -> list[_Tok]: + out: list[_Tok] = [] + line = 1 + pos = 0 + for m in _TOKEN_RE.finditer(text): + line += text.count("\n", pos, m.start()) + pos = m.start() + t = m.group(0) + if not t.startswith(("//", "#", "/*")): + out.append(_Tok(t, line)) + return out + + +def _is_name(t: str) -> bool: + return bool(t) and (t[0].isalpha() or t[0] == "_") + + +def _is_literal(t: str) -> bool: + """A closed, non-empty string literal: `"shared.thrift"` or `'shared.thrift'`.""" + return len(t) > 2 and t[0] in "\"'" and t[-1] == t[0] + + +def _bare_type(name: str) -> str: + """`shared.User` / `User` → `User`; '' for a base type or a non-name.""" + if not _is_name(name) or name in BASE_TYPES or name in _CONTAINERS: + return "" + return name.rsplit(".", 1)[-1] + + +def _join(toks: list[_Tok]) -> str: + """Tokens back to source-like text: `User get(1: UserId id) throws(1: NotFound nf)`.""" + out = "" + for t in toks: + x = t.text + if out and x not in "(),:<>" and out[-1] not in "(<": + out += " " + out += x + if x in ",:": + out += " " + return re.sub(r" +", " ", out).strip() + + +class _Parser: + def __init__(self, path: str, toks: list[_Tok]) -> None: + self.path = path + scope = module_scope(path) + # A root file needs a dotted scope to pass the script-name gate. + self.mod = scope if "." in scope else f"{scope}.thrift" + self.toks = toks + self.i = 0 + self.units = ThriftUnits() + # each `{` token's matching `}`, found in one pass + self._closes: dict[int, int] = {} + opens: list[int] = [] + for j, t in enumerate(toks): + if t.text == "{": + opens.append(j) + elif t.text == "}" and opens: + self._closes[opens.pop()] = j + + def _add(self, name: str, kind: str, sig: str, start: int, end: int, scope: str = "") -> None: + self.units.symbols.append( + Symbol( + qualified_name=f"{self.mod}::{scope + '.' if scope else ''}{name}", + name=name, + kind=kind, + signature=sig, + path=self.path, + start_line=start, + end_line=max(end, start), + ) + ) + + def _site(self, line: int, type_name: str, kind: str = SITE_FIELD_TYPE) -> None: + bare = _bare_type(type_name) + if bare: + self.units.sites.append((line, bare, kind)) + + def _type_metadata_end(self, j: int, stop: int) -> int: + if j < stop and self.toks[j].text == "(": + return self._group_end(j, stop) + if ( + j + 1 < stop + and self.toks[j].text == "cpp_type" + and self.toks[j + 1].text.startswith(('"', "'")) + ): + return j + 2 + return j + + def _type(self, j: int, stop: int) -> int: + """Read a type and its metadata, recording only named dependencies.""" + if j >= stop: + return j + t = self.toks[j] + if _is_name(t.text): + self._site(t.line, t.text) + j += 1 + if t.text in _CONTAINERS: + j = self._type_metadata_end(j, stop) + if j < stop and self.toks[j].text == "<": + depth = 1 + j += 1 + while j < stop and depth: + after = self._type_metadata_end(j, stop) + if after != j: + j = after + continue + tk = self.toks[j].text + if tk == "<": + depth += 1 + elif tk == ">": + depth -= 1 + elif _is_name(tk): + self._site(self.toks[j].line, tk) + j += 1 + j = self._type_metadata_end(j, stop) + return self._type_metadata_end(j, stop) + + def _group_end(self, j: int, stop: int) -> int: + """At an opening bracket, the index after its match (or `stop`); else `j + 1`.""" + close = _PAIRS.get(self.toks[j].text) + if close is None: + return j + 1 + opener = self.toks[j].text + if opener == "{": + return min(self._closes.get(j, stop) + 1, stop) + depth = 0 + while j < stop: + t = self.toks[j].text + if t == opener: + depth += 1 + elif t == close: + depth -= 1 + if depth == 0: + return j + 1 + j += 1 + return stop + + def _field(self, j: int, stop: int) -> tuple[int, tuple[int, int] | None]: + """One field, `[1:] [required|optional] Type [&] name [= value] [xsd options] + [(annotations)]`, as in the IDL grammar, where the numeric ID is optional: record + its type and return the index after it plus an optional attributes range.""" + t = self.toks[j].text + if t.lstrip("+-").isdigit() and j + 1 < stop and self.toks[j + 1].text == ":": + j += 2 + if j < stop and self.toks[j].text in _FIELD_REQ: + j += 1 + j = self._type(j, stop) + if j < stop and self.toks[j].text == "&": + j += 1 + if j < stop and _is_name(self.toks[j].text): + j += 1 + if j < stop and self.toks[j].text == "=" and j + 1 < stop: + j = self._group_end(j + 1, stop) + while j < stop and self.toks[j].text in _XSD_FIELD_FLAGS: + j += 1 + attrs = None + if j + 1 < stop and self.toks[j].text == "xsd_attrs" and self.toks[j + 1].text == "{": + attrs_end = self._group_end(j + 1, stop) + attrs = (j + 2, attrs_end - 1) + j = attrs_end + if j < stop and self.toks[j].text == "(": + j = self._group_end(j, stop) + return j, attrs + + def _fields(self, j: int, stop: int) -> None: + """Field lists between `j` and `stop`, with or without numeric IDs and separators: + record each field's type.""" + pending = [(j, stop)] + while pending: + j, stop = pending.pop() + while j < stop: + if self.toks[j].text in ",;": + j += 1 + continue + after, attrs = self._field(j, stop) + if attrs is not None: + pending.append((after, stop)) + j, stop = attrs + else: + j = max(after, j + 1) + + def _block_end(self, open_idx: int) -> int: + j = self._closes.get(open_idx) + return j if j is not None else len(self.toks) - 1 + + def _service(self, start: int, name: str, open_idx: int) -> None: + close = self._block_end(open_idx) + self._add( + name, KIND_SERVICE, f"service {name}", self.toks[start].line, self.toks[close].line + ) + j = open_idx + 1 + while j < close: + # [oneway] ReturnType name ( args ) [throws ( … )] [, ;] + fstart = j + if self.toks[j].text == "oneway": + j += 1 + ret_end = self._type(j, close) + if ( + ret_end < close + and _is_name(self.toks[ret_end].text) + and ret_end + 1 < close + and self.toks[ret_end + 1].text == "(" + ): + fname = self.toks[ret_end].text + args_open = ret_end + 1 + k = self._group_end(args_open, close) - 1 + self._fields(args_open + 1, k) + end = k + if ( + k + 1 < close + and self.toks[k + 1].text == "throws" + and k + 2 < close + and self.toks[k + 2].text == "(" + ): + m = self._group_end(k + 2, close) - 1 + self._fields(k + 3, m) + end = m + if end + 1 < close and self.toks[end + 1].text == "(": + end = self._group_end(end + 1, close) - 1 + sig = _join(self.toks[fstart : end + 1]) + self._add( + fname, KIND_FUNCTION, sig, self.toks[fstart].line, self.toks[end].line, name + ) + j = end + 1 + else: + j = max(ret_end, j + 1) + self.i = close + 1 + + def parse(self) -> ThriftUnits: + toks = self.toks + n = len(toks) + while self.i < n: + t = toks[self.i].text + nxt = toks[self.i + 1].text if self.i + 1 < n else "" + if t in _STRUCT_KEYWORDS and _is_name(nxt): + start = self.i + j = self.i + 2 + if j < n and toks[j].text == "xsd_all": + j += 1 + if j < n and toks[j].text == "{": + close = self._block_end(j) + self._add(nxt, KIND_STRUCT, f"{t} {nxt}", toks[start].line, toks[close].line) + self._fields(j + 1, close) + self.i = close + 1 + continue + elif t == "include" and _is_literal(nxt): + # read from the tokens, so an include inside a comment is not one + self.units.includes.append(nxt[1:-1]) + self.i += 2 + continue + elif t == "enum" and _is_name(nxt): + start = self.i + j = self.i + 2 + if j < n and toks[j].text == "{": + close = self._block_end(j) + self._add(nxt, KIND_ENUM, f"enum {nxt}", toks[start].line, toks[close].line) + self.i = close + 1 + continue + elif t == "typedef": + start = self.i + j = self._type(self.i + 1, n) + if j < n and _is_name(toks[j].text): + self._add( + toks[j].text, + KIND_TYPEDEF, + " ".join(x.text for x in toks[start : j + 1]), + toks[start].line, + toks[j].line, + ) + self.i = j + 1 + continue + self.i = max(self.i + 1, j) + continue + elif t == "service" and _is_name(nxt): + start = self.i + j = self.i + 2 + if j + 1 < n and toks[j].text == "extends": + self._site(toks[j + 1].line, toks[j + 1].text, SITE_EXTENDS) + j += 2 + if j < n and toks[j].text == "{": + self._service(start, nxt, j) + continue + self.i += 1 + return self.units + + +def thrift_units(path: str, text: str) -> ThriftUnits: + units = _Parser(path, _tokens(text)).parse() + units.includes = list(dict.fromkeys(units.includes)) + return units + + +def parse_thrift(path: str, source: bytes) -> ParsedFile: + units = thrift_units(path, source.decode("utf-8", "replace")) + pf = ParsedFile(path=path, lang=LANG_THRIFT, source=source) + pf.symbols = units.symbols + pf.includes = units.includes + return pf + + +def reference_sites(pf: ParsedFile) -> list[tuple[int, str, str]]: + """(line, bare type name, `field_type` | `extends`) for every named type a field, + argument, return, `throws` entry or `extends` names, in source order.""" + return thrift_units(pf.path, pf.source.decode("utf-8", "replace")).sites + + +def wanted_paths(from_path: str, specs: list[str]) -> list[str]: + """Every repository path the includes may name, for one membership read.""" + out: dict[str, None] = {} + for spec in specs: + out.update(dict.fromkeys(ancestor_candidates(from_path, spec))) + return list(out) + + +def resolve_imports(from_path: str, specs: list[str], present: set[str]) -> list[str]: + """One indexed file per include: the nearest candidate `present` holds.""" + out: dict[str, None] = {} + for spec in specs: + hit = next((c for c in ancestor_candidates(from_path, spec) if c in present), None) + if hit and hit != from_path: + out[hit] = None + return list(out) + + +__all__ = [ + "BASE_TYPES", + "CHUNK_OPTIONS", + "KIND_ENUM", + "KIND_FUNCTION", + "KIND_SERVICE", + "KIND_STRUCT", + "KIND_TYPEDEF", + "LANG_THRIFT", + "SITE_EXTENDS", + "SITE_FIELD_TYPE", + "SITE_RULES", + "ThriftUnits", + "parse_thrift", + "reference_sites", + "resolve_imports", + "thrift_units", + "wanted_paths", +] diff --git a/server/code/domain/extract/name_scope.py b/server/code/domain/extract/name_scope.py index 107f2fefbd..78aab49756 100644 --- a/server/code/domain/extract/name_scope.py +++ b/server/code/domain/extract/name_scope.py @@ -67,6 +67,7 @@ "rust": NATIVE, # Protobuf and Go-template names are module-scoped `::Name` like Python's "proto": SCRIPT, + "thrift": SCRIPT, "gotemplate": SCRIPT, "hcl": SCRIPT, # Dotted directory scopes "k8s": SCRIPT, # Dotted file scopes diff --git a/server/code/domain/search_scope.py b/server/code/domain/search_scope.py index 548da6c71e..d2d53cba84 100644 --- a/server/code/domain/search_scope.py +++ b/server/code/domain/search_scope.py @@ -56,6 +56,7 @@ "json": (".json",), "toml": (".toml",), "proto": (".proto",), + "thrift": (".thrift",), "cmake": (".cmake", "CMakeLists.txt"), "xml": (".xml",), "html": (".html", ".htm"), diff --git a/tests/code_lanes/corpus.py b/tests/code_lanes/corpus.py index c2170a1cce..901a0c7c61 100644 --- a/tests/code_lanes/corpus.py +++ b/tests/code_lanes/corpus.py @@ -154,6 +154,12 @@ def langs_for(self, path: str) -> frozenset[str]: ), Lane("prisma/", "prisma", "models, relations, an enum and attributes of the vendored schema"), Lane("proto/", "proto", "a package, an import, messages, an enum, a service and its RPCs"), + Lane( + "thrift/", + "thrift", + "an include, a typedef, an enum, a struct, a union, an exception, a service " + "extending an included one, and its functions", + ), Lane( "python/", "python", @@ -432,7 +438,7 @@ def _summarize() -> dict[str, Any]: pf.includes.append(hit) elif pf.lang == "rust": pf.includes = resolve_uses(rel, pf.includes, present) - elif pf.lang in ("proto", "gotemplate", "lua"): + elif pf.lang in ("proto", "thrift", "gotemplate", "lua"): # Import candidates settle against the corpus paths lane = _lane_module(pf.lang) pf.includes = lane.resolve_imports(rel, list(pf.includes), present) @@ -559,7 +565,7 @@ def _summarize() -> dict[str, Any]: ] if pf.lang == "prisma": es = [*es, *prisma_edges(pf)] - if pf.lang in ("proto", "gotemplate", "lua", "graphql"): + if pf.lang in ("proto", "thrift", "gotemplate", "lua", "graphql"): # The reference sites over the lane's table lane = _lane_module(pf.lang) es = [*es, *bind_sites(pf, lane.reference_sites(pf), gated, lane.SITE_RULES)] @@ -586,6 +592,7 @@ def _summarize() -> dict[str, Any]: if pf.lang in ts_langs or pf.lang in ( "rust", "proto", + "thrift", "gotemplate", "lua", "config", @@ -602,10 +609,18 @@ def _summarize() -> dict[str, Any]: def _lane_module(lang: str) -> Any: """The tree-less lane modules, as `ingest._lane_module`.""" - from server.code.domain.extract import lang_gotemplate, lang_graphql, lang_lua, lang_proto + from server.code.domain.extract import ( + lang_gotemplate, + lang_graphql, + lang_lua, + lang_proto, + lang_thrift, + ) if lang == "lua": return lang_lua + if lang == "thrift": + return lang_thrift if lang == "graphql": return lang_graphql return lang_proto if lang == "proto" else lang_gotemplate diff --git a/tests/fixtures/code_lanes/NOTICE.md b/tests/fixtures/code_lanes/NOTICE.md index 64d0426033..35d6575ee1 100644 --- a/tests/fixtures/code_lanes/NOTICE.md +++ b/tests/fixtures/code_lanes/NOTICE.md @@ -29,7 +29,7 @@ bats test, a Jinja template, a platform-suffixed Go file), so don't reformat or | java/ | synthetic: a Spring / JDBC repository, a generic interface and a generic class implementing it | — | | js/, ts/constructs/, ts/synthetic/ | synthetic: CommonJS, IIFE, class expressions, overloads, React components, SQL in template literals | — | | vue/ | synthetic single-file components and a composable | — | -| proto/, helm/ | synthetic | — | +| proto/, thrift/, helm/ | synthetic | — | | lua/ | synthetic: a vector module and a menu scene that requires it | — | | rust/synthetic/ | synthetic: a trait and its impl, a declarative macro, an async function | — | | sql/views/ | synthetic: a ClickHouse table, a materialized view into its rollup, a view and a CTE | — | diff --git a/tests/fixtures/code_lanes/expected.json b/tests/fixtures/code_lanes/expected.json index 2d53e293e0..674689790e 100644 --- a/tests/fixtures/code_lanes/expected.json +++ b/tests/fixtures/code_lanes/expected.json @@ -123,6 +123,11 @@ "sql/views/rollup.sql | calls | column": 3, "sql/views/rollup.sql | calls | table": 4, "sql/views/rollup.sql | calls | view": 1, + "thrift/acme/machines.thrift | includes | file": 1, + "thrift/acme/machines.thrift | inherits | interface": 1, + "thrift/acme/machines.thrift | uses_type | enum": 2, + "thrift/acme/machines.thrift | uses_type | struct": 7, + "thrift/acme/machines.thrift | uses_type | type": 6, "tla/Ledger.cfg | calls | operator": 3, "tla/Ledger.cfg | includes | file": 1, "tla/Ledger.tla | calls | constant": 3, @@ -1715,6 +1720,31 @@ "view": 2 } }, + "thrift/acme/machines.thrift": { + "facets": {}, + "includes": [ + "thrift/acme/shared.thrift" + ], + "lang": "thrift", + "symbols": { + "enum": 1, + "function": 4, + "interface": 1, + "struct": 3, + "type": 1 + } + }, + "thrift/acme/shared.thrift": { + "facets": {}, + "includes": [], + "lang": "thrift", + "symbols": { + "function": 1, + "interface": 1, + "struct": 2, + "type": 1 + } + }, "tla/Ledger.cfg": { "facets": { "checks": 3 diff --git a/tests/fixtures/code_lanes/thrift/acme/machines.thrift b/tests/fixtures/code_lanes/thrift/acme/machines.thrift new file mode 100644 index 0000000000..da359c698f --- /dev/null +++ b/tests/fixtures/code_lanes/thrift/acme/machines.thrift @@ -0,0 +1,37 @@ +namespace py acme.machines +namespace java com.acme.machines + +include "shared.thrift" + +typedef string MachineId + +enum State { + PENDING = 1, + RUNNING = 2, + STOPPED = 3 +} + +/* A machine as the API returns it */ +struct Machine { + 1: required MachineId id, + 2: optional State state = State.PENDING, + 3: shared.Timestamp created_at, + 4: map labels +} + +union Selector { + 1: MachineId id, + 2: list label_keys +} + +exception InvalidState { + 1: State current, + 2: string reason +} + +service Machines extends shared.BaseService { + Machine get(1: MachineId id) throws (1: shared.NotFound not_found), + list list_machines(1: shared.Page page, 2: Selector selector), + void stop(1: MachineId id) throws (1: shared.NotFound not_found, 2: InvalidState invalid), + oneway void touch(1: MachineId id) +} diff --git a/tests/fixtures/code_lanes/thrift/acme/shared.thrift b/tests/fixtures/code_lanes/thrift/acme/shared.thrift new file mode 100644 index 0000000000..1f8d29e533 --- /dev/null +++ b/tests/fixtures/code_lanes/thrift/acme/shared.thrift @@ -0,0 +1,17 @@ +namespace py acme.shared + +// Types every acme service shares +typedef i64 Timestamp + +struct Page { + 1: required i32 offset, + 2: required i32 limit +} + +exception NotFound { + 1: string message +} + +service BaseService { + string version() +} diff --git a/tests/inventory/build.py b/tests/inventory/build.py index da6f79b0f6..ddf4b13e43 100644 --- a/tests/inventory/build.py +++ b/tests/inventory/build.py @@ -87,6 +87,7 @@ "rust", "shell", "sql", + "thrift", "tla", "ts", "vue", diff --git a/tests/unit/lane_walks.py b/tests/unit/lane_walks.py index c0d1b67fa9..a9d07464ba 100644 --- a/tests/unit/lane_walks.py +++ b/tests/unit/lane_walks.py @@ -324,6 +324,10 @@ def walk(text: str) -> object: return lang_rust.wanted_paths("src/a/b.rs", specs) if lane == "go": return lang_go.go_wanted_paths(specs, [("m", "")]) + if lane == "thrift": + from server.code.domain.extract import lang_thrift + + return lang_thrift.wanted_paths("a/b/c.thrift", specs) return lang_proto.wanted_paths("a/b/c.proto", specs) return walk @@ -431,6 +435,7 @@ def _inline_templates(n: int) -> str: ("rust-long-use", _wanted("rust"), _run("", "a::")), ("go-imports", _wanted("go"), _numbered("m/p{}\n")), ("proto-imports", _wanted("proto"), _numbered("p{}.proto\n")), + ("thrift-includes", _wanted("thrift"), _numbered("t{}.thrift\n")), ("python-route-rows", _py_routes, _run("", "app.add_url_rule('a')\n")), ("python-unclosed-include-router", _py_routes, _run("", "a.include_router(a,")), ( @@ -539,6 +544,7 @@ def cost(n: int) -> float: "astro": ("a.astro", "", "typescript"), "angular_template": ("a.component.html", "", "typescript"), "proto": ("a.proto", "", "proto"), + "thrift": ("a.thrift", "", "thrift"), "gotemplate": ("chart/templates/a.yaml", "{{ .Values.a }}\n", "gotemplate"), "hcl": ("a.tf", "", "hcl"), "k8s": ("a.yaml", "apiVersion: v1\nkind: ConfigMap\n", "k8s"), diff --git a/tests/unit/test_code_extractor_stamp.py b/tests/unit/test_code_extractor_stamp.py index 727c8d1284..3b5a4efa7c 100644 --- a/tests/unit/test_code_extractor_stamp.py +++ b/tests/unit/test_code_extractor_stamp.py @@ -85,6 +85,10 @@ "protobuf scanner: messages, enums, services, rpcs, type sites and " "import candidates; applied by a targeted backfill over a namespace's .proto files" ), + "lang_thrift.py": ( + "Thrift IDL scanner: structs, enums, typedefs, services, functions, type sites " + "and include candidates — a lane module outside the stamp like lang_proto.py" + ), "lang_jsonnet.py": ( "jsonnet bindings, members and imports — a lane module outside the " "stamp like lang_proto.py; rows arrive by a targeted backfill" diff --git a/tests/unit/test_code_lanes_manifest.py b/tests/unit/test_code_lanes_manifest.py index 0bae78c2a8..a17ec1546f 100644 --- a/tests/unit/test_code_lanes_manifest.py +++ b/tests/unit/test_code_lanes_manifest.py @@ -23,7 +23,7 @@ # The languages whose summary row records includes resolved against the # corpus's own paths; the others record the specifier as written. RESOLVED_INCLUDE_LANGS = frozenset( - {"typescript", "javascript", "rust", "proto", "gotemplate", "python"} + {"typescript", "javascript", "rust", "proto", "thrift", "gotemplate", "python"} ) _EDGE_KEY = re.compile(r"^(?P.+) \| (?P[a-z_]+) \| (?P[a-z_?]+)$") _SECRET = re.compile( diff --git a/tests/unit/test_code_lang_thrift.py b/tests/unit/test_code_lang_thrift.py new file mode 100644 index 0000000000..46ec7c6d41 --- /dev/null +++ b/tests/unit/test_code_lang_thrift.py @@ -0,0 +1,370 @@ +"""The Thrift IDL lane: symbols, reference sites, includes and registration.""" + +from __future__ import annotations + +import time +from unittest.mock import AsyncMock, Mock + +import pytest + +from server.code.app.index.ingest_parse import LANE_UNIT_LANGS, _lane_module +from server.code.domain.extract import lang_thrift as th +from server.code.domain.extract.lane_units import bind_sites +from server.code.domain.extract.lang_detect import detect_by_extension +from server.code.domain.extract.name_scope import gate_name_table +from tests.unit.code_fixtures import assert_linear + +SRC = b"""namespace py acme +include "shared.thrift" +include "shared.thrift" + +typedef i64 UserId // trailing comment +enum Color { RED = 1, BLUE = 2 } + +/* struct Ghost { } inside a comment is not a symbol */ +struct User { + 1: required UserId id, + 2: optional list colors, + 3: map addresses, +} + +union Key { 1: UserId id, 2: string email } +exception NotFound { 1: string msg } + +service Users extends shared.Base { + User get(1: UserId id) throws (1: NotFound nf), + oneway void ping(); + set all() +} +""" + + +def _units() -> th.ThriftUnits: + return th.thrift_units("idl/users.thrift", SRC.decode()) + + +def test_symbols_kinds_and_names() -> None: + got = {(s.kind, s.qualified_name) for s in _units().symbols} + assert got == { + ("type", "idl.users::UserId"), + ("enum", "idl.users::Color"), + ("struct", "idl.users::User"), + ("struct", "idl.users::Key"), + ("struct", "idl.users::NotFound"), + ("interface", "idl.users::Users"), + ("function", "idl.users::Users.get"), + ("function", "idl.users::Users.ping"), + ("function", "idl.users::Users.all"), + } + + +def test_spans_and_signatures() -> None: + by = {s.name: s for s in _units().symbols} + assert (by["User"].start_line, by["User"].end_line) == (9, 13) + assert (by["Users"].start_line, by["Users"].end_line) == (18, 22) + assert by["get"].signature == "User get(1: UserId id) throws(1: NotFound nf)" + assert by["ping"].signature == "oneway void ping()" + assert by["all"].signature == "set all()" + assert by["NotFound"].signature == "exception NotFound" + assert by["UserId"].signature == "typedef i64 UserId" + + +def test_reference_sites_read_through_containers_and_includes() -> None: + sites = _units().sites + assert (10, "UserId", "field_type") in sites + assert (11, "Color", "field_type") in sites + assert (12, "Address", "field_type") in sites # `shared.Address` binds by its last part + assert (18, "Base", "extends") in sites + assert {(19, "User", "field_type"), (19, "NotFound", "field_type")} <= set(sites) + assert (21, "User", "field_type") in sites + names = {n for _, n, _ in sites} + assert not names & th.BASE_TYPES + assert "Ghost" not in {s.name for s in _units().symbols} + + +def test_includes_are_deduplicated_and_resolved_nearest_first() -> None: + assert _units().includes == ["shared.thrift"] + present = {"idl/shared.thrift", "shared.thrift"} + assert th.resolve_imports("idl/users.thrift", ["shared.thrift"], present) == [ + "idl/shared.thrift" + ] + assert "idl/shared.thrift" in th.wanted_paths("idl/users.thrift", ["shared.thrift"]) + + +def test_includes_inside_comments_are_not_includes() -> None: + src = b'/*\ninclude "unused.thrift"\n*/\nstruct Live { 1: i32 x }\n' + assert th.parse_thrift("idl/main.thrift", src).includes == [] + text = ( + '// include "a.thrift"\n# include "b.thrift"\ninclude "c.thrift" /* include "d.thrift" */\n' + ) + assert th.thrift_units("m.thrift", text).includes == ["c.thrift"] + # an unclosed string is not a file name + assert th.thrift_units("m.thrift", 'include "x').includes == [] + + +def _uses_type(path: str, src: bytes) -> set[tuple[str, str]]: + pf = th.parse_thrift(path, src) + edges = bind_sites(pf, th.reference_sites(pf), gate_name_table({}, "thrift"), th.SITE_RULES) + return { + (e.from_qualified_name.split("::")[1], e.to_qualified_name.split("::")[1]) + for e in edges + if e.edge_kind == "uses_type" + } + + +def test_fields_without_a_numeric_id_record_their_types() -> None: + # the IDL grammar makes `FieldID ':'` optional + src = b"struct User { 1: string id }\nstruct Envelope { optional User user }\n" + assert (2, "User", "field_type") in th.thrift_units("idl/e.thrift", src.decode()).sites + assert ("Envelope", "User") in _uses_type("idl/e.thrift", src) + with_id = b"struct User { 1: string id }\nstruct Envelope { 1: optional User user }\n" + assert ("Envelope", "User") in _uses_type("idl/e.thrift", with_id) + + +def test_id_less_fields_in_arguments_throws_and_mixed_lists() -> None: + src = b"""struct Id { 1: i64 v } +struct Tag { 1: string t } +exception Missing { 1: string why } +struct Mixed { + required Id id = {"v": 1} + list (python.immutable = "") tags; + 3: Tag& primary xsd_optional (doc = "x"), + map byName +} +service Lookup { + Mixed find(Id id, 2: Tag tag) throws (Missing missing) +} +""" + sites = th.thrift_units("idl/l.thrift", src.decode()).sites + assert { + (5, "Id", "field_type"), + (6, "Tag", "field_type"), + (7, "Tag", "field_type"), + (8, "Id", "field_type"), + } <= set(sites) + got = _uses_type("idl/l.thrift", src) + assert {("Mixed", "Id"), ("Mixed", "Tag")} <= got + assert {("Lookup.find", "Id"), ("Lookup.find", "Tag"), ("Lookup.find", "Missing")} <= got + # field names, default values and annotations are not types + names = {n for _, n, _ in sites} + assert not names & {"id", "tags", "primary", "byName", "v", "python", "doc", "x"} + + +def test_unterminated_input_does_not_raise() -> None: + for text in ("struct A {", "service S { void f(", "typedef list<", 'include "x', "/* open"): + th.thrift_units("a.thrift", text) + + +def test_registered_as_a_lane() -> None: + assert detect_by_extension("api/users.thrift") == "thrift" + assert "thrift" in LANE_UNIT_LANGS + assert _lane_module("thrift") is th + pf = th.parse_thrift("a.thrift", SRC) + assert pf.lang == "thrift" and pf.tree is None and pf.includes == ["shared.thrift"] + + +def test_edges_bind_same_file_then_repo_wide_and_builtins_to_nothing() -> None: + pf = th.parse_thrift("idl/users.thrift", SRC) + table = { + "Address": [("sid1", "idl.shared::Address", "struct")], + "Base": [("sid2", "idl.shared::Base", "interface")], + "Ghost": [("sid3", "a::Ghost", "struct"), ("sid4", "b::Ghost", "struct")], + } + edges = bind_sites(pf, th.reference_sites(pf), gate_name_table(table, "thrift"), th.SITE_RULES) + got = { + (e.from_qualified_name.split("::")[1], e.to_qualified_name.split("::")[1], e.edge_kind) + for e in edges + } + assert ("User", "UserId", "uses_type") in got # same file + assert ("User", "Color", "uses_type") in got # inside list<> + assert ("User", "Address", "uses_type") in got # included, repository-unique + assert ("Users", "Base", "inherits") in got + assert ("Users.get", "NotFound", "uses_type") in got # throws + assert ("Users.all", "User", "uses_type") in got # return inside set<> + assert not any(t in th.BASE_TYPES for _f, t, _k in got) + + +def _cost(n: int) -> float: + text = "".join( + f"struct S{i} {{ 1: list a }}\nservice V{i} {{ S{i} f(1: S{i} x) }}\n" + for i in range(n) + ) + best = float("inf") + for _ in range(3): + t = time.perf_counter() + th.thrift_units("a.thrift", text) + best = min(best, time.perf_counter() - t) + return best + + +def test_a_file_of_many_definitions_parses_in_linear_time() -> None: + assert_linear(_cost, 1_000, 4_000) + + +def test_unclosed_headers_repeated_cost_linear_time() -> None: + def cost(n: int) -> float: + text = "typedef list< struct A service S extends " * n + t = time.perf_counter() + th.thrift_units("a.thrift", text) + return time.perf_counter() - t + + assert_linear(cost, 2_000, 8_000) + + +async def test_root_file_types_bind_through_the_production_name_gate() -> None: + from server.code.app.index.ingest_edges import _lane_site_edges + + shared = th.parse_thrift("shared.thrift", b"struct Payload {}\nservice Base {}\n") + caller = th.parse_thrift( + "main.thrift", + b'include "shared.thrift"\nservice Child extends shared.Base {\n' + b" shared.Payload get()\n}\n", + ) + table = { + symbol.name: [(symbol.qualified_name, symbol.qualified_name, symbol.kind)] + for symbol in shared.symbols + } + table["Base"].append(("native-base", "Native::Base", "interface")) + table["Payload"].append(("native-payload", "Native::Payload", "struct")) + repo = Mock(name_candidates=AsyncMock(return_value=table)) + edges = await _lane_site_edges(repo, "o", "n", caller) + assert { + (edge.from_qualified_name, edge.to_qualified_name, edge.edge_kind, edge.confidence) + for edge in edges + } == { + ("main.thrift::Child", "shared.thrift::Base", "inherits", 0.6), + ("main.thrift::Child.get", "shared.thrift::Payload", "uses_type", 0.6), + } + repo.name_candidates.assert_awaited_once_with("o", "n", ["Base", "Payload"]) + + +def test_function_annotations_do_not_become_type_sites() -> None: + src = b"""struct Audit { 1: string value } +service S { + void ping() + (Audit = "metadata") + Audit fetch() +} +""" + assert th.thrift_units("idl/main.thrift", src.decode()).sites == [(5, "Audit", "field_type")] + assert _uses_type("idl/main.thrift", src) == {("S.fetch", "Audit")} + + +@pytest.mark.parametrize( + "default", ["+1", "-1", "+1.5e+2", "-1.5e-2", ".5", "+.5", "-.5", "+.5e-2"] +) +@pytest.mark.parametrize("field_id", ["", "+1: "]) +def test_signed_defaults_preserve_the_following_id_less_field(default: str, field_id: str) -> None: + src = ( + "struct Item { 1: string value }\n" + f"struct Box {{\n {field_id}double scale = {default}\n Item item\n}}\n" + ).encode() + assert _uses_type("idl/main.thrift", src) == {("Box", "Item")} + + +def _nested_attrs(n: int) -> str: + return ( + "struct Leaf {}\nstruct A {\n" + + "1: A a xsd_attrs {\n" * n + + "1: Leaf leaf\n" + + "}\n" * n + + "2: Leaf tail\n}\n" + ) + + +def test_nested_xsd_attrs_preserve_source_order_without_recursion() -> None: + depth = 1_500 + units = th.thrift_units("idl/main.thrift", _nested_attrs(depth)) + assert [symbol.name for symbol in units.symbols] == ["Leaf", "A"] + assert units.sites == [ + *((line, "A", "field_type") for line in range(3, depth + 3)), + (depth + 3, "Leaf", "field_type"), + (depth * 2 + 4, "Leaf", "field_type"), + ] + + +def test_nested_xsd_attrs_cost_linear_time() -> None: + def cost(n: int) -> float: + text = _nested_attrs(n) + started = time.perf_counter() + th.thrift_units("idl/main.thrift", text) + return time.perf_counter() - started + + assert_linear(cost, 500, 2_000) + + +def test_nested_type_annotations_are_not_type_sites() -> None: + src = b"""struct Audit {} +struct Item {} +struct Box { + 1: list names + 2: map (Audit = "metadata")> values +} +""" + assert th.thrift_units("idl/main.thrift", src.decode()).sites == [(5, "Item", "field_type")] + assert _uses_type("idl/main.thrift", src) == {("Box", "Item")} + + +def test_an_annotated_alias_keeps_its_symbol_and_type_edge() -> None: + src = b"""struct Audit {} +struct Meta {} +typedef list (Meta = "metadata") AuditList +""" + units = th.thrift_units("idl/main.thrift", src.decode()) + alias = next(symbol for symbol in units.symbols if symbol.name == "AuditList") + assert (alias.kind, alias.start_line, alias.end_line) == ("type", 3, 3) + assert units.sites == [(3, "Audit", "field_type")] + assert _uses_type("idl/main.thrift", src) == {("AuditList", "Audit")} + + +def test_an_annotated_return_keeps_its_signature_span_and_source_edge() -> None: + src = b"""struct Audit {} +struct Meta {} +service S { + list + (Meta = "metadata") + fetch() + string (Meta = "metadata") ping() + Audit following() +} +""" + units = th.thrift_units("idl/main.thrift", src.decode()) + by_name = {symbol.name: symbol for symbol in units.symbols} + fetch = by_name["fetch"] + assert (fetch.start_line, fetch.end_line) == (4, 6) + assert fetch.signature == 'list(Meta = "metadata") fetch()' + assert by_name["ping"].signature == 'string(Meta = "metadata") ping()' + assert units.sites == [(4, "Audit", "field_type"), (8, "Audit", "field_type")] + assert _uses_type("idl/main.thrift", src) == {("S.fetch", "Audit"), ("S.following", "Audit")} + + +def test_native_type_hints_do_not_hide_nested_types_or_create_sites() -> None: + src = b"""struct Audit {} +struct Item {} +typedef list cpp_type "vector" Items +struct Box { + 1: map cpp_type "Map" > entries +} +service S { + list cpp_type "vector" fetch() +} +""" + units = th.thrift_units("idl/main.thrift", src.decode()) + assert [symbol.name for symbol in units.symbols] == [ + "Audit", + "Item", + "Items", + "Box", + "S", + "fetch", + ] + assert units.sites == [ + (3, "Item", "field_type"), + (5, "Item", "field_type"), + (8, "Item", "field_type"), + ] + assert _uses_type("idl/main.thrift", src) == { + ("Items", "Item"), + ("Box", "Item"), + ("S.fetch", "Item"), + } diff --git a/tests/unit/test_lang_text.py b/tests/unit/test_lang_text.py index 5dd4051703..69677ac9a0 100644 --- a/tests/unit/test_lang_text.py +++ b/tests/unit/test_lang_text.py @@ -28,7 +28,6 @@ def test_code_without_a_parser_and_plain_text_are_kept_as_text() -> None: "notes.txt": b"hello", "setup.cfg": b"[flake8]\nmax-line-length = 100\n", "gradle/build.gradle": b"plugins {}", - "api/service.thrift": b"service Q { i32 a() }", "LICENSE": b"MIT", "Justfile": b"build:\n cargo build", }.items(): @@ -37,6 +36,14 @@ def test_code_without_a_parser_and_plain_text_are_kept_as_text() -> None: assert pf is not None and pf.lang == TEXT_LANG and pf.symbols == [] and err == "", path +def test_thrift_goes_to_its_lane_not_to_text() -> None: + src = b"service Q { i32 a() }" + assert text_lang_for_path("api/service.thrift", src) == "" + pf, err = parse_for_ingest("api/service.thrift", src) + assert pf is not None and pf.lang == "thrift" and err == "" + assert {(s.kind, s.name) for s in pf.symbols} >= {("interface", "Q"), ("function", "a")} + + def test_binaries_oversized_and_empty_stay_out_and_claimed_types_are_untouched() -> None: assert text_lang_for_path("img/a.png", b"\x89PNG") == "" assert text_lang_for_path("dist/app.min.js", b"x") == ""