rbtr-lang-python 2026.7.0.dev0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,54 @@
1
+ """Python docstring extraction.
2
+
3
+ rbtr folds a symbol's docstring into its chunk content. Python is an
4
+ interior-doc language — the docstring is captured by the `@_docstring` query
5
+ capture, orthogonal to the leading-comment sibling walk — so suppressing that
6
+ walk leaves the doc (and `line_start`) unchanged. Data lives in
7
+ `cases_docstrings.py`, sliced by tag.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ from pytest_cases import parametrize_with_cases
13
+
14
+ from rbtr.git import FileEntry
15
+ from rbtr.languages.extract import extract_file
16
+
17
+
18
+ @parametrize_with_cases(
19
+ "lang, source, name, snippet", cases=".cases_docstrings", has_tag="documented"
20
+ )
21
+ def test_documented_chunk_includes_doc_text(
22
+ lang: str, source: str, name: str, snippet: str
23
+ ) -> None:
24
+ """By default the chunk content carries the symbol's docs."""
25
+ chunks = extract_file(FileEntry("input", "sha1", source.encode()), lang)
26
+ chunk = next(c for c in chunks if c.name == name)
27
+ assert snippet in chunk.content, (
28
+ f"expected {snippet!r} in {lang}.{name} content; got:\n{chunk.content!r}"
29
+ )
30
+
31
+
32
+ @parametrize_with_cases(
33
+ "lang, source, name, snippet", cases=".cases_docstrings", has_tag="undocumented"
34
+ )
35
+ def test_no_phantom_documentation(lang: str, source: str, name: str, snippet: str) -> None:
36
+ """Symbols without documentation do not gain any in content."""
37
+ chunks = extract_file(FileEntry("input", "sha1", source.encode()), lang)
38
+ chunk = next(c for c in chunks if c.name == name)
39
+ assert snippet not in chunk.content, (
40
+ f"unexpected {snippet!r} in {lang}.{name} content; got:\n{chunk.content!r}"
41
+ )
42
+
43
+
44
+ @parametrize_with_cases(
45
+ "lang, source, name, snippet", cases=".cases_docstrings", has_tag="interior_doc"
46
+ )
47
+ def test_interior_docstring_folds_into_content(
48
+ lang: str, source: str, name: str, snippet: str
49
+ ) -> None:
50
+ """An interior (`@_docstring`) doc is part of its symbol's chunk content."""
51
+ chunk = next(
52
+ c for c in extract_file(FileEntry("input", "sha1", source.encode()), lang) if c.name == name
53
+ )
54
+ assert snippet in chunk.content
@@ -0,0 +1,110 @@
1
+ """Python extraction tests.
2
+
3
+ Symbol, import, and mixed cases (`cases_extraction.py`) drive the shared
4
+ checks; the functions below pin Python's module-variable, function-local,
5
+ class-attribute, and tuple-unpacking edge behaviour.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from pytest_cases import parametrize_with_cases
11
+
12
+ from rbtr.git import FileEntry
13
+ from rbtr.index.models import ChunkKind, ImportMeta
14
+ from rbtr.languages.extract import extract_file
15
+
16
+
17
+ @parametrize_with_cases("lang, source, expected", cases=".cases_extraction", has_tag="symbol")
18
+ def test_extracts_expected_symbols(lang: str, source: str, expected: list) -> None:
19
+ """Each expected (kind, name, scope) tuple appears in the output."""
20
+ chunks = extract_file(FileEntry("input", "sha1", source.encode()), lang)
21
+ symbols = [(c.kind, c.name, c.scope) for c in chunks]
22
+ for exp in expected:
23
+ assert exp in symbols, f"expected {exp} not found in {symbols}"
24
+
25
+
26
+ @parametrize_with_cases(
27
+ "lang, source, expected_kinds, expected_methods", cases=".cases_extraction", has_tag="mixed"
28
+ )
29
+ def test_extracts_all_expected_kinds(
30
+ lang: str, source: str, expected_kinds: set[str], expected_methods: list[tuple[str, str]]
31
+ ) -> None:
32
+ """Realistic source produces all expected chunk kinds and method scoping."""
33
+ chunks = extract_file(FileEntry("input", "sha1", source.encode()), lang)
34
+ kinds = {c.kind for c in chunks}
35
+ for kind in expected_kinds:
36
+ assert kind in kinds, f"expected kind {kind!r} not in {kinds}"
37
+ methods = [(c.name, c.scope) for c in chunks if c.kind == ChunkKind.METHOD]
38
+ for name, scope in expected_methods:
39
+ assert (name, scope) in methods, f"expected method ({name}, {scope}) not in {methods}"
40
+
41
+
42
+ @parametrize_with_cases("lang, source, expected", cases=".cases_extraction", has_tag="import")
43
+ def test_extracts_import_metadata(lang: str, source: str, expected: dict) -> None:
44
+ """First import chunk has the expected metadata."""
45
+ chunks = extract_file(FileEntry("input", "sha1", source.encode()), lang)
46
+ imports = [c for c in chunks if c.kind == ChunkKind.IMPORT]
47
+ assert len(imports) >= 1, f"no import chunks extracted from {source!r}"
48
+ assert imports[0].metadata == ImportMeta(**expected)
49
+
50
+
51
+ @parametrize_with_cases(
52
+ "lang, source, count, metadata_list", cases=".cases_extraction", has_tag="multi_import"
53
+ )
54
+ def test_extracts_multi_import(
55
+ lang: str, source: str, count: int, metadata_list: list[dict]
56
+ ) -> None:
57
+ """Multiple imports have correct count and per-import metadata."""
58
+ chunks = extract_file(FileEntry("input", "sha1", source.encode()), lang)
59
+ imports = [c for c in chunks if c.kind == ChunkKind.IMPORT]
60
+ assert len(imports) == count
61
+ for imp, expected in zip(imports, metadata_list, strict=True):
62
+ assert imp.metadata == ImportMeta(**expected)
63
+
64
+
65
+ def test_py_module_variable_content_is_whole_statement() -> None:
66
+ """A module-level VARIABLE chunk spans the whole statement, named by LHS."""
67
+ src = """\
68
+ MAX_SIZE = 100
69
+ """
70
+ chunks = extract_file(FileEntry("input", "sha1", src.encode()), "python")
71
+ variables = [c for c in chunks if c.kind == ChunkKind.VARIABLE]
72
+ assert len(variables) == 1
73
+ assert variables[0].name == "MAX_SIZE"
74
+ assert variables[0].content.strip() == "MAX_SIZE = 100"
75
+
76
+
77
+ def test_py_function_local_not_captured_as_variable() -> None:
78
+ """Assignments inside a function stay part of the function chunk."""
79
+ src = """\
80
+ def f():
81
+ tmp = 1
82
+ return tmp
83
+ """
84
+ chunks = extract_file(FileEntry("input", "sha1", src.encode()), "python")
85
+ assert [c for c in chunks if c.kind == ChunkKind.VARIABLE] == []
86
+
87
+
88
+ def test_py_class_attribute_not_captured_as_variable() -> None:
89
+ """Class-body attributes stay part of the class chunk, not VARIABLE chunks."""
90
+ src = """\
91
+ class Config:
92
+ DEFAULT = 30
93
+ """
94
+ chunks = extract_file(FileEntry("input", "sha1", src.encode()), "python")
95
+ assert [c for c in chunks if c.kind == ChunkKind.VARIABLE] == []
96
+
97
+
98
+ def test_py_tuple_unpacking_captured_as_variables() -> None:
99
+ """Flat tuple-unpacking binds each target as its own VARIABLE chunk.
100
+
101
+ Both names come from one statement (tree-sitter fans the destructuring
102
+ into a match per identifier), and each chunk spans the whole statement.
103
+ """
104
+ src = """\
105
+ a, b = compute()
106
+ """
107
+ chunks = extract_file(FileEntry("input", "sha1", src.encode()), "python")
108
+ variables = [c for c in chunks if c.kind == ChunkKind.VARIABLE]
109
+ assert {c.name for c in variables} == {"a", "b"}
110
+ assert all(c.content.strip() == "a, b = compute()" for c in variables)
@@ -0,0 +1,83 @@
1
+ """Python sample extraction: the `samples/python/` project through the real pipeline.
2
+
3
+ The snapshots are the golden record of what Python extraction produces.
4
+ Engine-wide invariants are covered once in core.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ from pathlib import Path
10
+ from typing import TYPE_CHECKING
11
+
12
+ import pytest
13
+ from tree_sitter import Parser
14
+
15
+ from rbtr.git import FileEntry
16
+ from rbtr.index.models import Chunk, ChunkKind, Edge
17
+ from rbtr.languages.edges import build_resolution_map, infer_import_edges
18
+ from rbtr.languages.extract import extract_file
19
+ from rbtr.languages.manager import get_manager
20
+ from rbtr.testing import render_edges
21
+
22
+ if TYPE_CHECKING:
23
+ from syrupy.assertion import SnapshotAssertion
24
+
25
+
26
+ @pytest.fixture
27
+ def project() -> list[tuple[str, str]]:
28
+ """The `(relative path, text)` files of the `samples/python/` project."""
29
+ root = Path(__file__).parent / "samples" / "python"
30
+ return [
31
+ (str(p.relative_to(root)), p.read_text()) for p in sorted(root.rglob("*")) if p.is_file()
32
+ ]
33
+
34
+
35
+ @pytest.fixture
36
+ def chunks(project: list[tuple[str, str]]) -> list[Chunk]:
37
+ """Chunks from every project file, each via the real `extract_file`."""
38
+ manager = get_manager()
39
+ out: list[Chunk] = []
40
+ for path, text in project:
41
+ lang = manager.detect_language(path) or "python"
42
+ out.extend(extract_file(FileEntry(path, "sha1", text.encode()), lang))
43
+ return out
44
+
45
+
46
+ @pytest.fixture
47
+ def edges(project: list[tuple[str, str]], chunks: list[Chunk]) -> list[Edge]:
48
+ """Import edges inferred across the project's files."""
49
+ manager = get_manager()
50
+ repo_files = {path for path, _ in project}
51
+ return infer_import_edges(chunks, repo_files, build_resolution_map(manager))
52
+
53
+
54
+ def test_emits_expected_kinds(chunks: list[Chunk]) -> None:
55
+ """The sample exercises Python's function, class, method, variable, and import chunks."""
56
+ kinds = {c.kind for c in chunks}
57
+ assert {
58
+ ChunkKind.FUNCTION,
59
+ ChunkKind.CLASS,
60
+ ChunkKind.METHOD,
61
+ ChunkKind.VARIABLE,
62
+ ChunkKind.IMPORT,
63
+ ChunkKind.COMMENT,
64
+ } <= kinds
65
+
66
+
67
+ def test_parses_cleanly(project: list[tuple[str, str]]) -> None:
68
+ """Every project file is valid source — no tree-sitter ERROR/MISSING nodes."""
69
+ manager = get_manager()
70
+ for path, text in project:
71
+ grammar = manager.grammar(manager.detect_language(path) or "python")
72
+ assert grammar is not None
73
+ assert not Parser(grammar).parse(text.encode()).root_node.has_error, path
74
+
75
+
76
+ def test_extraction_matches_snapshot(chunks: list[Chunk], snapshot_json: SnapshotAssertion) -> None:
77
+ assert chunks == snapshot_json
78
+
79
+
80
+ def test_edges_match_snapshot(
81
+ chunks: list[Chunk], edges: list[Edge], snapshot_json: SnapshotAssertion
82
+ ) -> None:
83
+ assert render_edges(edges, chunks) == snapshot_json
@@ -0,0 +1,8 @@
1
+ Metadata-Version: 2.4
2
+ Name: rbtr-lang-python
3
+ Version: 2026.7.0.dev0
4
+ Summary: rbtr — Python language plugin
5
+ License-Expression: MIT
6
+ Requires-Dist: rbtr==2026.7.0.dev0
7
+ Requires-Dist: tree-sitter-python
8
+ Requires-Python: >=3.13
@@ -0,0 +1,18 @@
1
+ rbtr_lang_python/__init__.py,sha256=mdH22CFTQNelM0_lyVdigVKZOL4ErqZoCFVDc01DvHE,38
2
+ rbtr_lang_python/plugin.py,sha256=FTnc6Vt7nog5Z2WhK3G0AbWx3kRJ-CPMvIo4gsDVaVw,4157
3
+ rbtr_lang_python/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ rbtr_lang_python/python.scm,sha256=aIeFFw1i0nvcAWkdqJLB-B0N7FNiOdF0wKuKjxVmJiM,1602
5
+ rbtr_lang_python/tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
+ rbtr_lang_python/tests/__snapshots__/test_samples/test_edges_match_snapshot.json,sha256=7KeVtn7c62Hfz8Kv3p-Tp8Q88IMympsICYe935jDXwc,77
7
+ rbtr_lang_python/tests/__snapshots__/test_samples/test_extraction_matches_snapshot.json,sha256=I4jAqsjOvt5wLIda25Tk9BpoIt0Jv9mctTAx3q2DIN0,12687
8
+ rbtr_lang_python/tests/cases_docstrings.py,sha256=zqIjJHom1mSP0X42RpfsFf4jjrPSrM3X7jd7pMQeaxQ,4851
9
+ rbtr_lang_python/tests/cases_extraction.py,sha256=dlGp-_M-BB1TQYG5w6BSXP2F0qfaPqa0mhC1I_rMG2Y,14436
10
+ rbtr_lang_python/tests/samples/python/config.py,sha256=vq19QIAEQ4RSj5JNq5sprVVMACq7nwgEZ6ikCFpHnpM,59
11
+ rbtr_lang_python/tests/samples/python/python.py,sha256=Q2Ggu-RMMM_iIHpg_YttbBK835sQ1AcfQ86BYFxemw0,2442
12
+ rbtr_lang_python/tests/test_docstrings.py,sha256=YvjbJVWsASmSmEBGTJUgYaCeQqgHoxzyisEsplQg95U,2081
13
+ rbtr_lang_python/tests/test_extraction.py,sha256=4OGNGIE8rOMaB00jdr5UkwNtgFxmsBg8Fyj8LfJX2bs,4580
14
+ rbtr_lang_python/tests/test_samples.py,sha256=lrT_iSY85rl1wNOMqpfwtUB4Ys2d5OKSRJrjTYenxnE,2826
15
+ rbtr_lang_python-2026.7.0.dev0.dist-info/WHEEL,sha256=CoDSoyhtC_eO_tlxRYzsTraPv1fPJRXFx91k6ISeAvA,81
16
+ rbtr_lang_python-2026.7.0.dev0.dist-info/entry_points.txt,sha256=RNfHUZ0LplE85FNOnadS_BV0BM1jhRtsz_qPal60sTk,58
17
+ rbtr_lang_python-2026.7.0.dev0.dist-info/METADATA,sha256=LIJFl35OAPWU5JuQQ5zkiPnMJ75C3mnT_rZJ2Z-aXOU,226
18
+ rbtr_lang_python-2026.7.0.dev0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: uv 0.11.28
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,3 @@
1
+ [rbtr.languages]
2
+ python = rbtr_lang_python.plugin:python
3
+