/ queries / python / exports.scm
exports.scm
 1  ;; ═══════════════════════════════════════════════════════════════════════════
 2  ;; Python Export Queries
 3  ;; ═══════════════════════════════════════════════════════════════════════════
 4  ;;
 5  ;; Python doesn't have explicit export syntax - all module-level names are
 6  ;; implicitly exported. We capture module-level assignments to track potential
 7  ;; env var re-exports.
 8  ;;
 9  ;; The __all__ list can be used for explicit exports but is optional.
10  ;;
11  ;; Captures:
12  ;;   @export_name    - The exported identifier name
13  ;;   @export_value   - The value being exported (optional)
14  ;;   @export_stmt    - The assignment/definition statement
15  ;;   @all_list       - The __all__ list if defined
16  
17  ;; ───────────────────────────────────────────────────────────────────────────
18  ;; Module-level variable assignments: foo = value
19  ;; These are implicit exports in Python
20  ;; ───────────────────────────────────────────────────────────────────────────
21  (module
22    (expression_statement
23      (assignment
24        left: (identifier) @export_name
25        right: (_) @export_value)) @export_stmt)
26  
27  ;; ───────────────────────────────────────────────────────────────────────────
28  ;; Module-level annotated assignments: foo: Type = value
29  ;; ───────────────────────────────────────────────────────────────────────────
30  (module
31    (expression_statement
32      (assignment
33        left: (identifier) @export_name
34        type: (_)
35        right: (_) @export_value)) @export_stmt)
36  
37  ;; ───────────────────────────────────────────────────────────────────────────
38  ;; Module-level function definitions: def foo(): ...
39  ;; ───────────────────────────────────────────────────────────────────────────
40  (module
41    (function_definition
42      name: (identifier) @export_name) @export_stmt)
43  
44  ;; ───────────────────────────────────────────────────────────────────────────
45  ;; Module-level class definitions: class Foo: ...
46  ;; ───────────────────────────────────────────────────────────────────────────
47  (module
48    (class_definition
49      name: (identifier) @export_name) @export_stmt)
50  
51  ;; ───────────────────────────────────────────────────────────────────────────
52  ;; __all__ = ["foo", "bar"] - explicit export list
53  ;; This defines which names are exported when using `from module import *`
54  ;; ───────────────────────────────────────────────────────────────────────────
55  (module
56    (expression_statement
57      (assignment
58        left: (identifier) @_all_name
59        right: (list) @all_list)
60      (#eq? @_all_name "__all__"))) @all_definition
61  
62  ;; ───────────────────────────────────────────────────────────────────────────
63  ;; Re-export via from x import y pattern at module level
64  ;; from .config import DATABASE_URL  # re-exports DATABASE_URL
65  ;; ───────────────────────────────────────────────────────────────────────────
66  (module
67    (import_from_statement
68      module_name: (_) @reexport_source
69      name: (dotted_name
70        (identifier) @export_name))) @reexport_stmt
71  
72  ;; ───────────────────────────────────────────────────────────────────────────
73  ;; Re-export via from x import y as z pattern
74  ;; from .config import DB_URL as DATABASE_URL
75  ;; ───────────────────────────────────────────────────────────────────────────
76  (module
77    (import_from_statement
78      module_name: (_) @reexport_source
79      name: (aliased_import
80        name: (dotted_name
81          (identifier) @local_name)
82        alias: (identifier) @export_name))) @reexport_stmt
83  
84  ;; ───────────────────────────────────────────────────────────────────────────
85  ;; Wildcard re-export: from .config import *
86  ;; ───────────────────────────────────────────────────────────────────────────
87  (module
88    (import_from_statement
89      module_name: (_) @wildcard_source
90      (wildcard_import))) @wildcard_reexport