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
use dissolve::mypy_integration::query_type_with_python;
use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;

/// Create a test file with the given content in a temporary directory
fn create_test_file(dir: &TempDir, filename: &str, content: &str) -> PathBuf {
    let file_path = dir.path().join(filename);
    fs::write(&file_path, content).unwrap();
    file_path
}

#[test]
#[ignore] // Requires Python environment
fn test_name_expr_nodes() {
    let temp_dir = TempDir::new().unwrap();
    let test_file = create_test_file(
        &temp_dir,
        "name_expr_test.py",
        r#"
x: int = 42
y: str = "hello"
z = x + 10
print(y)
"#,
    );

    // Test 'x' in assignment
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "name_expr_test", 2, 1).unwrap();
    assert_eq!(result.variable, "x");
    assert_eq!(result.type_, "builtins.int");

    // Test 'y' in assignment
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "name_expr_test", 3, 1).unwrap();
    assert_eq!(result.variable, "y");
    assert_eq!(result.type_, "builtins.str");

    // Test 'z' in assignment
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "name_expr_test", 4, 1).unwrap();
    assert_eq!(result.variable, "z");
    assert_eq!(result.type_, "builtins.int");

    // Test 'y' in function call
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "name_expr_test", 5, 7).unwrap();
    assert_eq!(result.variable, "y");
    assert_eq!(result.type_, "builtins.str");
}

#[test]
#[ignore] // Requires Python environment
fn test_member_expr_nodes() {
    let temp_dir = TempDir::new().unwrap();
    let test_file = create_test_file(
        &temp_dir,
        "member_expr_test.py",
        r#"
class MyClass:
    attr: int = 10
    
    def method(self) -> str:
        return "result"

obj = MyClass()
val1 = obj.attr
val2 = obj.method()
"#,
    );

    // Test 'obj' in obj.attr
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "member_expr_test", 9, 10).unwrap();
    assert_eq!(result.variable, "obj");
    assert_eq!(result.type_, "member_expr_test.MyClass");

    // Test 'obj' in obj.method()
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "member_expr_test", 10, 10).unwrap();
    assert_eq!(result.variable, "obj");
    assert_eq!(result.type_, "member_expr_test.MyClass");
}

#[test]
#[ignore] // Requires Python environment
fn test_call_expr_nodes() {
    let temp_dir = TempDir::new().unwrap();
    let test_file = create_test_file(
        &temp_dir,
        "call_expr_test.py",
        r#"
def func() -> int:
    return 42

class MyClass:
    @classmethod
    def create(cls) -> "MyClass":
        return cls()
        
    def method(self, x: int) -> str:
        return str(x)

# Function call
result1 = func()

# Class instantiation
obj = MyClass()

# Class method call
obj2 = MyClass.create()

# Instance method call
result2 = obj.method(5)
"#,
    );

    // Test function call return type
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "call_expr_test", 14, 7).unwrap();
    assert_eq!(result.variable, "result1");
    assert_eq!(result.type_, "builtins.int");

    // Test class instantiation
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "call_expr_test", 17, 3).unwrap();
    assert_eq!(result.variable, "obj");
    assert_eq!(result.type_, "call_expr_test.MyClass");

    // Test class method call
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "call_expr_test", 20, 4).unwrap();
    assert_eq!(result.variable, "obj2");
    assert_eq!(result.type_, "call_expr_test.MyClass");

    // Test instance method call
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "call_expr_test", 23, 7).unwrap();
    assert_eq!(result.variable, "result2");
    assert_eq!(result.type_, "builtins.str");
}

#[test]
#[ignore] // Requires Python environment
fn test_list_and_tuple_expr() {
    let temp_dir = TempDir::new().unwrap();
    let test_file = create_test_file(
        &temp_dir,
        "collection_test.py",
        r#"
from typing import List, Tuple

# List expressions
list1: List[int] = [1, 2, 3]
list2 = [x * 2 for x in list1]

# Tuple expressions
tuple1: Tuple[int, str] = (42, "hello")
tuple2 = (1, "a", True)

# Nested collections
nested: List[Tuple[str, int]] = [("a", 1), ("b", 2)]
"#,
    );

    // Test list variable
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "collection_test", 5, 5).unwrap();
    assert_eq!(result.variable, "list1");
    assert_eq!(result.type_, "builtins.list[builtins.int]");

    // Test tuple variable
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "collection_test", 9, 6).unwrap();
    assert_eq!(result.variable, "tuple1");
    assert_eq!(result.type_, "tuple[builtins.int, builtins.str]");

    // Test nested collection
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "collection_test", 13, 6).unwrap();
    assert_eq!(result.variable, "nested");
    assert_eq!(
        result.type_,
        "builtins.list[tuple[builtins.str, builtins.int]]"
    );
}

#[test]
#[ignore] // Requires Python environment
fn test_control_flow_statements() {
    let temp_dir = TempDir::new().unwrap();
    let test_file = create_test_file(
        &temp_dir,
        "control_flow_test.py",
        r#"
from typing import List

items: List[str] = ["a", "b", "c"]

# If statement
if len(items) > 0:
    first = items[0]
    print(first)

# For loop
for item in items:
    print(item)
    
# While loop
i = 0
while i < len(items):
    current = items[i]
    i += 1
"#,
    );

    // Test variable in if block (first in print statement)
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "control_flow_test", 9, 15).unwrap();
    assert_eq!(result.variable, "first");
    assert_eq!(result.type_, "builtins.str");

    // Test first assignment
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "control_flow_test", 8, 9).unwrap();
    assert_eq!(result.variable, "first");
    assert_eq!(result.type_, "builtins.str");

    // Test variable in while loop
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "control_flow_test", 18, 11).unwrap();
    assert_eq!(result.variable, "current");
    assert_eq!(result.type_, "builtins.str");
}

#[test]
#[ignore] // Requires Python environment
fn test_deeply_nested_indentation() {
    let temp_dir = TempDir::new().unwrap();
    let test_file = create_test_file(
        &temp_dir,
        "indentation_test.py",
        r#"
class OuterClass:
    class InnerClass:
        def deep_method(self) -> int:  # Add return type annotation
            local_var: int = 42
            if True:
                nested_var: str = "nested"
                if True:
                    deeply_nested = local_var + 10
                    return deeply_nested
            return local_var

outer = OuterClass()
inner = OuterClass.InnerClass()
result = inner.deep_method()
"#,
    );

    // Test deeply nested variable
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "indentation_test", 9, 33).unwrap();
    assert_eq!(result.variable, "deeply_nested");
    assert_eq!(result.type_, "builtins.int");

    // Test method return type
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "indentation_test", 15, 6).unwrap();
    assert_eq!(result.variable, "result");
    assert_eq!(result.type_, "builtins.int");
}

#[test]
#[ignore] // Requires Python environment
fn test_line_continuations() {
    let temp_dir = TempDir::new().unwrap();
    let test_file = create_test_file(
        &temp_dir,
        "continuation_test.py",
        r#"
from typing import Dict, List

# Backslash continuation
very_long_variable_name: int = \
    42 + \
    100

# Parentheses continuation
result = (
    very_long_variable_name +
    200 +
    300
)

# Method chaining with continuation
class Builder:
    def with_x(self, x: int) -> "Builder":
        return self
    def with_y(self, y: int) -> "Builder":
        return self
    def build(self) -> Dict[str, int]:
        return {"x": 0, "y": 0}

builder_result = (
    Builder()
    .with_x(10)
    .with_y(20)
    .build()
)

# List with continuation
long_list: List[str] = [
    "first",
    "second",
    "third",
    "fourth"
]
"#,
    );

    // Test expression with parentheses continuation
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "continuation_test", 10, 6).unwrap();
    assert_eq!(result.variable, "result");
    assert_eq!(result.type_, "builtins.int");

    // Test method chaining result
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "continuation_test", 25, 14).unwrap();
    assert_eq!(result.variable, "builder_result");
    assert_eq!(result.type_, "builtins.dict[builtins.str, builtins.int]");

    // Test list with continuation
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "continuation_test", 33, 9).unwrap();
    assert_eq!(result.variable, "long_list");
    assert_eq!(result.type_, "builtins.list[builtins.str]");
}

#[test]
#[ignore] // Requires Python environment
fn test_unicode_and_special_chars() {
    let temp_dir = TempDir::new().unwrap();
    let test_file = create_test_file(
        &temp_dir,
        "unicode_test.py",
        r#"
# Unicode variable names (Python 3 allows this)
π: float = 3.14159
δ: float = 0.001

# String with unicode
greeting: str = "Hello, 世界! 🌍"

# Variables with underscores
_private_var: int = 42
__very_private: str = "secret"
snake_case_var: bool = True
"#,
    );

    // Test unicode variable
    let result = query_type_with_python(test_file.to_str().unwrap(), "unicode_test", 3, 1).unwrap();
    assert_eq!(result.variable, "π");
    assert_eq!(result.type_, "builtins.float");

    // Test string with unicode content
    let result = query_type_with_python(test_file.to_str().unwrap(), "unicode_test", 7, 8).unwrap();
    assert_eq!(result.variable, "greeting");
    assert_eq!(result.type_, "builtins.str");

    // Test underscore variables
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "unicode_test", 10, 12).unwrap();
    assert_eq!(result.variable, "_private_var");
    assert_eq!(result.type_, "builtins.int");
}

#[test]
#[ignore] // Requires Python environment
fn test_empty_file() {
    let temp_dir = TempDir::new().unwrap();
    let test_file = create_test_file(&temp_dir, "empty.py", "");

    let result = query_type_with_python(test_file.to_str().unwrap(), "empty", 1, 0);
    assert!(result.is_err());
}

#[test]
#[ignore] // Requires Python environment
fn test_comment_only_file() {
    let temp_dir = TempDir::new().unwrap();
    let test_file = create_test_file(
        &temp_dir,
        "comments_only.py",
        r#"
# This is a comment
# Another comment
"#,
    );

    let result = query_type_with_python(test_file.to_str().unwrap(), "comments_only", 2, 5);
    assert!(result.is_err());
}

#[test]
#[ignore] // Requires Python environment
fn test_out_of_bounds_position() {
    let temp_dir = TempDir::new().unwrap();
    let test_file = create_test_file(
        &temp_dir,
        "bounds_test.py",
        r#"
x = 1
y = 2
"#,
    );

    // Line beyond file
    let result = query_type_with_python(test_file.to_str().unwrap(), "bounds_test", 10, 0);
    assert!(result.is_err());

    // Column beyond line length
    let result = query_type_with_python(test_file.to_str().unwrap(), "bounds_test", 2, 50);
    assert!(result.is_err());
}

#[test]
#[ignore] // Requires Python environment
fn test_complex_type_annotations() {
    let temp_dir = TempDir::new().unwrap();
    let test_file = create_test_file(
        &temp_dir,
        "complex_types.py",
        r#"
from typing import Union, Optional, Callable, TypeVar, Generic

# Union types
var1: Union[int, str] = 42
var2: Optional[str] = None

# Callable types
func_var: Callable[[int, str], bool] = lambda x, y: True

# TypeVar and Generic
T = TypeVar('T')

class Container(Generic[T]):
    def __init__(self, value: T):
        self.value = value

# Generic usage
int_container = Container(42)
str_container = Container("hello")
"#,
    );

    // Test union type
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "complex_types", 5, 4).unwrap();
    assert_eq!(result.variable, "var1");
    assert!(
        result.type_.contains("Union")
            || result.type_.contains("int")
            || result.type_.contains("str")
    );

    // Test optional type
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "complex_types", 6, 4).unwrap();
    assert_eq!(result.variable, "var2");
    assert!(
        result.type_.contains("Optional")
            || result.type_.contains("Union")
            || result.type_.contains("None")
    );

    // Test generic container
    let result =
        query_type_with_python(test_file.to_str().unwrap(), "complex_types", 19, 13).unwrap();
    assert_eq!(result.variable, "int_container");
    assert!(result.type_.contains("Container"));
}