depyler-core 3.24.0

Core transpilation engine for the Depyler Python-to-Rust transpiler
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
//! # Pytest Assertion Extractor for CITL Training Pipeline
//!
//! GH-174: Extracts simple `assert` statements from `test_*.py` files
//! as additional CITL training signal.
//!
//! ## Overview
//!
//! Many test files contain simple I/O assertions equivalent to doctests:
//!
//! ```python
//! def test_square():
//!     assert square(4) == 16
//!     assert square(-3) == 9
//! ```
//!
//! This module extracts these patterns into the same format as doctests.
//!
//! ## Scope
//!
//! Extract **only** simple patterns:
//! - `assert f(x) == y`
//! - `assert f(x, y) == z`
//! - `assert f(x) == [a, b, c]`
//!
//! **Ignore** complex patterns:
//! - Fixtures, mocks, parametrize
//! - Exception testing (`pytest.raises`)
//! - Approximate comparisons (`pytest.approx`)

use crate::doctest_extractor::Doctest;
use anyhow::Result;
use serde::{Deserialize, Serialize};

/// Result of extracting pytest assertions
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PytestResult {
    /// Source file
    pub source: String,
    /// Extracted assertions as doctests
    pub assertions: Vec<Doctest>,
}

/// Extracts simple assert statements from pytest files
#[derive(Debug, Clone, Default)]
pub struct PytestExtractor {
    /// Only extract from test_*.py files
    pub strict_test_files: bool,
}

impl PytestExtractor {
    /// Creates a new PytestExtractor with default settings
    pub fn new() -> Self {
        Self {
            strict_test_files: true,
        }
    }

    /// Configure whether to only extract from test_*.py files
    pub fn with_strict_test_files(mut self, strict: bool) -> Self {
        self.strict_test_files = strict;
        self
    }

    /// Extract all simple assertions from Python test source code
    pub fn extract(&self, source: &str) -> Result<Vec<Doctest>> {
        let mut assertions = Vec::new();
        let lines: Vec<&str> = source.lines().collect();

        let mut current_function: Option<String> = None;

        for (line_num, line) in lines.iter().enumerate() {
            let trimmed = line.trim();

            // Track test function definitions
            if trimmed.starts_with("def test_") {
                if let Some(name) = Self::extract_function_name(trimmed) {
                    current_function = Some(name);
                }
            } else if trimmed.starts_with("def ") && !trimmed.starts_with("def test_") {
                // Non-test function, clear context
                current_function = None;
            }

            // Look for simple assert statements
            if trimmed.starts_with("assert ") {
                if let Some(doctest) = self.parse_assert(trimmed, line_num + 1, &current_function) {
                    assertions.push(doctest);
                }
            }
        }

        Ok(assertions)
    }

    /// Extract function name from a def line
    fn extract_function_name(line: &str) -> Option<String> {
        let after_def = line.strip_prefix("def ")?.trim();
        let paren_idx = after_def.find('(')?;
        Some(after_def[..paren_idx].to_string())
    }

    /// Parse an assert statement into a Doctest if it's a simple pattern
    fn parse_assert(
        &self,
        line: &str,
        line_num: usize,
        _current_function: &Option<String>,
    ) -> Option<Doctest> {
        // Remove "assert " prefix
        let assertion = line.strip_prefix("assert ")?.trim();

        // Skip complex patterns
        if self.is_complex_assertion(assertion) {
            return None;
        }

        // Look for == comparison
        let eq_idx = assertion.find(" == ")?;
        let left = assertion[..eq_idx].trim();
        let right = assertion[eq_idx + 4..].trim();

        // Left side should be a function call
        if !left.contains('(') || !left.contains(')') {
            return None;
        }

        // Extract the function being tested (from the call)
        let func_name = self.extract_called_function(left)?;

        // Clean up right side (remove trailing comments, etc.)
        let expected = self.clean_expected(right);

        Some(Doctest {
            function: func_name,
            input: left.to_string(),
            expected,
            line: line_num,
        })
    }

    /// Check if an assertion is too complex to extract
    fn is_complex_assertion(&self, assertion: &str) -> bool {
        // Skip pytest-specific patterns
        if assertion.contains("pytest.") {
            return true;
        }

        // Skip approximate comparisons
        if assertion.contains("approx(") {
            return true;
        }

        // Skip assertions with 'in' operator
        if assertion.contains(" in ") && !assertion.contains(" == ") {
            return true;
        }

        // Skip assertions with 'is' operator (identity checks)
        if assertion.contains(" is ") && !assertion.contains(" == ") {
            return true;
        }

        // Skip assertions with 'not' operator at the start
        if assertion.starts_with("not ") {
            return true;
        }

        // Skip multi-condition assertions
        if assertion.contains(" and ") || assertion.contains(" or ") {
            return true;
        }

        // Skip assertions with lambda
        if assertion.contains("lambda") {
            return true;
        }

        // Skip type checks
        if assertion.contains("isinstance(") || assertion.contains("type(") {
            return true;
        }

        false
    }

    /// Extract the function name being called
    fn extract_called_function(&self, call_expr: &str) -> Option<String> {
        let paren_idx = call_expr.find('(')?;
        let func_part = &call_expr[..paren_idx];

        // Handle method calls like obj.method()
        if let Some(dot_idx) = func_part.rfind('.') {
            Some(func_part[dot_idx + 1..].to_string())
        } else {
            Some(func_part.to_string())
        }
    }

    /// Clean up the expected value
    fn clean_expected(&self, expected: &str) -> String {
        let mut result = expected.to_string();

        // Remove trailing comments
        if let Some(hash_idx) = result.find('#') {
            result = result[..hash_idx].trim().to_string();
        }

        // Remove trailing comma (from tuple unpacking)
        result = result.trim_end_matches(',').trim().to_string();

        result
    }

    /// Extract assertions to the same format as doctest results
    pub fn extract_to_result(&self, source: &str, filename: &str) -> Result<PytestResult> {
        let assertions = self.extract(source)?;
        Ok(PytestResult {
            source: filename.to_string(),
            assertions,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // =========================================================================
    // RED TESTS - These define the expected behavior (GH-174)
    // =========================================================================

    #[test]
    fn test_extract_simple_assert_eq() {
        let source = r#"
def test_square():
    assert square(4) == 16
"#;

        let extractor = PytestExtractor::new();
        let assertions = extractor.extract(source).unwrap();

        assert_eq!(assertions.len(), 1);
        assert_eq!(assertions[0].function, "square");
        assert_eq!(assertions[0].input, "square(4)");
        assert_eq!(assertions[0].expected, "16");
    }

    #[test]
    fn test_extract_multiple_assertions() {
        let source = r#"
def test_square():
    assert square(4) == 16
    assert square(-3) == 9
    assert square(0) == 0
"#;

        let extractor = PytestExtractor::new();
        let assertions = extractor.extract(source).unwrap();

        assert_eq!(assertions.len(), 3);
        assert_eq!(assertions[0].expected, "16");
        assert_eq!(assertions[1].expected, "9");
        assert_eq!(assertions[2].expected, "0");
    }

    #[test]
    fn test_extract_multiple_args() {
        let source = r#"
def test_add():
    assert add(1, 2) == 3
    assert add(-1, 1) == 0
"#;

        let extractor = PytestExtractor::new();
        let assertions = extractor.extract(source).unwrap();

        assert_eq!(assertions.len(), 2);
        assert_eq!(assertions[0].input, "add(1, 2)");
        assert_eq!(assertions[0].expected, "3");
    }

    #[test]
    fn test_extract_string_expected() {
        let source = r#"
def test_greet():
    assert greet("World") == "Hello, World!"
"#;

        let extractor = PytestExtractor::new();
        let assertions = extractor.extract(source).unwrap();

        assert_eq!(assertions.len(), 1);
        assert_eq!(assertions[0].expected, "\"Hello, World!\"");
    }

    #[test]
    fn test_extract_list_expected() {
        let source = r#"
def test_range_list():
    assert range_list(3) == [0, 1, 2]
"#;

        let extractor = PytestExtractor::new();
        let assertions = extractor.extract(source).unwrap();

        assert_eq!(assertions.len(), 1);
        assert_eq!(assertions[0].expected, "[0, 1, 2]");
    }

    #[test]
    fn test_extract_dict_expected() {
        let source = r#"
def test_make_dict():
    assert make_dict("a", 1) == {"a": 1}
"#;

        let extractor = PytestExtractor::new();
        let assertions = extractor.extract(source).unwrap();

        assert_eq!(assertions.len(), 1);
        assert_eq!(assertions[0].expected, "{\"a\": 1}");
    }

    #[test]
    fn test_extract_boolean_expected() {
        let source = r#"
def test_is_even():
    assert is_even(4) == True
    assert is_even(3) == False
"#;

        let extractor = PytestExtractor::new();
        let assertions = extractor.extract(source).unwrap();

        assert_eq!(assertions.len(), 2);
        assert_eq!(assertions[0].expected, "True");
        assert_eq!(assertions[1].expected, "False");
    }

    #[test]
    fn test_skip_pytest_raises() {
        let source = r#"
def test_error():
    with pytest.raises(ValueError):
        divide(1, 0)
    assert divide(10, 2) == 5
"#;

        let extractor = PytestExtractor::new();
        let assertions = extractor.extract(source).unwrap();

        // Should only extract the simple assertion
        assert_eq!(assertions.len(), 1);
        assert_eq!(assertions[0].input, "divide(10, 2)");
    }

    #[test]
    fn test_skip_pytest_approx() {
        let source = r#"
def test_float():
    assert divide(10, 3) == pytest.approx(3.333, rel=0.01)
    assert multiply(2, 3) == 6
"#;

        let extractor = PytestExtractor::new();
        let assertions = extractor.extract(source).unwrap();

        // Should only extract the exact comparison
        assert_eq!(assertions.len(), 1);
        assert_eq!(assertions[0].input, "multiply(2, 3)");
    }

    #[test]
    fn test_skip_complex_and_or() {
        let source = r#"
def test_complex():
    assert foo(1) == 1 and bar(2) == 2
    assert simple(3) == 3
"#;

        let extractor = PytestExtractor::new();
        let assertions = extractor.extract(source).unwrap();

        // Should only extract the simple assertion
        assert_eq!(assertions.len(), 1);
        assert_eq!(assertions[0].input, "simple(3)");
    }

    #[test]
    fn test_skip_isinstance() {
        let source = r#"
def test_types():
    assert isinstance(foo(), int)
    assert bar() == 42
"#;

        let extractor = PytestExtractor::new();
        let assertions = extractor.extract(source).unwrap();

        assert_eq!(assertions.len(), 1);
        assert_eq!(assertions[0].input, "bar()");
    }

    #[test]
    fn test_skip_in_operator() {
        let source = r#"
def test_membership():
    assert 1 in get_list()
    assert get_first() == 1
"#;

        let extractor = PytestExtractor::new();
        let assertions = extractor.extract(source).unwrap();

        assert_eq!(assertions.len(), 1);
        assert_eq!(assertions[0].input, "get_first()");
    }

    #[test]
    fn test_method_call() {
        let source = r#"
def test_method():
    obj = MyClass()
    assert obj.compute(5) == 25
"#;

        let extractor = PytestExtractor::new();
        let assertions = extractor.extract(source).unwrap();

        assert_eq!(assertions.len(), 1);
        assert_eq!(assertions[0].function, "compute");
        assert_eq!(assertions[0].input, "obj.compute(5)");
    }

    #[test]
    fn test_line_numbers() {
        let source = r#"
def test_foo():
    x = 1
    assert foo(1) == 1
    y = 2
    assert foo(2) == 4
"#;

        let extractor = PytestExtractor::new();
        let assertions = extractor.extract(source).unwrap();

        assert_eq!(assertions.len(), 2);
        assert_eq!(assertions[0].line, 4);
        assert_eq!(assertions[1].line, 6);
    }

    #[test]
    fn test_extract_to_result() {
        let source = r#"
def test_square():
    assert square(4) == 16
"#;

        let extractor = PytestExtractor::new();
        let result = extractor.extract_to_result(source, "test_math.py").unwrap();

        assert_eq!(result.source, "test_math.py");
        assert_eq!(result.assertions.len(), 1);
    }

    #[test]
    fn test_empty_source() {
        let source = "";
        let extractor = PytestExtractor::new();
        let assertions = extractor.extract(source).unwrap();
        assert!(assertions.is_empty());
    }

    #[test]
    fn test_no_assertions() {
        let source = r#"
def test_foo():
    x = compute()
    print(x)
"#;

        let extractor = PytestExtractor::new();
        let assertions = extractor.extract(source).unwrap();
        assert!(assertions.is_empty());
    }

    #[test]
    fn test_non_function_call_lhs() {
        let source = r#"
def test_foo():
    assert x == 1
    assert foo() == 2
"#;

        let extractor = PytestExtractor::new();
        let assertions = extractor.extract(source).unwrap();

        // Should only extract the function call assertion
        assert_eq!(assertions.len(), 1);
        assert_eq!(assertions[0].input, "foo()");
    }

    #[test]
    fn test_trailing_comment() {
        let source = r#"
def test_foo():
    assert foo(1) == 1  # This tests the basic case
"#;

        let extractor = PytestExtractor::new();
        let assertions = extractor.extract(source).unwrap();

        assert_eq!(assertions.len(), 1);
        assert_eq!(assertions[0].expected, "1");
    }

    #[test]
    fn test_none_expected() {
        let source = r#"
def test_returns_none():
    assert returns_none() == None
"#;

        let extractor = PytestExtractor::new();
        let assertions = extractor.extract(source).unwrap();

        assert_eq!(assertions.len(), 1);
        assert_eq!(assertions[0].expected, "None");
    }

    #[test]
    fn test_tuple_expected() {
        let source = r#"
def test_tuple():
    assert get_tuple() == (1, 2, 3)
"#;

        let extractor = PytestExtractor::new();
        let assertions = extractor.extract(source).unwrap();

        assert_eq!(assertions.len(), 1);
        assert_eq!(assertions[0].expected, "(1, 2, 3)");
    }

    #[test]
    fn test_float_expected() {
        let source = r#"
def test_float():
    assert divide(10, 4) == 2.5
"#;

        let extractor = PytestExtractor::new();
        let assertions = extractor.extract(source).unwrap();

        assert_eq!(assertions.len(), 1);
        assert_eq!(assertions[0].expected, "2.5");
    }

    #[test]
    fn test_negative_number_expected() {
        let source = r#"
def test_negative():
    assert negate(5) == -5
"#;

        let extractor = PytestExtractor::new();
        let assertions = extractor.extract(source).unwrap();

        assert_eq!(assertions.len(), 1);
        assert_eq!(assertions[0].expected, "-5");
    }
}