forked from deepset-ai/haystack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_python_code_splitter.py
More file actions
880 lines (723 loc) · 34.2 KB
/
Copy pathtest_python_code_splitter.py
File metadata and controls
880 lines (723 loc) · 34.2 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
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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
import textwrap
import pytest
from haystack import Document
from haystack.components.preprocessors import PythonCodeSplitter
@pytest.fixture
def simple_module_source():
return textwrap.dedent(
'''
"""Example module docstring."""
import os
import sys
from math import sqrt
def add(a, b):
"""Add two numbers."""
return a + b
def subtract(a, b):
"""Subtract two numbers."""
return a - b
'''
).lstrip()
@pytest.fixture
def class_source():
return textwrap.dedent(
'''
"""Geometry helpers."""
from math import pi
class Shape:
"""Base shape."""
kind = "shape"
def __init__(self, name: str) -> None:
self.name = name
def describe(self) -> str:
return f"shape {self.name}"
class Circle(Shape, metaclass=type):
"""A circle."""
def __init__(self, r: float) -> None:
super().__init__("circle")
self.r = r
@staticmethod
def pi_value() -> float:
return pi
@classmethod
def unit(cls) -> "Circle":
return cls(1.0)
def area(self) -> float:
return pi * self.r * self.r
'''
).lstrip()
@pytest.fixture
def oversized_function_source():
"""A function long enough to trigger the secondary line-based split."""
body_lines = "\n".join(f" x_{i} = {i}" for i in range(200))
return f"def giant():\n{body_lines}\n return x_0\n"
class TestInitValidation:
def test_defaults(self):
splitter = PythonCodeSplitter()
assert splitter.min_effective_lines == 20
assert splitter.max_effective_lines == 100
assert splitter.expected_chars_per_line == 45
assert splitter.oversized_factor == 3
assert splitter.strip_docstrings is False
assert splitter.preserve_class_definition is True
assert splitter.secondary_split_overlap == 5
assert splitter.secondary_split_length is None
def test_custom_values(self):
splitter = PythonCodeSplitter(
min_effective_lines=2,
max_effective_lines=10,
expected_chars_per_line=80,
oversized_factor=4,
strip_docstrings=True,
preserve_class_definition=False,
secondary_split_overlap=2,
secondary_split_length=15,
)
assert splitter.min_effective_lines == 2
assert splitter.max_effective_lines == 10
assert splitter.expected_chars_per_line == 80
assert splitter.oversized_factor == 4
assert splitter.strip_docstrings is True
assert splitter.preserve_class_definition is False
assert splitter.secondary_split_overlap == 2
assert splitter.secondary_split_length == 15
@pytest.mark.parametrize(
"kwargs",
[
{"min_effective_lines": 0},
{"min_effective_lines": -1},
{"max_effective_lines": 0},
{"max_effective_lines": -3},
{"min_effective_lines": 10, "max_effective_lines": 5},
{"expected_chars_per_line": 0},
{"oversized_factor": 0},
{"secondary_split_overlap": -1},
{"secondary_split_length": -1},
],
)
def test_invalid_init_raises(self, kwargs):
with pytest.raises(ValueError):
PythonCodeSplitter(**kwargs)
class TestRunInputValidation:
def test_none_content_raises_value_error(self):
splitter = PythonCodeSplitter()
doc = Document(content=None)
with pytest.raises(ValueError):
splitter.run(documents=[doc])
def test_non_string_content_raises_type_error(self):
splitter = PythonCodeSplitter()
doc = Document(content="placeholder")
doc.content = 12345 # type: ignore[assignment]
with pytest.raises(TypeError):
splitter.run(documents=[doc])
def test_invalid_syntax_raises(self):
splitter = PythonCodeSplitter()
doc = Document(content="def broken(:\n pass\n")
with pytest.raises(SyntaxError):
splitter.run(documents=[doc])
def test_empty_documents_list(self):
splitter = PythonCodeSplitter()
result = splitter.run(documents=[])
assert result == {"documents": []}
class TestBasicOutput:
def test_returns_dict_with_documents(self, simple_module_source):
splitter = PythonCodeSplitter()
result = splitter.run(documents=[Document(content=simple_module_source)])
assert isinstance(result, dict)
assert "documents" in result
assert isinstance(result["documents"], list)
assert len(result["documents"]) >= 1
for chunk in result["documents"]:
assert isinstance(chunk, Document)
assert isinstance(chunk.content, str)
assert chunk.content
def test_split_id_starts_at_zero_and_increments(self, class_source):
splitter = PythonCodeSplitter(min_effective_lines=2, max_effective_lines=5)
result = splitter.run(documents=[Document(content=class_source)])
chunks = result["documents"]
ids = [c.meta["split_id"] for c in chunks]
assert ids == list(range(len(chunks)))
def test_source_id_consistent_within_one_document(self, class_source):
splitter = PythonCodeSplitter(min_effective_lines=2, max_effective_lines=5)
result = splitter.run(documents=[Document(content=class_source)])
chunks = result["documents"]
source_ids = {c.meta["source_id"] for c in chunks}
assert len(source_ids) == 1
def test_source_id_differs_between_documents(self, simple_module_source, class_source):
splitter = PythonCodeSplitter(min_effective_lines=2, max_effective_lines=10)
docs = [Document(content=simple_module_source), Document(content=class_source)]
result = splitter.run(documents=docs)
source_ids = {c.meta["source_id"] for c in result["documents"]}
assert len(source_ids) == 2
def test_chunks_have_required_meta_fields(self, simple_module_source):
splitter = PythonCodeSplitter(min_effective_lines=2, max_effective_lines=10)
result = splitter.run(documents=[Document(content=simple_module_source)])
for chunk in result["documents"]:
assert "source_id" in chunk.meta
assert "split_id" in chunk.meta
assert "start_line" in chunk.meta
assert "end_line" in chunk.meta
assert "unit_kinds" in chunk.meta
assert isinstance(chunk.meta["unit_kinds"], list)
assert chunk.meta["start_line"] >= 1
assert chunk.meta["end_line"] >= chunk.meta["start_line"]
def test_unit_kinds_lists_what_was_merged(self, simple_module_source):
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=200)
result = splitter.run(documents=[Document(content=simple_module_source)])
all_kinds = set()
for chunk in result["documents"]:
for kind in chunk.meta["unit_kinds"]:
all_kinds.add(kind)
text = " ".join(all_kinds).lower()
assert any("import" in t for t in all_kinds) or "import" in text
assert any("func" in t or "method" in t for t in all_kinds) or any("func" in t for t in text.split())
def test_multiple_documents_each_produces_chunks(self, simple_module_source, class_source):
splitter = PythonCodeSplitter(min_effective_lines=2, max_effective_lines=10)
docs = [
Document(content=simple_module_source, meta={"file_name": "a.py"}),
Document(content=class_source, meta={"file_name": "b.py"}),
]
result = splitter.run(documents=docs)
file_names = {c.meta.get("file_name") for c in result["documents"]}
assert file_names == {"a.py", "b.py"}
def test_split_id_resets_per_document(self, simple_module_source, class_source):
splitter = PythonCodeSplitter(min_effective_lines=2, max_effective_lines=10)
docs = [
Document(content=simple_module_source, meta={"file_name": "a.py"}),
Document(content=class_source, meta={"file_name": "b.py"}),
]
result = splitter.run(documents=docs)
per_file_ids: dict[str, list[int]] = {"a.py": [], "b.py": []}
for chunk in result["documents"]:
per_file_ids[chunk.meta["file_name"]].append(chunk.meta["split_id"])
for ids in per_file_ids.values():
assert ids == sorted(ids)
assert ids[0] == 0
class TestOrderingAndLineRanges:
def test_chunks_are_in_source_order(self, class_source):
splitter = PythonCodeSplitter(min_effective_lines=2, max_effective_lines=5)
result = splitter.run(documents=[Document(content=class_source)])
prev_start = 0
for chunk in result["documents"]:
assert chunk.meta["start_line"] >= prev_start
assert chunk.meta["end_line"] >= chunk.meta["start_line"]
prev_start = chunk.meta["start_line"]
def test_chunks_dont_overlap_in_primary_split(self, class_source):
splitter = PythonCodeSplitter(min_effective_lines=2, max_effective_lines=5)
chunks = splitter.run(documents=[Document(content=class_source)])["documents"]
for prev, nxt in zip(chunks, chunks[1:], strict=False):
assert nxt.meta["start_line"] > prev.meta["end_line"]
def test_chunks_read_top_to_bottom(self, class_source):
# With the default preserve_class_definition=True, chunks may have a class
# signature prefixed but the source slice itself must still appear verbatim.
splitter = PythonCodeSplitter(min_effective_lines=2, max_effective_lines=5)
result = splitter.run(documents=[Document(content=class_source)])
source_lines = class_source.splitlines(keepends=True)
for chunk in result["documents"]:
expected = "".join(source_lines[chunk.meta["start_line"] - 1 : chunk.meta["end_line"]])
assert expected in (chunk.content or "")
def test_chunks_equal_source_slice_without_class_preservation(self, class_source):
splitter = PythonCodeSplitter(min_effective_lines=2, max_effective_lines=5, preserve_class_definition=False)
result = splitter.run(documents=[Document(content=class_source)])
source_lines = class_source.splitlines(keepends=True)
for chunk in result["documents"]:
expected = "".join(source_lines[chunk.meta["start_line"] - 1 : chunk.meta["end_line"]])
assert (chunk.content or "").endswith(expected)
class TestFileNamePropagation:
def test_file_name_propagated_to_all_chunks(self, simple_module_source):
splitter = PythonCodeSplitter(min_effective_lines=2, max_effective_lines=5)
result = splitter.run(documents=[Document(content=simple_module_source, meta={"file_name": "sample.py"})])
for chunk in result["documents"]:
assert chunk.meta["file_name"] == "sample.py"
def test_no_file_name_when_absent(self, simple_module_source):
splitter = PythonCodeSplitter(min_effective_lines=2, max_effective_lines=5)
result = splitter.run(documents=[Document(content=simple_module_source)])
for chunk in result["documents"]:
assert "file_name" not in chunk.meta
def test_other_meta_is_propagated(self, simple_module_source):
splitter = PythonCodeSplitter(min_effective_lines=2, max_effective_lines=5)
result = splitter.run(
documents=[Document(content=simple_module_source, meta={"file_name": "x.py", "project": "haystack"})]
)
for chunk in result["documents"]:
assert chunk.meta["project"] == "haystack"
class TestDecorators:
def test_decorators_metadata_present(self):
source = textwrap.dedent(
"""
class A:
@staticmethod
def s():
return 1
@classmethod
def c(cls):
return 2
"""
).lstrip()
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=2)
result = splitter.run(documents=[Document(content=source)])
all_decorators = []
for chunk in result["documents"]:
all_decorators.extend(chunk.meta.get("decorators") or [])
assert any("staticmethod" in d for d in all_decorators)
assert any("classmethod" in d for d in all_decorators)
def test_decorator_lines_included_in_chunk_content(self):
source = textwrap.dedent(
"""
class A:
@staticmethod
def s():
return 1
"""
).lstrip()
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=2)
result = splitter.run(documents=[Document(content=source)])
chunks_with_s = [c for c in result["documents"] if "def s" in (c.content or "")]
assert chunks_with_s
for chunk in chunks_with_s:
assert "@staticmethod" in (chunk.content or "")
def test_decorators_deduped_in_chunk(self):
source = textwrap.dedent(
"""
class A:
@staticmethod
def one():
return 1
@staticmethod
def two():
return 2
"""
).lstrip()
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=20)
result = splitter.run(documents=[Document(content=source)])
for chunk in result["documents"]:
decorators = chunk.meta.get("decorators") or []
assert len(decorators) == len(set(decorators))
def test_function_with_three_decorators_lists_all(self):
source = textwrap.dedent(
"""
def deco_a(fn):
return fn
def deco_b(fn):
return fn
def deco_c(fn):
return fn
@deco_a
@deco_b
@deco_c
def triple():
return 1
"""
).lstrip()
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=200)
result = splitter.run(documents=[Document(content=source)])
chunks_with_triple = [c for c in result["documents"] if "def triple" in (c.content or "")]
assert chunks_with_triple
decorators = [d for c in chunks_with_triple for d in (c.meta.get("decorators") or [])]
joined = " ".join(decorators)
assert "deco_a" in joined
assert "deco_b" in joined
assert "deco_c" in joined
def test_function_with_three_decorators_all_lines_in_content(self):
source = textwrap.dedent(
"""
@deco_a
@deco_b
@deco_c
def triple():
return 1
"""
).lstrip()
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=50)
result = splitter.run(documents=[Document(content=source)])
chunks_with_triple = [c for c in result["documents"] if "def triple" in (c.content or "")]
assert chunks_with_triple
for chunk in chunks_with_triple:
content = chunk.content or ""
assert "@deco_a" in content
assert "@deco_b" in content
assert "@deco_c" in content
class TestIncludeClassesMeta:
@pytest.mark.parametrize("class_name", ["Shape", "Circle"])
def test_class_methods_carry_include_classes(self, class_source, class_name):
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=3)
result = splitter.run(documents=[Document(content=class_source)])
chunks = [c for c in result["documents"] if class_name in (c.meta.get("include_classes") or [])]
assert chunks
def test_include_classes_absent_when_no_class_involved(self, simple_module_source):
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=200)
result = splitter.run(documents=[Document(content=simple_module_source)])
for chunk in result["documents"]:
include_classes = chunk.meta.get("include_classes")
assert not include_classes
def test_include_classes_is_deduplicated(self):
source = textwrap.dedent(
"""
class A:
def one(self):
return 1
def two(self):
return 2
def three(self):
return 3
"""
).lstrip()
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=200)
result = splitter.run(documents=[Document(content=source)])
for chunk in result["documents"]:
include_classes = chunk.meta.get("include_classes") or []
assert len(include_classes) == len(set(include_classes))
def test_include_classes_preserves_source_order(self):
source = textwrap.dedent(
"""
class First:
def f(self):
return 1
class Second:
def g(self):
return 2
class Third:
def h(self):
return 3
"""
).lstrip()
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=500)
result = splitter.run(documents=[Document(content=source)])
for chunk in result["documents"]:
include_classes = chunk.meta.get("include_classes") or []
if len(include_classes) >= 2:
expected_order = [c for c in ["First", "Second", "Third"] if c in include_classes]
assert include_classes == expected_order
class TestPreserveClassDefinition:
@pytest.fixture
def multi_method_class_source(self):
return textwrap.dedent(
'''
class Greeter:
"""A friendly greeter."""
kind = "greeter"
def __init__(self, name: str) -> None:
self.name = name
def hello(self) -> str:
return f"hello {self.name}"
def bye(self) -> str:
return f"bye {self.name}"
def shout(self) -> str:
return f"HELLO {self.name.upper()}"
def whisper(self) -> str:
return f"hello {self.name}..."
'''
).lstrip()
def test_default_preserve_class_definition_is_true(self):
splitter = PythonCodeSplitter()
assert splitter.preserve_class_definition is True
def test_class_signature_prepended_to_later_chunks(self, multi_method_class_source):
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=3, preserve_class_definition=True)
chunks = splitter.run(documents=[Document(content=multi_method_class_source)])["documents"]
assert len(chunks) >= 2
for chunk in chunks:
if "Greeter" in (chunk.meta.get("include_classes") or []):
assert "class Greeter" in (chunk.content or "")
def test_disabled_does_not_prepend_class_signature(self, multi_method_class_source):
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=3, preserve_class_definition=False)
chunks = splitter.run(documents=[Document(content=multi_method_class_source)])["documents"]
chunks_with_header = [c for c in chunks if "class Greeter" in (c.content or "")]
assert len(chunks_with_header) == 1
def test_preserve_does_not_duplicate_header_in_original_chunk(self, multi_method_class_source):
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=3, preserve_class_definition=True)
result = splitter.run(documents=[Document(content=multi_method_class_source)])
for chunk in result["documents"]:
assert (chunk.content or "").count("class Greeter") <= 1
def test_preserve_keeps_inheritance_and_metaclass(self):
source = textwrap.dedent(
'''
class Base:
pass
class Meta(type):
pass
class Child(Base, metaclass=Meta):
"""A child class."""
def one(self):
return 1
def two(self):
return 2
def three(self):
return 3
def four(self):
return 4
'''
).lstrip()
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=3, preserve_class_definition=True)
result = splitter.run(documents=[Document(content=source)])
child_chunks = [c for c in result["documents"] if "Child" in (c.meta.get("include_classes") or [])]
assert len(child_chunks) >= 2
for chunk in child_chunks:
assert "class Child(Base, metaclass=Meta):" in (chunk.content or "")
def test_preserve_keeps_decorators_on_class(self):
source = textwrap.dedent(
"""
def reg(cls):
return cls
@reg
class Decorated:
def one(self):
return 1
def two(self):
return 2
def three(self):
return 3
def four(self):
return 4
"""
).lstrip()
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=3, preserve_class_definition=True)
result = splitter.run(documents=[Document(content=source)])
decorated_chunks = [c for c in result["documents"] if "Decorated" in (c.meta.get("include_classes") or [])]
assert len(decorated_chunks) >= 2
for chunk in decorated_chunks:
content = chunk.content or ""
assert "class Decorated" in content
assert "@reg" in content
def test_preserve_handles_multiple_classes(self):
source = textwrap.dedent(
"""
class A:
def a1(self):
return 1
def a2(self):
return 2
def a3(self):
return 3
class B:
def b1(self):
return 1
def b2(self):
return 2
def b3(self):
return 3
"""
).lstrip()
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=3, preserve_class_definition=True)
result = splitter.run(documents=[Document(content=source)])
for chunk in result["documents"]:
content = chunk.content or ""
for cls in chunk.meta.get("include_classes") or []:
assert f"class {cls}" in content
class TestDocstringStripping:
def test_default_keeps_docstrings_in_content(self, class_source):
splitter = PythonCodeSplitter(min_effective_lines=2, max_effective_lines=8)
result = splitter.run(documents=[Document(content=class_source)])
contents = "\n".join(c.content or "" for c in result["documents"])
assert "A circle." in contents
for chunk in result["documents"]:
assert not chunk.meta.get("docstrings")
def test_strip_docstrings_moves_them_to_meta(self):
source = textwrap.dedent(
'''
def foo():
"""Foo docstring."""
return 1
def bar():
"""Bar docstring."""
return 2
'''
).lstrip()
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=10, strip_docstrings=True)
result = splitter.run(documents=[Document(content=source)])
all_docstrings = [d for c in result["documents"] for d in (c.meta.get("docstrings") or [])]
joined_docstrings = " | ".join(all_docstrings)
assert "Foo docstring." in joined_docstrings
assert "Bar docstring." in joined_docstrings
joined_content = "\n".join(c.content or "" for c in result["documents"])
assert "Foo docstring." not in joined_content
assert "Bar docstring." not in joined_content
def test_strip_docstrings_preserves_module_docstring(self):
source = textwrap.dedent(
'''
"""Module-level docstring."""
def foo():
"""Inner."""
return 1
'''
).lstrip()
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=20, strip_docstrings=True)
result = splitter.run(documents=[Document(content=source)])
joined = "\n".join(c.content or "" for c in result["documents"])
assert "Module-level docstring." in joined
def test_strip_class_header_docstring_moves_to_meta(self):
source = textwrap.dedent(
'''
class MyClass:
"""Class-level docstring."""
class_var = 42
def method(self):
return self.class_var
'''
).lstrip()
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=10, strip_docstrings=True)
result = splitter.run(documents=[Document(content=source)])
header_chunks = [c for c in result["documents"] if "class_header" in c.meta.get("unit_kinds", [])]
assert header_chunks
header = header_chunks[0]
assert "Class-level docstring." not in (header.content or "")
assert "Class-level docstring." in " | ".join(header.meta.get("docstrings") or [])
class TestTopLevelStatements:
@pytest.fixture
def rich_module_source(self):
return textwrap.dedent(
'''
"""Utility helpers for the pipeline."""
import os
import sys
from pathlib import Path
MAX_RETRIES = 3
DEFAULT_TIMEOUT = 30.0
LOG_PREFIX = "app"
def process(data):
"""Process data."""
result = data.strip()
return result
def validate(value):
"""Validate a value."""
if value is None:
raise ValueError("value cannot be None")
return True
class Manager:
"""Resource manager."""
def __init__(self):
self.items = []
def add(self, item):
self.items.append(item)
def remove(self, item):
self.items.remove(item)
if __name__ == "__main__":
mgr = Manager()
mgr.add(process("test"))
'''
).lstrip()
@pytest.mark.parametrize("expected_kind", ["statement", "imports", "module_docstring"])
def test_unit_kind_present(self, rich_module_source, expected_kind):
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=50)
result = splitter.run(documents=[Document(content=rich_module_source)])
all_kinds = [k for c in result["documents"] for k in c.meta.get("unit_kinds", [])]
assert expected_kind in all_kinds
def test_first_chunk_contains_preamble_statements(self, rich_module_source):
# max_effective_lines=6 matches the preamble (module docstring + 3 imports + 3
# assignments) so the greedy merger flushes before the first function.
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=6)
result = splitter.run(documents=[Document(content=rich_module_source)])
assert len(result["documents"]) >= 2
first = result["documents"][0]
content = first.content or ""
assert '"""Utility helpers for the pipeline."""' in content
assert "import os" in content
assert "import sys" in content
assert "from pathlib import Path" in content
assert "MAX_RETRIES = 3" in content
assert "DEFAULT_TIMEOUT = 30.0" in content
assert 'LOG_PREFIX = "app"' in content
assert "def process" not in content
assert "def validate" not in content
assert "class Manager" not in content
def test_if_main_produces_statement_unit(self, rich_module_source):
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=50)
result = splitter.run(documents=[Document(content=rich_module_source)])
all_kinds = [k for c in result["documents"] for k in c.meta.get("unit_kinds", [])]
assert "statement" in all_kinds
joined = "\n".join(c.content or "" for c in result["documents"])
assert 'if __name__ == "__main__"' in joined
def test_all_imports_appear_in_output(self, rich_module_source):
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=50)
result = splitter.run(documents=[Document(content=rich_module_source)])
joined = "\n".join(c.content or "" for c in result["documents"])
assert "import os" in joined
assert "import sys" in joined
assert "from pathlib import Path" in joined
def test_module_docstring_text_preserved(self, rich_module_source):
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=50)
result = splitter.run(documents=[Document(content=rich_module_source)])
joined = "\n".join(c.content or "" for c in result["documents"])
assert "Utility helpers for the pipeline." in joined
class TestOversizedFallback:
def test_warns_on_oversized_function(self, oversized_function_source, caplog):
import logging
splitter = PythonCodeSplitter(min_effective_lines=2, max_effective_lines=5, oversized_factor=3)
with caplog.at_level(logging.WARNING):
result = splitter.run(documents=[Document(content=oversized_function_source)])
text = caplog.text.lower()
assert "oversiz" in text or "secondary" in text
assert len(result["documents"]) >= 2
def test_oversized_chunks_marked_with_secondary_split(self, oversized_function_source):
splitter = PythonCodeSplitter(min_effective_lines=2, max_effective_lines=5, oversized_factor=3)
result = splitter.run(documents=[Document(content=oversized_function_source)])
assert [c for c in result["documents"] if c.meta.get("secondary_split")]
def test_oversized_chunks_belong_to_same_function(self, oversized_function_source):
splitter = PythonCodeSplitter(min_effective_lines=2, max_effective_lines=5, oversized_factor=3)
result = splitter.run(documents=[Document(content=oversized_function_source)])
secondary = [c for c in result["documents"] if c.meta.get("secondary_split")]
for chunk in secondary:
assert chunk.meta["start_line"] >= 1
assert chunk.meta["end_line"] <= oversized_function_source.count("\n") + 1
def test_small_function_does_not_trigger_secondary(self, simple_module_source):
splitter = PythonCodeSplitter(min_effective_lines=2, max_effective_lines=50, oversized_factor=3)
result = splitter.run(documents=[Document(content=simple_module_source)])
for chunk in result["documents"]:
assert not chunk.meta.get("secondary_split")
def test_long_lines_count_as_more_effective_lines(self):
long_line_source = (
textwrap.dedent(
"""
def short():
return 1
def longer():
return "{padding}"
"""
)
.lstrip()
.format(padding="x" * 500)
)
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=2, expected_chars_per_line=10)
result = splitter.run(documents=[Document(content=long_line_source)])
chunks_with_short = [c for c in result["documents"] if "def short" in (c.content or "")]
chunks_with_long = [c for c in result["documents"] if "def longer" in (c.content or "")]
assert chunks_with_short and chunks_with_long
assert chunks_with_short[0] is not chunks_with_long[0]
def test_qualified_name_in_meta_for_all_pieces(self, oversized_function_source):
splitter = PythonCodeSplitter(min_effective_lines=2, max_effective_lines=5, oversized_factor=3)
result = splitter.run(documents=[Document(content=oversized_function_source)])
for piece in result["documents"]:
assert piece.meta.get("qualified_name") == "giant"
class TestEdgeCases:
def test_module_with_only_docstring(self):
source = '"""Just a docstring."""\n'
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=5)
result = splitter.run(documents=[Document(content=source)])
assert len(result["documents"]) == 1
assert "Just a docstring." in (result["documents"][0].content or "")
def test_module_with_only_imports(self):
source = "import os\nimport sys\nfrom math import pi\n"
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=5)
result = splitter.run(documents=[Document(content=source)])
joined = "\n".join(c.content or "" for c in result["documents"])
assert "import os" in joined
assert "import sys" in joined
assert "from math import pi" in joined
def test_module_with_only_one_function(self):
source = "def f():\n return 1\n"
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=5)
result = splitter.run(documents=[Document(content=source)])
assert len(result["documents"]) == 1
assert "def f" in (result["documents"][0].content or "")
def test_empty_string_source(self):
# Empty source is valid Python; the splitter must not raise.
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=5)
result = splitter.run(documents=[Document(content="")])
assert isinstance(result["documents"], list)
def test_invalid_syntax_raises_syntax_error(self):
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=5)
doc = Document(content="class Broken(\n pass\n")
with pytest.raises(SyntaxError):
splitter.run(documents=[doc])