Skip to content

fix: align PythonCodeSplitter line splits with ast - #13301

Merged
anakin87 merged 3 commits into
deepset-ai:mainfrom
Eryx-Z:fix/python-code-splitter-form-feed
Oct 19, 2026
Merged

anakin87 merged 3 commits into
deepset-ai:mainfrom
Eryx-Z:fix/python-code-splitter-form-feed

Conversation

@Eryx-Z

@Eryx-Z Eryx-Z commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

PythonCodeSplitter built its line list with str.splitlines(keepends=True), which also breaks on form feed and other Unicode separators. ast line numbers only use \n / \r\n / \r, so a form feed shifted every later unit by one line (functions split across chunks, wrong start_line / end_line).

Split with io.StringIO(source, newline="").readlines() so the line list matches ast.

How did you test it?

Added test_form_feed_does_not_shift_ast_line_numbers and ran TestEdgeCases.

Notes for the reviewer

Suggested approach from the issue report.

Checklist

@Eryx-Z
Eryx-Z requested a review from a team as a code owner October 16, 2026 20:49
@Eryx-Z
Eryx-Z requested review from anakin87 and removed request for a team October 16, 2026 20:49
@vercel

vercel Bot commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

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

A member of the Team first needs to authorize it.

@CLAassistant

CLAassistant commented Oct 16, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@HaystackBot

Copy link
Copy Markdown
Contributor

Hi @Eryx-Z, thanks a lot for your contribution! 🙏

We noticed that the Contributor License Agreement (CLA) check (license/cla) hasn't passed yet, so we've temporarily moved this PR to draft and paused the review assignment.

To get your PR reviewed, please sign the CLA via the link in the license/cla check below (or in the CLA bot comment). As soon as the check turns green, this PR will automatically be marked ready for review again and a reviewer will be re-assigned.

@HaystackBot
HaystackBot removed the request for review from anakin87 October 16, 2026 21:51
@HaystackBot HaystackBot added the cla-pending PR is in draft until the contributor signs the CLA label Oct 16, 2026
@HaystackBot
HaystackBot marked this pull request as draft October 16, 2026 21:51
@Eryx-Z
Eryx-Z marked this pull request as ready for review October 17, 2026 06:10
str.splitlines() also breaks on form feed and other Unicode separators,
but ast line numbers only use \\n / \\r\\n / \\r. Sources with a form feed
then sliced units one line early. Split with io.StringIO(..., newline="")
so chunks and start_line / end_line stay correct.

Fixes deepset-ai#13299
@Eryx-Z
Eryx-Z force-pushed the fix/python-code-splitter-form-feed branch from d4b22fe to 7ece48c Compare October 17, 2026 06:18
@HaystackBot
HaystackBot requested a review from anakin87 October 17, 2026 06:40
@HaystackBot

Copy link
Copy Markdown
Contributor

Thanks for signing the CLA, @Eryx-Z! 🎉 This PR is now ready for review again and the reviewer has been re-assigned.

@HaystackBot HaystackBot removed the cla-pending PR is in draft until the contributor signs the CLA label Oct 17, 2026
@0xamlab

0xamlab commented Oct 17, 2026

Copy link
Copy Markdown

Verified locally: the new test_form_feed_does_not_shift_ast_line_numbers passes, and on main the same source splits first() across chunks because the form feed shifts the line list away from ast. Switching source.splitlines(keepends=True) to io.StringIO(source, newline="").readlines() at haystack/components/preprocessors/python_code_splitter.py:326 keeps the line indices aligned with ast line numbers. The regression test confirms both functions stay intact after the change.

@anakin87 anakin87 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you!

I pushed a few minor simplifications

@anakin87
anakin87 enabled auto-merge (squash) October 19, 2026 09:11
@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
  haystack/core/pipeline
  pipeline.py
Project Total  

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

@anakin87
anakin87 merged commit 38cc7a6 into deepset-ai:main Oct 19, 2026
22 of 23 checks passed
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 misaligns chunks and line numbers after a form feed (str.splitlines vs ast line numbers)

5 participants