dissolve-python 0.3.0

A tool to dissolve deprecated calls in Python codebases
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
// Extended edge case tests for AST parameter substitution

use crate::migrate_ruff::migrate_file;
use crate::type_introspection_context::TypeIntrospectionContext;
use crate::{RuffDeprecatedFunctionCollector, TypeIntrospectionMethod};
use std::collections::HashMap;
use std::path::Path;

#[test]
fn test_ellipsis_literal_in_parameters() {
    // Test ellipsis literal (...) used in type hints and slices
    let source = r#"
from dissolve import replace_me
from typing import Tuple

@replace_me()
def process_ellipsis(data, slice_val):
    return handle(data[slice_val])

# Ellipsis usage
result1 = process_ellipsis(array, ...)
result2 = process_ellipsis(tensor[:, ..., :], slice(None))
"#;

    let collector = RuffDeprecatedFunctionCollector::new("test_module".to_string(), None);
    let result = collector.collect_from_source(source.to_string()).unwrap();

    let mut type_context =
        TypeIntrospectionContext::new(TypeIntrospectionMethod::PyrightLsp).unwrap();
    let migrated = migrate_file(
        source,
        "test_module",
        Path::new("test.py"),
        &mut type_context,
        result.replacements,
        HashMap::new(),
    )
    .unwrap();
    type_context.shutdown().unwrap();

    // Should handle ellipsis literal
    assert!(migrated.contains("handle(array[...])"));
}

#[test]
fn test_matrix_multiplication_operator() {
    // Test @ operator for matrix multiplication
    let source = r#"
from dissolve import replace_me

@replace_me()
def matrix_op(a, b):
    return compute(a @ b)

# Matrix multiplication
result = matrix_op(matrix1, matrix2)
result2 = matrix_op(A @ B, C)
"#;

    let collector = RuffDeprecatedFunctionCollector::new("test_module".to_string(), None);
    let result = collector.collect_from_source(source.to_string()).unwrap();

    let mut type_context =
        TypeIntrospectionContext::new(TypeIntrospectionMethod::PyrightLsp).unwrap();
    let migrated = migrate_file(
        source,
        "test_module",
        Path::new("test.py"),
        &mut type_context,
        result.replacements,
        HashMap::new(),
    )
    .unwrap();
    type_context.shutdown().unwrap();

    assert!(migrated.contains("compute(matrix1 @ matrix2)"));
    assert!(migrated.contains("compute(A @ B @ C)"));
}

#[test]
fn test_complex_number_literals() {
    // Test complex number literals
    let source = r#"
from dissolve import replace_me

@replace_me()
def process_complex(num):
    return calculate(num)

# Complex numbers
result1 = process_complex(3+4j)
result2 = process_complex(1.5-2.5j)
result3 = process_complex(5j)
"#;

    let collector = RuffDeprecatedFunctionCollector::new("test_module".to_string(), None);
    let result = collector.collect_from_source(source.to_string()).unwrap();

    let mut type_context =
        TypeIntrospectionContext::new(TypeIntrospectionMethod::PyrightLsp).unwrap();
    let migrated = migrate_file(
        source,
        "test_module",
        Path::new("test.py"),
        &mut type_context,
        result.replacements,
        HashMap::new(),
    )
    .unwrap();
    type_context.shutdown().unwrap();

    // Note: AST formats complex numbers as real + imaginary parts
    assert!(migrated.contains("calculate(3 + 4j)"));
    assert!(migrated.contains("calculate(1.5 - 2.5j)"));
    assert!(migrated.contains("calculate(5j)"));
}

#[test]
fn test_chained_comparisons() {
    // Test chained comparison operations
    let source = r#"
from dissolve import replace_me

@replace_me()
def check_range(val):
    return validate(val)

# Chained comparisons
result1 = check_range(0 < x < 10)
result2 = check_range(a <= b < c <= d)
result3 = check_range(x == y == z)
"#;

    let collector = RuffDeprecatedFunctionCollector::new("test_module".to_string(), None);
    let result = collector.collect_from_source(source.to_string()).unwrap();

    let mut type_context =
        TypeIntrospectionContext::new(TypeIntrospectionMethod::PyrightLsp).unwrap();
    let migrated = migrate_file(
        source,
        "test_module",
        Path::new("test.py"),
        &mut type_context,
        result.replacements,
        HashMap::new(),
    )
    .unwrap();
    type_context.shutdown().unwrap();

    assert!(migrated.contains("validate(0 < x < 10)"));
    assert!(migrated.contains("validate(a <= b < c <= d)"));
    assert!(migrated.contains("validate(x == y == z)"));
}

#[test]
fn test_dict_merge_operators() {
    // Test dictionary unpacking and merge operations
    let source = r#"
from dissolve import replace_me

@replace_me()
def merge_data(data):
    return process(data)

# Dict merge operations
result1 = merge_data({**base_dict})
result2 = merge_data({**dict1, **dict2})
result3 = merge_data({**config, "key": "value", **overrides})
result4 = merge_data({"a": 1, **{"b": 2}, "c": 3})
"#;

    let collector = RuffDeprecatedFunctionCollector::new("test_module".to_string(), None);
    let result = collector.collect_from_source(source.to_string()).unwrap();

    let mut type_context =
        TypeIntrospectionContext::new(TypeIntrospectionMethod::PyrightLsp).unwrap();
    let migrated = migrate_file(
        source,
        "test_module",
        Path::new("test.py"),
        &mut type_context,
        result.replacements,
        HashMap::new(),
    )
    .unwrap();
    type_context.shutdown().unwrap();

    assert!(migrated.contains("process({**base_dict})"));
    assert!(migrated.contains("process({**dict1, **dict2})"));
}

#[test]
fn test_long_integer_literals_with_underscores() {
    // Test integer literals with underscores for readability
    let source = r#"
from dissolve import replace_me

@replace_me()
def process_number(num):
    return calculate(num)

# Long integers with underscores
result1 = process_number(1_000_000)
result2 = process_number(0xFF_FF_FF)
result3 = process_number(0b1111_0000_1111_0000)
"#;

    let collector = RuffDeprecatedFunctionCollector::new("test_module".to_string(), None);
    let result = collector.collect_from_source(source.to_string()).unwrap();

    let mut type_context =
        TypeIntrospectionContext::new(TypeIntrospectionMethod::PyrightLsp).unwrap();
    let migrated = migrate_file(
        source,
        "test_module",
        Path::new("test.py"),
        &mut type_context,
        result.replacements,
        HashMap::new(),
    )
    .unwrap();
    type_context.shutdown().unwrap();

    // Note: AST might normalize these to regular integers
    assert!(migrated.contains("calculate(1000000)"));
}

#[test]
fn test_empty_collections() {
    // Test empty collection literals
    let source = r#"
from dissolve import replace_me

@replace_me()
def process_collection(coll):
    return handle(coll)

# Empty collections
result1 = process_collection([])
result2 = process_collection({})
result3 = process_collection(())
result4 = process_collection(set())
"#;

    let collector = RuffDeprecatedFunctionCollector::new("test_module".to_string(), None);
    let result = collector.collect_from_source(source.to_string()).unwrap();

    let mut type_context =
        TypeIntrospectionContext::new(TypeIntrospectionMethod::PyrightLsp).unwrap();
    let migrated = migrate_file(
        source,
        "test_module",
        Path::new("test.py"),
        &mut type_context,
        result.replacements,
        HashMap::new(),
    )
    .unwrap();
    type_context.shutdown().unwrap();

    assert!(migrated.contains("handle([])"));
    assert!(migrated.contains("handle({})"));
    assert!(migrated.contains("handle(())"));
    assert!(migrated.contains("handle(set())"));
}

#[test]
fn test_nested_comprehensions_with_multiple_for_clauses() {
    // Test complex nested comprehensions
    let source = r#"
from dissolve import replace_me

@replace_me()
def process_nested(data):
    return analyze(data)

# Nested comprehensions
result1 = process_nested([x * y for x in range(3) for y in range(3)])
result2 = process_nested({(x, y): x*y for x in range(3) for y in range(3) if x != y})
result3 = process_nested([
    [x + y for y in row]
    for x, row in enumerate(matrix)
    if sum(row) > 0
])
"#;

    let collector = RuffDeprecatedFunctionCollector::new("test_module".to_string(), None);
    let result = collector.collect_from_source(source.to_string()).unwrap();

    let mut type_context =
        TypeIntrospectionContext::new(TypeIntrospectionMethod::PyrightLsp).unwrap();
    let migrated = migrate_file(
        source,
        "test_module",
        Path::new("test.py"),
        &mut type_context,
        result.replacements,
        HashMap::new(),
    )
    .unwrap();
    type_context.shutdown().unwrap();

    assert!(migrated.contains("analyze([x * y for x in range(3) for y in range(3)])"));
}

#[test]
fn test_class_attribute_access() {
    // Test class attribute access (not instance)
    let source = r#"
from dissolve import replace_me

class Config:
    DEFAULT_VALUE = 42
    settings = {"debug": True}

@replace_me()
def get_config(key):
    return fetch(key)

# Class attribute access
result1 = get_config(Config.DEFAULT_VALUE)
result2 = get_config(Config.settings["debug"])
result3 = get_config(MyClass.__name__)
"#;

    let collector = RuffDeprecatedFunctionCollector::new("test_module".to_string(), None);
    let result = collector.collect_from_source(source.to_string()).unwrap();

    let mut type_context =
        TypeIntrospectionContext::new(TypeIntrospectionMethod::PyrightLsp).unwrap();
    let migrated = migrate_file(
        source,
        "test_module",
        Path::new("test.py"),
        &mut type_context,
        result.replacements,
        HashMap::new(),
    )
    .unwrap();
    type_context.shutdown().unwrap();

    assert!(migrated.contains("fetch(Config.DEFAULT_VALUE)"));
    assert!(migrated.contains(r#"fetch(Config.settings["debug"])"#));
    assert!(migrated.contains("fetch(MyClass.__name__)"));
}

#[test]
fn test_tuple_unpacking_in_parameters() {
    // Test tuple unpacking scenarios
    let source = r#"
from dissolve import replace_me

@replace_me()
def process_tuple(data):
    return handle(*data)

@replace_me()
def process_args(a, b, c):
    return compute(a, b, c)

# Tuple unpacking
coords = (10, 20, 30)
result1 = process_tuple(coords)
result2 = process_args(*coords)
"#;

    let collector = RuffDeprecatedFunctionCollector::new("test_module".to_string(), None);
    let result = collector.collect_from_source(source.to_string()).unwrap();

    let mut type_context =
        TypeIntrospectionContext::new(TypeIntrospectionMethod::PyrightLsp).unwrap();
    let migrated = migrate_file(
        source,
        "test_module",
        Path::new("test.py"),
        &mut type_context,
        result.replacements,
        HashMap::new(),
    )
    .unwrap();
    type_context.shutdown().unwrap();

    // Note: Current implementation filters out unprovided *args parameters
    // The current behavior is acceptable - perfect *args mapping is complex
    assert!(migrated.contains("handle(*coords)")); // *coords is preserved
    assert!(migrated.contains("compute(*coords")); // *coords is preserved
}

#[test]
fn test_nested_walrus_operators() {
    // Test multiple walrus operators in complex expressions
    let source = r#"
from dissolve import replace_me

@replace_me()
def process_values(val):
    return compute(val)

# Nested walrus operators
if x := process_values((y := get_value()) + (z := get_other())):
    print(x, y, z)

# In nested comprehensions
data = [process_values(inner) 
        for outer in items 
        if (inner := transform(outer)) and (check := validate(inner))]
"#;

    let collector = RuffDeprecatedFunctionCollector::new("test_module".to_string(), None);
    let result = collector.collect_from_source(source.to_string()).unwrap();

    let mut type_context =
        TypeIntrospectionContext::new(TypeIntrospectionMethod::PyrightLsp).unwrap();
    let migrated = migrate_file(
        source,
        "test_module",
        Path::new("test.py"),
        &mut type_context,
        result.replacements,
        HashMap::new(),
    )
    .unwrap();
    type_context.shutdown().unwrap();

    // Walrus operators are properly parenthesized in binary operations
    assert!(migrated.contains("compute((y := get_value()) + (z := get_other()))"));
}

#[test]
fn test_unicode_identifiers() {
    // Test non-ASCII variable names
    let source = r#"
from dissolve import replace_me

@replace_me()
def process_data(données):
    return traiter(données)

# Unicode identifiers
π = 3.14159
result = process_data(π)
λ_function = lambda x: x * 2
result2 = process_data(λ_function(5))
"#;

    let collector = RuffDeprecatedFunctionCollector::new("test_module".to_string(), None);
    let result = collector.collect_from_source(source.to_string()).unwrap();

    let mut type_context =
        TypeIntrospectionContext::new(TypeIntrospectionMethod::PyrightLsp).unwrap();
    let migrated = migrate_file(
        source,
        "test_module",
        Path::new("test.py"),
        &mut type_context,
        result.replacements,
        HashMap::new(),
    )
    .unwrap();
    type_context.shutdown().unwrap();

    assert!(migrated.contains("traiter(π)"));
    assert!(migrated.contains("traiter(λ_function(5))"));
}

#[test]
fn test_nested_f_strings() {
    // Test f-strings containing expressions with other f-strings
    let source = r#"
from dissolve import replace_me

@replace_me()
def log_nested(msg):
    return logger.log(msg)

# Nested f-strings and complex expressions
name = "test"
result = log_nested(f"Processing {f'item_{name}'} with value {x if x > 0 else 'negative'}")
result2 = log_nested(f"Result: {','.join(f'{k}={v}' for k, v in data.items())}")
"#;

    let collector = RuffDeprecatedFunctionCollector::new("test_module".to_string(), None);
    let result = collector.collect_from_source(source.to_string()).unwrap();

    let mut type_context =
        TypeIntrospectionContext::new(TypeIntrospectionMethod::PyrightLsp).unwrap();
    let migrated = migrate_file(
        source,
        "test_module",
        Path::new("test.py"),
        &mut type_context,
        result.replacements,
        HashMap::new(),
    )
    .unwrap();
    type_context.shutdown().unwrap();

    // Check that f-strings are preserved
    assert!(migrated.contains("logger.log(f\""));
}

#[test]
fn test_starred_expressions_in_lists() {
    // Test starred expressions in list/tuple literals
    let source = r#"
from dissolve import replace_me

@replace_me()
def process_list(items):
    return handle(items)

# Starred expressions
first = [1, 2, 3]
second = [4, 5, 6]
result1 = process_list([*first, *second])
result2 = process_list([0, *first, 7, 8, *second, 9])
result3 = process_list((*first, *second))
"#;

    let collector = RuffDeprecatedFunctionCollector::new("test_module".to_string(), None);
    let result = collector.collect_from_source(source.to_string()).unwrap();

    let mut type_context =
        TypeIntrospectionContext::new(TypeIntrospectionMethod::PyrightLsp).unwrap();
    let migrated = migrate_file(
        source,
        "test_module",
        Path::new("test.py"),
        &mut type_context,
        result.replacements,
        HashMap::new(),
    )
    .unwrap();
    type_context.shutdown().unwrap();

    assert!(migrated.contains("handle([*first, *second])"));
    assert!(migrated.contains("handle([0, *first, 7, 8, *second, 9])"));
}

#[test]
fn test_async_comprehensions() {
    // Test async comprehensions
    let source = r#"
from dissolve import replace_me

@replace_me()
async def process_async_data(data):
    return await handle_async(data)

# Async comprehensions
async def test():
    result = await process_async_data([x async for x in async_generator()])
    result2 = await process_async_data({k: v async for k, v in async_items()})
"#;

    let collector = RuffDeprecatedFunctionCollector::new("test_module".to_string(), None);
    let result = collector.collect_from_source(source.to_string()).unwrap();

    let mut type_context =
        TypeIntrospectionContext::new(TypeIntrospectionMethod::PyrightLsp).unwrap();
    let migrated = migrate_file(
        source,
        "test_module",
        Path::new("test.py"),
        &mut type_context,
        result.replacements,
        HashMap::new(),
    )
    .unwrap();
    type_context.shutdown().unwrap();

    let expected = r#"
from dissolve import replace_me

@replace_me()
async def process_async_data(data):
    return await handle_async(data)

# Async comprehensions
async def test():
    result = await handle_async([x async for x in async_generator()])
    result2 = await handle_async({k: v async for (k, v) in async_items()})
"#;

    assert_eq!(migrated, expected);
}

#[test]
fn test_power_operator_with_negative_base() {
    // Test power operator with various edge cases
    let source = r#"
from dissolve import replace_me

@replace_me()
def calculate_power(expr):
    return compute(expr)

# Power operator edge cases
result1 = calculate_power(-2 ** 3)  # Should be -(2**3) = -8
result2 = calculate_power((-2) ** 3)  # Should be (-2)**3 = -8
result3 = calculate_power(2 ** -3)  # Should be 2**(-3) = 0.125
result4 = calculate_power(2 ** 3 ** 2)  # Right associative: 2**(3**2)
"#;

    let collector = RuffDeprecatedFunctionCollector::new("test_module".to_string(), None);
    let result = collector.collect_from_source(source.to_string()).unwrap();

    let mut type_context =
        TypeIntrospectionContext::new(TypeIntrospectionMethod::PyrightLsp).unwrap();
    let migrated = migrate_file(
        source,
        "test_module",
        Path::new("test.py"),
        &mut type_context,
        result.replacements,
        HashMap::new(),
    )
    .unwrap();
    type_context.shutdown().unwrap();

    // Check precedence is preserved
    assert!(migrated.contains("compute(-2 ** 3)"));
    // Note: AST removes parentheses around negative numbers, both results are -2 ** 3
    assert!(migrated.contains("compute(-2 ** 3)") && migrated.contains("result2 ="));
}