sqry-lang-python 7.2.0

python language plugin for sqry
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
//! Coverage-targeted tests for `sqry-lang-python`.
//!
//! Exercises uncovered paths in:
//! - `src/relations/graph_builder.rs`:
//!   - `__all__` assignment (list export, tuple export, augmented assignment)
//!   - class inheritance: qualified (attribute), call, subscript (generic) bases
//!   - async generators, property decorators, route decorators (Flask/FastAPI)
//!   - `has_all_assignment` detection
//!   - relative imports (`from . import x`)
//!   - `is_module_level` and `is_public_name` edge cases
//!   - `ffi_library_simple_name` variants
//! - `src/relations/local_scopes.rs`:
//!   - lambda scope, comprehension scope (list/set/dict/generator)
//!   - walrus operator binding
//!   - `except Exception as e` binding
//!   - `with ... as x` binding
//!   - for-in-clause (comprehension variable)
//!   - `typed_parameter`, `default_parameter`, `typed_default_parameter`
//!   - `list_splat_pattern` (*args), `dictionary_splat_pattern` (**kwargs)

use sqry_core::graph::GraphBuilder;
use sqry_core::graph::unified::build::staging::StagingGraph;
use sqry_lang_python::relations::PythonGraphBuilder;
use std::path::Path;
use tree_sitter::Tree;

// ─────────────────────────────────────────────────────────────────────────────
// Shared helpers
// ─────────────────────────────────────────────────────────────────────────────

fn parse_python(source: &str) -> Tree {
    let mut parser = tree_sitter::Parser::new();
    parser
        .set_language(&tree_sitter_python::LANGUAGE.into())
        .expect("set Python language");
    parser.parse(source, None).expect("parse Python")
}

fn build_graph(source: &str) -> StagingGraph {
    let tree = parse_python(source);
    let mut staging = StagingGraph::new();
    let builder = PythonGraphBuilder::default();
    builder
        .build_graph(&tree, source.as_bytes(), Path::new("test.py"), &mut staging)
        .expect("build_graph should not fail");
    staging
}

fn has_edge_tag(staging: &StagingGraph, tag: &str) -> bool {
    use sqry_core::graph::unified::build::staging::StagingOp;
    staging
        .operations()
        .iter()
        .any(|op| matches!(op, StagingOp::AddEdge { kind, .. } if kind.tag() == tag))
}

fn all_edge_tags(staging: &StagingGraph) -> Vec<String> {
    use sqry_core::graph::unified::build::staging::StagingOp;
    staging
        .operations()
        .iter()
        .filter_map(|op| {
            if let StagingOp::AddEdge { kind, .. } = op {
                Some(kind.tag().to_string())
            } else {
                None
            }
        })
        .collect()
}

// ─────────────────────────────────────────────────────────────────────────────
// __all__ assignment export handling
// ─────────────────────────────────────────────────────────────────────────────

/// `__all__` list export creates export edges for listed names.
#[test]
fn all_assignment_list_creates_export_edges() {
    let source = r"
def public_func():
    pass

def _private_func():
    pass

__all__ = ['public_func']
";
    let staging = build_graph(source);
    assert!(
        has_edge_tag(&staging, "exports"),
        "Expected exports edge from __all__. Tags: {:?}",
        all_edge_tags(&staging)
    );
}

/// `__all__` with tuple syntax (also valid Python)
#[test]
fn all_assignment_tuple_creates_export_edges() {
    let source = r"
def alpha():
    pass

def beta():
    pass

__all__ = ('alpha', 'beta')
";
    let staging = build_graph(source);
    assert!(
        has_edge_tag(&staging, "exports"),
        "Expected exports edge from __all__ tuple. Tags: {:?}",
        all_edge_tags(&staging)
    );
}

/// `__all__ += ['name']` augmented assignment
#[test]
fn all_augmented_assignment() {
    let source = r"
def extra():
    pass

__all__ = ['extra']
__all__ += ['more']
";
    // Should not panic; base __all__ assignment produces export edges
    let staging = build_graph(source);
    assert!(
        staging.stats().nodes_staged >= 1,
        "Expected at least one node from augmented assignment source"
    );
}

// ─────────────────────────────────────────────────────────────────────────────
// Class inheritance — all 4 base kinds
// ─────────────────────────────────────────────────────────────────────────────

/// Simple identifier base class → Inherits edge
#[test]
fn class_inherits_identifier_base() {
    let source = r"
class Animal:
    def speak(self): pass

class Dog(Animal):
    def bark(self): pass
";
    let staging = build_graph(source);
    assert!(
        has_edge_tag(&staging, "inherits"),
        "Expected inherits edge. Tags: {:?}",
        all_edge_tags(&staging)
    );
}

/// Attribute base class: `class Child(module.Base):`
#[test]
fn class_inherits_attribute_base() {
    let source = r"
import abc

class MyInterface(abc.ABC):
    def method(self): pass
";
    let staging = build_graph(source);
    // Attribute-form base (abc.ABC) produces class + method nodes
    assert!(
        staging.stats().nodes_staged >= 1,
        "Expected class and method nodes from attribute base class"
    );
}

/// Call base class: `class Child(SomeMixin()):`
#[test]
fn class_inherits_call_base() {
    let source = r"
def make_mixin():
    class M:
        pass
    return M

class Concrete(make_mixin()):
    pass
";
    let staging = build_graph(source);
    // Call-form base exercises the call-expression branch of base resolution
    assert!(
        staging.stats().nodes_staged >= 1,
        "Expected nodes from call-form base class"
    );
}

/// Subscript (generic) base class: `class MyList(list[int]):`
#[test]
fn class_inherits_subscript_base() {
    let source = r"
from typing import Generic, TypeVar
T = TypeVar('T')

class Box(Generic[T]):
    def get(self) -> T: ...
";
    let staging = build_graph(source);
    // Subscript-form base (Generic[T]) exercises subscript branch; imports edge expected
    assert!(
        has_edge_tag(&staging, "imports"),
        "Expected imports edge from 'from typing import'. Tags: {:?}",
        all_edge_tags(&staging)
    );
}

/// Multiple inheritance with keyword argument (metaclass): should skip keyword args
#[test]
fn class_inherits_skips_keyword_arguments() {
    let source = r"
import abc

class MyABC(abc.ABC, metaclass=abc.ABCMeta):
    def do_it(self): pass
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 1);
}

// ─────────────────────────────────────────────────────────────────────────────
// Property decorator handling
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn property_decorator_creates_property_node() {
    let source = r"
class Temperature:
    def __init__(self, value: float):
        self._value = value

    @property
    def celsius(self) -> float:
        return self._value

    @property
    def fahrenheit(self) -> float:
        return self._value * 9 / 5 + 32
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 2, "Expected property nodes");
}

// ─────────────────────────────────────────────────────────────────────────────
// Async generators and async functions
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn async_function_creates_function_node() {
    let source = r"
import asyncio

async def fetch(url: str) -> str:
    await asyncio.sleep(0)
    return url
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 1);
}

#[test]
fn async_generator_function() {
    let source = r"
async def async_range(n: int):
    for i in range(n):
        yield i
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 1);
}

/// Awaited call (`await fetch()`) should be recorded as async call edge
#[test]
fn awaited_call_recorded() {
    let source = r"
import asyncio

async def helper():
    return 42

async def runner():
    result = await helper()
    return result
";
    let staging = build_graph(source);
    // Should have a Calls edge (is_async may be set)
    assert!(
        has_edge_tag(&staging, "calls"),
        "Expected calls edge for awaited call. Tags: {:?}",
        all_edge_tags(&staging)
    );
}

// ─────────────────────────────────────────────────────────────────────────────
// Route decorators (Flask / FastAPI)
// ─────────────────────────────────────────────────────────────────────────────

/// Flask-style route: `@app.route('/path')`
#[test]
fn flask_route_decorator() {
    let source = r"
class Flask:
    def route(self, path, methods=None):
        def decorator(f):
            return f
        return decorator

app = Flask()

@app.route('/users', methods=['GET'])
def get_users():
    return []
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 1);
}

/// FastAPI-style route: `@router.get('/path')`
#[test]
fn fastapi_get_route_decorator() {
    let source = r"
class APIRouter:
    def get(self, path: str):
        def decorator(f):
            return f
        return decorator

router = APIRouter()

@router.get('/items')
async def list_items():
    return []
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 1);
}

// ─────────────────────────────────────────────────────────────────────────────
// Import handling edge cases
// ─────────────────────────────────────────────────────────────────────────────

/// `import numpy as np` (aliased import)
#[test]
fn aliased_import() {
    let source = r"
import numpy as np

def use_numpy():
    return np.array([1, 2, 3])
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 1);
}

/// `from os.path import join` (from import)
#[test]
fn from_import() {
    let source = r"
from os.path import join

def make_path(base: str, name: str) -> str:
    return join(base, name)
";
    let staging = build_graph(source);
    assert!(
        has_edge_tag(&staging, "imports"),
        "Expected imports edge. Tags: {:?}",
        all_edge_tags(&staging)
    );
}

/// Relative import `from . import utils`
#[test]
fn relative_import() {
    let source = r"
from . import utils

def use_utils():
    return utils.helper()
";
    // Relative imports should produce an imports edge (or at minimum not panic)
    let staging = build_graph(source);
    assert!(
        has_edge_tag(&staging, "imports"),
        "Expected imports edge for relative import. Tags: {:?}",
        all_edge_tags(&staging)
    );
}

/// Relative import with module: `from .models import User`
#[test]
fn relative_import_with_module() {
    let source = r"
from .models import User

def get_user(user_id: int) -> User:
    return User(user_id)
";
    let staging = build_graph(source);
    assert!(
        has_edge_tag(&staging, "imports"),
        "Expected imports edge for relative import with module. Tags: {:?}",
        all_edge_tags(&staging)
    );
}

/// Wildcard import `from module import *`
#[test]
fn wildcard_import() {
    let source = r"
from typing import *

def foo(x: Optional[int]) -> List[str]:
    return []
";
    let staging = build_graph(source);
    assert!(
        has_edge_tag(&staging, "imports"),
        "Expected imports edge for wildcard import. Tags: {:?}",
        all_edge_tags(&staging)
    );
}

// ─────────────────────────────────────────────────────────────────────────────
// Type-annotated functions (return type + parameter type hints)
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn function_with_return_type_annotation() {
    let source = r"
from typing import List

def get_names() -> List[str]:
    return ['alice', 'bob']
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 1);
}

#[test]
fn method_with_typed_parameters() {
    let source = r"
class Calculator:
    def add(self, a: int, b: int) -> int:
        return a + b

    def subtract(self, a: float, b: float) -> float:
        return a - b
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 2);
}

// ─────────────────────────────────────────────────────────────────────────────
// local_scopes.rs — scope and binding coverage
// ─────────────────────────────────────────────────────────────────────────────

/// Lambda scope: binds its parameters separately from enclosing function
#[test]
fn lambda_creates_scope() {
    let source = r"
def make_adder(n):
    return lambda x: x + n
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 1);
}

/// List comprehension scope (iteration variable scoped to comprehension)
#[test]
fn list_comprehension_scope() {
    let source = r"
def squares(n):
    return [x * x for x in range(n)]
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 1);
}

/// Set comprehension scope
#[test]
fn set_comprehension_scope() {
    let source = r"
def unique_squares(items):
    return {x * x for x in items}
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 1);
}

/// Dictionary comprehension scope
#[test]
fn dict_comprehension_scope() {
    let source = r"
def invert(d: dict) -> dict:
    return {v: k for k, v in d.items()}
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 1);
}

/// Generator expression scope
#[test]
fn generator_expression_scope() {
    let source = r"
def total(items):
    return sum(x * 2 for x in items)
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 1);
}

/// Walrus operator `:=` in condition binds in enclosing function scope
#[test]
fn walrus_operator_binding() {
    let source = r"
import re

def find_match(pattern: str, text: str):
    if (m := re.search(pattern, text)):
        return m.group(0)
    return None
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 1);
}

/// `except Exception as e:` creates binding for `e`
#[test]
fn except_clause_binding() {
    let source = r"
def safe_parse(text: str):
    try:
        return int(text)
    except ValueError as e:
        print(e)
        return None
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 1);
}

/// `with open(...) as f:` creates binding for `f`
#[test]
fn with_statement_binding() {
    let source = r"
def read_file(path: str) -> str:
    with open(path) as f:
        return f.read()
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 1);
}

/// For-loop variable binding
#[test]
fn for_statement_binding() {
    let source = r"
def sum_all(numbers):
    total = 0
    for n in numbers:
        total += n
    return total
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 1);
}

/// `*args` in function parameters (`list_splat_pattern`)
#[test]
fn splat_args_parameter() {
    let source = r"
def variadic(*args):
    return sum(args)
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 1);
}

/// `**kwargs` in function parameters (`dictionary_splat_pattern`)
#[test]
fn kwargs_parameter() {
    let source = r"
def flexible(**kwargs):
    return kwargs
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 1);
}

/// `*args` with type annotation (`*args: int`) — `typed_parameter` path
#[test]
fn typed_splat_args_parameter() {
    let source = r"
def typed_variadic(*args: int) -> int:
    return sum(args)
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 1);
}

/// Default parameter: `def foo(x=5):`
#[test]
fn default_parameter() {
    let source = r"
def greet(name='world'):
    return f'Hello, {name}!'
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 1);
}

/// Typed default parameter: `def foo(x: int = 5):`
#[test]
fn typed_default_parameter() {
    let source = r"
def power(base: int, exp: int = 2) -> int:
    return base ** exp
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 1);
}

// ─────────────────────────────────────────────────────────────────────────────
// Native extension FFI handling (ctypes / cffi)
// ─────────────────────────────────────────────────────────────────────────────

/// Importing a known native C module triggers FFI edge
#[test]
fn native_c_module_import_creates_ffi_edge() {
    let source = r"
import math

def circle_area(radius: float) -> float:
    return math.pi * radius ** 2
";
    let staging = build_graph(source);
    // math is in THIRD_PARTY_C_PACKAGES or STD_C_MODULES — may produce FfiCall;
    // at minimum, a function node and an imports edge must be produced
    assert!(
        staging.stats().nodes_staged >= 1,
        "Expected at least one node from native C module import source"
    );
}

/// ctypes CDLL usage
#[test]
fn ctypes_cdll_usage() {
    let source = r#"
import ctypes

libc = ctypes.CDLL("libc.so.6")

def call_printf():
    libc.printf(b"Hello\n")
"#;
    let staging = build_graph(source);
    // ctypes import exercises the CDLL code path; function node must be present
    assert!(
        staging.stats().nodes_staged >= 1,
        "Expected at least one node from ctypes CDLL usage source"
    );
}

// ─────────────────────────────────────────────────────────────────────────────
// Annotated variable assignments (type hints at module level)
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn annotated_assignment_module_level() {
    let source = r"
from typing import Optional

MAX_SIZE: int = 100
name: Optional[str] = None
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 1);
}

// ─────────────────────────────────────────────────────────────────────────────
// Public vs private name export
// ─────────────────────────────────────────────────────────────────────────────

/// Public functions (no leading underscore) get export edges when no __all__
#[test]
fn public_function_exported_without_all() {
    let source = r"
def public_helper():
    return 42

def _internal_helper():
    return 0
";
    let staging = build_graph(source);
    // public_helper should get an Exports edge, _internal_helper should not
    assert!(
        has_edge_tag(&staging, "exports"),
        "Expected exports edge for public function. Tags: {:?}",
        all_edge_tags(&staging)
    );
}

/// Private functions (leading underscore) should NOT be auto-exported
#[test]
fn private_function_not_exported() {
    let source = r"
def _private():
    pass
";
    let staging = build_graph(source);
    // _private should NOT have an exports edge (tag is lowercase "exports")
    assert!(
        !has_edge_tag(&staging, "exports"),
        "Private function should not be exported. Tags: {:?}",
        all_edge_tags(&staging)
    );
}

// ─────────────────────────────────────────────────────────────────────────────
// Class methods — public vs private, static vs instance
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn class_with_dunder_methods() {
    let source = r"
class Node:
    def __init__(self, value: int):
        self.value = value

    def __repr__(self) -> str:
        return f'Node({self.value})'

    def __eq__(self, other: object) -> bool:
        if isinstance(other, Node):
            return self.value == other.value
        return NotImplemented
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 3);
}

/// Self-referential call: `self.method()` should be resolved
#[test]
fn self_method_call_resolved() {
    let source = r"
class Worker:
    def prepare(self):
        self.validate()
        self.process()

    def validate(self):
        pass

    def process(self):
        pass
";
    let staging = build_graph(source);
    assert!(
        has_edge_tag(&staging, "calls"),
        "Expected calls edges for self method calls. Tags: {:?}",
        all_edge_tags(&staging)
    );
}

// ─────────────────────────────────────────────────────────────────────────────
// Module-level top-level calls
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn module_level_call() {
    let source = r"
def setup():
    pass

def teardown():
    pass

setup()
teardown()
";
    let staging = build_graph(source);
    assert!(
        has_edge_tag(&staging, "calls"),
        "Expected calls from module-level calls. Tags: {:?}",
        all_edge_tags(&staging)
    );
}

// ─────────────────────────────────────────────────────────────────────────────
// Nested classes and nested functions
// ─────────────────────────────────────────────────────────────────────────────

#[test]
fn nested_class_in_function() {
    let source = r"
def make_counter():
    class Counter:
        def __init__(self):
            self.n = 0

        def increment(self):
            self.n += 1

    return Counter()
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 2);
}

#[test]
fn nested_function_in_class() {
    let source = r"
class Processor:
    def process(self, items):
        def transform(item):
            return item * 2
        return [transform(x) for x in items]
";
    let staging = build_graph(source);
    assert!(staging.stats().nodes_staged >= 2);
}