-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtest_instrument_codeflash_trace.py
More file actions
687 lines (542 loc) · 19.4 KB
/
Copy pathtest_instrument_codeflash_trace.py
File metadata and controls
687 lines (542 loc) · 19.4 KB
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
from __future__ import annotations
import tempfile
from pathlib import Path
from codeflash.benchmarking.instrument_codeflash_trace import add_codeflash_decorator_to_code, \
instrument_codeflash_trace_decorator
from codeflash.discovery.functions_to_optimize import FunctionParent, FunctionToOptimize
def test_add_decorator_to_normal_function() -> None:
"""Test adding decorator to a normal function."""
code = """
def normal_function():
return "Hello, World!"
"""
fto = FunctionToOptimize(
function_name="normal_function",
file_path=Path("dummy_path.py"),
parents=[]
)
modified_code = add_codeflash_decorator_to_code(
code=code,
functions_to_optimize=[fto]
)
expected_code = """
from codeflash.benchmarking.codeflash_trace import codeflash_trace
@codeflash_trace
def normal_function():
return "Hello, World!"
"""
assert modified_code.strip() == expected_code.strip()
def test_add_decorator_to_normal_method() -> None:
"""Test adding decorator to a normal method."""
code = """
class TestClass:
def normal_method(self):
return "Hello from method"
"""
fto = FunctionToOptimize(
function_name="normal_method",
file_path=Path("dummy_path.py"),
parents=[FunctionParent(name="TestClass", type="ClassDef")]
)
modified_code = add_codeflash_decorator_to_code(
code=code,
functions_to_optimize=[fto]
)
expected_code = """
from codeflash.benchmarking.codeflash_trace import codeflash_trace
class TestClass:
@codeflash_trace
def normal_method(self):
return "Hello from method"
"""
assert modified_code.strip() == expected_code.strip()
def test_add_decorator_to_classmethod() -> None:
"""Test adding decorator to a classmethod."""
code = """
class TestClass:
@classmethod
def class_method(cls):
return "Hello from classmethod"
"""
fto = FunctionToOptimize(
function_name="class_method",
file_path=Path("dummy_path.py"),
parents=[FunctionParent(name="TestClass", type="ClassDef")]
)
modified_code = add_codeflash_decorator_to_code(
code=code,
functions_to_optimize=[fto]
)
expected_code = """
from codeflash.benchmarking.codeflash_trace import codeflash_trace
class TestClass:
@classmethod
@codeflash_trace
def class_method(cls):
return "Hello from classmethod"
"""
assert modified_code.strip() == expected_code.strip()
def test_add_decorator_to_staticmethod() -> None:
"""Test adding decorator to a staticmethod."""
code = """
class TestClass:
@staticmethod
def static_method():
return "Hello from staticmethod"
"""
fto = FunctionToOptimize(
function_name="static_method",
file_path=Path("dummy_path.py"),
parents=[FunctionParent(name="TestClass", type="ClassDef")]
)
modified_code = add_codeflash_decorator_to_code(
code=code,
functions_to_optimize=[fto]
)
expected_code = """
from codeflash.benchmarking.codeflash_trace import codeflash_trace
class TestClass:
@staticmethod
@codeflash_trace
def static_method():
return "Hello from staticmethod"
"""
assert modified_code.strip() == expected_code.strip()
def test_add_decorator_to_init_function() -> None:
"""Test adding decorator to an __init__ function."""
code = """
class TestClass:
def __init__(self, value):
self.value = value
"""
fto = FunctionToOptimize(
function_name="__init__",
file_path=Path("dummy_path.py"),
parents=[FunctionParent(name="TestClass", type="ClassDef")]
)
modified_code = add_codeflash_decorator_to_code(
code=code,
functions_to_optimize=[fto]
)
expected_code = """
from codeflash.benchmarking.codeflash_trace import codeflash_trace
class TestClass:
@codeflash_trace
def __init__(self, value):
self.value = value
"""
assert modified_code.strip() == expected_code.strip()
def test_add_decorator_with_multiple_decorators() -> None:
"""Test adding decorator to a function with multiple existing decorators."""
code = """
class TestClass:
@property
@other_decorator
def property_method(self):
return self._value
"""
fto = FunctionToOptimize(
function_name="property_method",
file_path=Path("dummy_path.py"),
parents=[FunctionParent(name="TestClass", type="ClassDef")]
)
modified_code = add_codeflash_decorator_to_code(
code=code,
functions_to_optimize=[fto]
)
expected_code = """
from codeflash.benchmarking.codeflash_trace import codeflash_trace
class TestClass:
@property
@other_decorator
@codeflash_trace
def property_method(self):
return self._value
"""
assert modified_code.strip() == expected_code.strip()
def test_add_decorator_to_function_in_multiple_classes() -> None:
"""Test that only the right class's method gets the decorator."""
code = """
class TestClass:
def test_method(self):
return "This should get decorated"
class OtherClass:
def test_method(self):
return "This should NOT get decorated"
"""
fto = FunctionToOptimize(
function_name="test_method",
file_path=Path("dummy_path.py"),
parents=[FunctionParent(name="TestClass", type="ClassDef")]
)
modified_code = add_codeflash_decorator_to_code(
code=code,
functions_to_optimize=[fto]
)
expected_code = """
from codeflash.benchmarking.codeflash_trace import codeflash_trace
class TestClass:
@codeflash_trace
def test_method(self):
return "This should get decorated"
class OtherClass:
def test_method(self):
return "This should NOT get decorated"
"""
assert modified_code.strip() == expected_code.strip()
def test_add_decorator_to_nonexistent_function() -> None:
"""Test that code remains unchanged when function doesn't exist."""
code = """
def existing_function():
return "This exists"
"""
fto = FunctionToOptimize(
function_name="nonexistent_function",
file_path=Path("dummy_path.py"),
parents=[]
)
modified_code = add_codeflash_decorator_to_code(
code=code,
functions_to_optimize=[fto]
)
# Code should remain unchanged
assert modified_code.strip() == code.strip()
def test_add_decorator_to_multiple_functions() -> None:
"""Test adding decorator to multiple functions."""
code = """
def function_one():
return "First function"
class TestClass:
def method_one(self):
return "First method"
def method_two(self):
return "Second method"
def function_two():
return "Second function"
"""
functions_to_optimize = [
FunctionToOptimize(
function_name="function_one",
file_path=Path("dummy_path.py"),
parents=[]
),
FunctionToOptimize(
function_name="method_two",
file_path=Path("dummy_path.py"),
parents=[FunctionParent(name="TestClass", type="ClassDef")]
),
FunctionToOptimize(
function_name="function_two",
file_path=Path("dummy_path.py"),
parents=[]
)
]
modified_code = add_codeflash_decorator_to_code(
code=code,
functions_to_optimize=functions_to_optimize
)
expected_code = """
from codeflash.benchmarking.codeflash_trace import codeflash_trace
@codeflash_trace
def function_one():
return "First function"
class TestClass:
def method_one(self):
return "First method"
@codeflash_trace
def method_two(self):
return "Second method"
@codeflash_trace
def function_two():
return "Second function"
"""
assert modified_code.strip() == expected_code.strip()
def test_instrument_codeflash_trace_decorator_single_file() -> None:
"""Test instrumenting codeflash trace decorator on a single file."""
with tempfile.TemporaryDirectory() as temp_dir:
# Create a test Python file
test_file_path = Path(temp_dir) / "test_module.py"
test_file_content = """
def function_one():
return "First function"
class TestClass:
def method_one(self):
return "First method"
def method_two(self):
return "Second method"
def function_two():
return "Second function"
"""
test_file_path.write_text(test_file_content, encoding="utf-8")
# Define functions to optimize
functions_to_optimize = [
FunctionToOptimize(
function_name="function_one",
file_path=test_file_path,
parents=[]
),
FunctionToOptimize(
function_name="method_two",
file_path=test_file_path,
parents=[FunctionParent(name="TestClass", type="ClassDef")]
)
]
# Execute the function being tested
instrument_codeflash_trace_decorator({test_file_path: functions_to_optimize})
# Read the modified file
modified_content = test_file_path.read_text(encoding="utf-8")
# Define expected content (with isort applied)
expected_content = """
from codeflash.benchmarking.codeflash_trace import codeflash_trace
@codeflash_trace
def function_one():
return "First function"
class TestClass:
def method_one(self):
return "First method"
@codeflash_trace
def method_two(self):
return "Second method"
def function_two():
return "Second function"
"""
# Compare the modified content with expected content
assert modified_content.strip() == expected_content.strip()
def test_instrument_codeflash_trace_decorator_multiple_files() -> None:
"""Test instrumenting codeflash trace decorator on multiple files."""
with tempfile.TemporaryDirectory() as temp_dir:
# Create first test Python file
test_file_1_path = Path(temp_dir) / "module_a.py"
test_file_1_content = """
def function_a():
return "Function in module A"
class ClassA:
def method_a(self):
return "Method in ClassA"
"""
test_file_1_path.write_text(test_file_1_content, encoding="utf-8")
# Create second test Python file
test_file_2_path = Path(temp_dir) / "module_b.py"
test_file_2_content ="""
def function_b():
return "Function in module B"
class ClassB:
@staticmethod
def static_method_b():
return "Static method in ClassB"
"""
test_file_2_path.write_text(test_file_2_content, encoding="utf-8")
# Define functions to optimize
file_to_funcs_to_optimize = {
test_file_1_path: [
FunctionToOptimize(
function_name="function_a",
file_path=test_file_1_path,
parents=[]
)
],
test_file_2_path: [
FunctionToOptimize(
function_name="static_method_b",
file_path=test_file_2_path,
parents=[FunctionParent(name="ClassB", type="ClassDef")]
)
]
}
# Execute the function being tested
instrument_codeflash_trace_decorator(file_to_funcs_to_optimize)
# Read the modified files
modified_content_1 = test_file_1_path.read_text(encoding="utf-8")
modified_content_2 = test_file_2_path.read_text(encoding="utf-8")
# Define expected content for first file (with isort applied)
expected_content_1 = """
from codeflash.benchmarking.codeflash_trace import codeflash_trace
@codeflash_trace
def function_a():
return "Function in module A"
class ClassA:
def method_a(self):
return "Method in ClassA"
"""
# Define expected content for second file (with isort applied)
expected_content_2 = """
from codeflash.benchmarking.codeflash_trace import codeflash_trace
def function_b():
return "Function in module B"
class ClassB:
@staticmethod
@codeflash_trace
def static_method_b():
return "Static method in ClassB"
"""
# Compare the modified content with expected content
assert modified_content_1.strip() == expected_content_1.strip()
assert modified_content_2.strip() == expected_content_2.strip()
def test_add_decorator_to_method_after_nested_class() -> None:
"""Test adding decorator to a method that appears after a nested class definition."""
code = """
class OuterClass:
class NestedClass:
def nested_method(self):
return "Hello from nested class method"
def target_method(self):
return "Hello from target method after nested class"
"""
fto = FunctionToOptimize(
function_name="target_method",
file_path=Path("dummy_path.py"),
parents=[FunctionParent(name="OuterClass", type="ClassDef")]
)
modified_code = add_codeflash_decorator_to_code(
code=code,
functions_to_optimize=[fto]
)
expected_code = """
from codeflash.benchmarking.codeflash_trace import codeflash_trace
class OuterClass:
class NestedClass:
def nested_method(self):
return "Hello from nested class method"
@codeflash_trace
def target_method(self):
return "Hello from target method after nested class"
"""
assert modified_code.strip() == expected_code.strip()
def test_add_decorator_to_function_after_nested_function() -> None:
"""Test adding decorator to a function that appears after a function with a nested function."""
code = """
def function_with_nested():
def inner_function():
return "Hello from inner function"
return inner_function()
def target_function():
return "Hello from target function after nested function"
"""
fto = FunctionToOptimize(
function_name="target_function",
file_path=Path("dummy_path.py"),
parents=[]
)
modified_code = add_codeflash_decorator_to_code(
code=code,
functions_to_optimize=[fto]
)
expected_code = """
from codeflash.benchmarking.codeflash_trace import codeflash_trace
def function_with_nested():
def inner_function():
return "Hello from inner function"
return inner_function()
@codeflash_trace
def target_function():
return "Hello from target function after nested function"
"""
assert modified_code.strip() == expected_code.strip()
def test_instrument_codeflash_trace_skips_benchmarking_module() -> None:
"""Test that files in codeflash/benchmarking/ are skipped to avoid circular imports."""
with tempfile.TemporaryDirectory() as temp_dir:
# Create a directory structure that mimics codeflash/benchmarking/
benchmarking_dir = Path(temp_dir) / "codeflash" / "benchmarking"
benchmarking_dir.mkdir(parents=True)
test_file_path = benchmarking_dir / "some_module.py"
original_content = """
def some_function():
return "This should not be modified"
"""
test_file_path.write_text(original_content, encoding="utf-8")
fto = FunctionToOptimize(
function_name="some_function",
file_path=test_file_path,
parents=[]
)
instrument_codeflash_trace_decorator({test_file_path: [fto]})
# File should remain unchanged
assert test_file_path.read_text(encoding="utf-8") == original_content
def test_instrument_codeflash_trace_skips_picklepatch_module() -> None:
"""Test that files in codeflash/picklepatch/ are skipped to avoid circular imports."""
with tempfile.TemporaryDirectory() as temp_dir:
# Create a directory structure that mimics codeflash/picklepatch/
picklepatch_dir = Path(temp_dir) / "codeflash" / "picklepatch"
picklepatch_dir.mkdir(parents=True)
test_file_path = picklepatch_dir / "patcher.py"
original_content = """
def patch_function():
return "This should not be modified"
"""
test_file_path.write_text(original_content, encoding="utf-8")
fto = FunctionToOptimize(
function_name="patch_function",
file_path=test_file_path,
parents=[]
)
instrument_codeflash_trace_decorator({test_file_path: [fto]})
# File should remain unchanged
assert test_file_path.read_text(encoding="utf-8") == original_content
def test_instrument_codeflash_trace_nested_codeflash_path_skips_benchmarking() -> None:
"""Test that nested codeflash paths like /project/codeflash/codeflash/benchmarking/ are skipped.
The rpartition logic should find the LAST 'codeflash' in the path.
"""
with tempfile.TemporaryDirectory() as temp_dir:
# Create nested structure: project_codeflash/codeflash/benchmarking/
nested_dir = Path(temp_dir) / "project_codeflash" / "codeflash" / "benchmarking"
nested_dir.mkdir(parents=True)
test_file_path = nested_dir / "trace_module.py"
original_content = """
def trace_func():
return "Should not be modified"
"""
test_file_path.write_text(original_content, encoding="utf-8")
fto = FunctionToOptimize(
function_name="trace_func",
file_path=test_file_path,
parents=[]
)
instrument_codeflash_trace_decorator({test_file_path: [fto]})
# File should remain unchanged because last /codeflash/ is followed by benchmarking
assert test_file_path.read_text(encoding="utf-8") == original_content
def test_instrument_codeflash_trace_nested_codeflash_path_instruments_other_modules() -> None:
"""Test that nested codeflash paths with non-skipped modules ARE instrumented.
The rpartition logic should allow instrumentation when the submodule is not benchmarking/picklepatch.
"""
with tempfile.TemporaryDirectory() as temp_dir:
# Create nested structure: project_codeflash/codeflash/other_module/
nested_dir = Path(temp_dir) / "project_codeflash" / "codeflash" / "other_module"
nested_dir.mkdir(parents=True)
test_file_path = nested_dir / "utils.py"
original_content = """
def util_func():
return "Should be modified"
"""
test_file_path.write_text(original_content, encoding="utf-8")
fto = FunctionToOptimize(
function_name="util_func",
file_path=test_file_path,
parents=[]
)
instrument_codeflash_trace_decorator({test_file_path: [fto]})
# File SHOULD be modified because other_module is not in skip list
modified_content = test_file_path.read_text(encoding="utf-8")
assert "codeflash_trace" in modified_content
assert "@codeflash_trace" in modified_content
def test_instrument_codeflash_trace_no_codeflash_in_path() -> None:
"""Test that paths without 'codeflash' directory are instrumented normally."""
with tempfile.TemporaryDirectory() as temp_dir:
# Create a path with no 'codeflash' directory
project_dir = Path(temp_dir) / "myproject" / "src"
project_dir.mkdir(parents=True)
test_file_path = project_dir / "main.py"
original_content = """
def main_func():
return "Should be modified"
"""
test_file_path.write_text(original_content, encoding="utf-8")
fto = FunctionToOptimize(
function_name="main_func",
file_path=test_file_path,
parents=[]
)
instrument_codeflash_trace_decorator({test_file_path: [fto]})
# File SHOULD be modified
modified_content = test_file_path.read_text(encoding="utf-8")
assert "codeflash_trace" in modified_content
assert "@codeflash_trace" in modified_content