Skip to content

fix: keep code on the same line as a docstring in PythonCodeSplitter - #13309

Closed
rautaditya2606 wants to merge 1 commit into
deepset-ai:mainfrom
rautaditya2606:fix/python-code-splitter-strip-docstring-same-line
Closed

rautaditya2606 wants to merge 1 commit into
deepset-ai:mainfrom
rautaditya2606:fix/python-code-splitter-strip-docstring-same-line

Conversation

@rautaditya2606

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

_strip_docstring removes the docstring by dropping whole lines, so anything sharing a line with it was dropped too ("""Docs."""; return 5 lost the return 5).

Now it checks the text before and after the docstring on its lines. If there's other code there, it leaves the docstring in place, the same way it already does when the docstring sits on the def line. A trailing comment (like # noqa: D400) doesn't count and is still dropped with the docstring, as before. The column offsets from ast are UTF-8 byte offsets, so the check slices the encoded line.

How did you test it?

Added unit tests in test_python_code_splitter.py: three cases where code shares the docstring's line (function, multi-line docstring, class), and one for the trailing comment. The three code-loss tests fail without the change and pass with it. The trailing comment test passes either way, it just pins the existing behavior.

I ran hatch run test:unit test/components/preprocessors/, hatch run fmt and hatch run test:types, all clean. I only ran them on Python 3.13, not 3.11.

Notes for the reviewer

The check is small and sits right after the existing "docstring shares a line with the def" guard. I chose to keep the docstring in the chunk (and out of meta["docstrings"]) in this case instead of splitting the line, to keep the line-based stripping simple.

This PR was generated with an AI assistant (Claude Code). The relevant tests, hatch run fmt and hatch run test:types were run.

Checklist

🤖 Generated with Claude Code

@vercel

vercel Bot commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

@rautaditya2606 is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@rautaditya2606
rautaditya2606 marked this pull request as ready for review October 17, 2026 15:47
@rautaditya2606
rautaditya2606 requested a review from a team as a code owner October 17, 2026 15:47
@rautaditya2606
rautaditya2606 requested review from bogdankostic and a balanced review from Copilot and removed request for a team October 17, 2026 15:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/preprocessors
  python_code_splitter.py 198
Project Total  

This report was generated by python-coverage-comment-action

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants