Skip to content

PythonCodeSplitter with strip_docstrings=True deletes code on the same line as a docstring #13308

Open

Description

@rautaditya2606

Describe the bug
PythonCodeSplitter with strip_docstrings=True removes a docstring by dropping whole source lines. If other code sits on the same line as the docstring, that code gets dropped too. For example """Docs."""; return 5 loses the return 5, and the function is left with an empty body.

Nothing is raised, and the docstring still shows up in meta["docstrings"], so the chunk looks fine unless you compare it to the source.

The cause is in PythonCodeSplitter._strip_docstring, where after = source_lines[ds_end:unit_end] skips the last docstring line completely. The fix for #12330 adds a pass when the docstring is the only statement, but it doesn't cover this case.

Error message
No error from the splitter. The chunk from the example below fails ast.parse with IndentationError: expected an indented block after function definition on line 1.

Expected behavior
Only the docstring is removed. If that can't be done safely, the docstring should stay in the chunk, like the splitter already does when the docstring is on the same line as the def.

Additional context
I found this by reading the code, not in a real project. The style is rare, but the code loss is silent. It also happens with code after the closing quotes of a multi-line docstring ("""Docs\n more."""; x = 1) and with class docstrings ("""Docs."""; y = 2).

To Reproduce

import ast

from haystack import Document
from haystack.components.preprocessors import PythonCodeSplitter

source = 'def f():\n    """Docs."""; return 5\n'

splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=10, strip_docstrings=True)
chunk = splitter.run(documents=[Document(content=source)])["documents"][0]

print(repr(chunk.content))        # 'def f():\n'
print(chunk.meta["docstrings"])   # ['Docs.']
ast.parse(chunk.content)          # IndentationError

System:

  • OS: Linux (Fedora)
  • Haystack version: main (c6c46c6), 3.5.0rc0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions