Skip to content

fix: preserve PythonCodeSplitter member decorator boundaries - #13422

Merged
anakin87 merged 3 commits into
deepset-ai:mainfrom
Aurantium-47:fix/python-code-splitter-decorator-boundaries
Nov 5, 2026
Merged

anakin87 merged 3 commits into
deepset-ai:mainfrom
Aurantium-47:fix/python-code-splitter-decorator-boundaries

Conversation

@Aurantium-47

Copy link
Copy Markdown
Contributor

Related Issues

No matching bug report found. The original Python splitter feature is #11354. This is separate from the docstring stripping fix in #13309 and qualified-name metadata work in #11875.

Proposed Changes:

PythonCodeSplitter(preserve_class_definition=True) currently includes the first class member's decorators in the reusable class signature. This duplicates them on that member and copies them onto unrelated later members. For example, a first @property makes the chunk for a later ordinary method look like another property.

Use the first decorator marker as the member boundary. AST expression line numbers alone are insufficient for parenthesized multiline decorators: the expression can begin after the @( line. A single lexical pass maps decorator-expression lines to their opening marker; the same boundary helper is used for class signatures, class headers, members and top-level definitions. Only a logical line's first meaningful @ counts, not matrix multiplication. Newlines are normalized for lexing only; output slices retain original source spelling.

This also keeps parenthesized decorators with their definitions and reports their actual marker in start_line. Ordinary decorator and undecorated metadata is unchanged. This does not redesign the existing bare class-header chunk or promise independently executable chunks.

How did you test it?

All project execution used Hatch, with Python 3.12.14 and a reduced local test environment (not included in this PR).

  • Full test/components/preprocessors/test_python_code_splitter.py: 134 passed, one existing Document-mutation warning; independently rerun with the same result.
  • The 60 added parametrized cases on pristine base 76fc2d3d7492ec243eed40eb939a836f2a8b29da: 46 failed, 14 passed. They cover first methods, async methods, nested classes, outer classes, top-level functions, parenthesized decorators, comments, preservation on/off, LF/CRLF/CR, exact ordered content and source metadata.
  • Independent supplemental checks: 756 cases passed (144 intended differences and 612 identical controls), plus 450 lexical-boundary cases passed. Extraction compatibility across 295 package Python files found no new errors or changes to unit contents and ordinary metadata; the intentionally changed class_signature field was excluded from that compatibility comparison.
  • Targeted Ruff check and format check, targeted mypy with --follow-imports=silent, release-note backtick check, and git diff --check passed.
  • Not run: full pre-commit aggregate (including codespell), full repository tests/type aggregate, network/model integrations, or multiple Python versions. Targeted checks are not a claim that full pre-commit passed.

Notes for the reviewer

This implementation, tests and PR description were generated by AI assistants. A separate AI reviewer reviewed the revised patch and independently reran the checks described above. The account owner has reviewed and understood the changes. Tests were executed by the preparing AI assistant and independent AI reviewer.

The additional token pass has a measurable cost. Five-run median extraction-only measurements for 1,000 synthetic definitions were 9.52 ms → 17.06 ms for plain functions and 13.24 ms → 24.50 ms for decorated functions, approximately 1.8x at that stage. These are not end-to-end throughput measurements. The implementation tokenizes once per input, not once per definition.

The release note advises re-splitting affected sources and documents the parenthesized-decorator start-line correction. CLA signature status has not been verified; no agreement has been accepted on the contributor's behalf.

Checklist

  • Contribution guidelines and code of conduct were read by the preparing AI assistant; this is not a declaration of personal review by the account owner.
  • Related issue update: not applicable; no matching bug report was found and no issue comment was posted.
  • Added unit tests and private-helper docstrings.
  • Used the conventional fix: PR title.
  • Documented the boundary rationale and user-visible change.
  • Added one Reno-generated release note.
  • Full pre-commit hooks run and all issues fixed. Only the targeted checks listed above were run.

@vercel

vercel Bot commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

@Aurantium-47 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 24, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@Aurantium-47
Aurantium-47 marked this pull request as ready for review October 24, 2026 12:29
@Aurantium-47
Aurantium-47 requested a review from a team as a code owner October 24, 2026 12:29
@Aurantium-47
Aurantium-47 requested review from anakin87 and removed request for a team October 24, 2026 12:29
@github-actions github-actions Bot added the type:documentation Improvements on the docs label Nov 4, 2026

@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.

Thanks for working on this!

  1. Please simplify the implementation by starting from the first decorator's AST line and walking backward until we find the @ marker. That should also handle multiline decorators in an efficient way.

  2. Could you also enable "Allow edits by maintainers" on this PR so we can make small refinements if needed?

Walk backward from the first decorator AST line to its opening @ marker instead of tokenizing the whole source.

Cover multiline decorators, unusual whitespace, Unicode expressions and newline variants while preserving exact chunk content and metadata.

@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.

Comment on lines +2 to +14
upgrade:
- |
Re-split Python sources whose first class member is decorated if their chunks were
produced with ``PythonCodeSplitter(preserve_class_definition=True)``. Previously,
the first member's decorators could be copied into other members' chunks. No
configuration changes are required.
fixes:
- |
``PythonCodeSplitter`` now excludes member decorators from the class signature
prepended to separate member chunks, preserving each member's original decorators
instead of duplicating them or applying them to unrelated methods.
Parenthesized multiline decorators are kept with their definitions, and their
start-line metadata points to the opening decorator marker.

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.

Let's simplify the release note

Suggested change
upgrade:
- |
Re-split Python sources whose first class member is decorated if their chunks were
produced with ``PythonCodeSplitter(preserve_class_definition=True)``. Previously,
the first member's decorators could be copied into other members' chunks. No
configuration changes are required.
fixes:
- |
``PythonCodeSplitter`` now excludes member decorators from the class signature
prepended to separate member chunks, preserving each member's original decorators
instead of duplicating them or applying them to unrelated methods.
Parenthesized multiline decorators are kept with their definitions, and their
start-line metadata points to the opening decorator marker.
fixes:
- |
``PythonCodeSplitter`` now keeps decorators with the methods or classes they
belong to, without copying them to other chunks. This also works with
multiline decorators.

Comment on lines +242 to +245
if (
isinstance(first_statement, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef))
and first_statement.decorator_list
):

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.

Could we move the type check into _definition_start and accept ast.stmt, so we don't repeat it in the callers?



class TestDecorators:
@pytest.mark.parametrize(

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.

I'd remove these tests and reduce the new test combinations:

  • a first method with @property, with class preservation on/off
  • a method with a multiline decorator and a comment
  • a top-level function with a multiline decorator, with LF/CRLF/CR

We can check exact chunk content and line metadata without repeating the general metadata checks. I'd put the first test in TestPreserveClassDefinition and the other two in TestDecorators.

Centralize statement type handling in the decorator boundary helper. Replace the broad regression matrix with the requested focused content and line-range checks, and use the concise fixes release note.
@HaystackBot

Copy link
Copy Markdown
Contributor

Hi @Aurantium-47, 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 added the cla-pending PR is in draft until the contributor signs the CLA label Nov 4, 2026
@HaystackBot
HaystackBot marked this pull request as draft November 4, 2026 21:21
@HaystackBot

Copy link
Copy Markdown
Contributor

Hi @Aurantium-47, just a friendly reminder: this PR is still in draft because the Contributor License Agreement (CLA) hasn't been signed yet. We'd love to review your contribution! Please sign the CLA via the link in the license/cla check, and this PR will automatically be marked ready for review.

@github-actions

github-actions Bot commented Nov 5, 2026

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 291
Project Total  

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

@HaystackBot
HaystackBot marked this pull request as ready for review November 5, 2026 08:22
@HaystackBot HaystackBot removed the cla-pending PR is in draft until the contributor signs the CLA label Nov 5, 2026
@HaystackBot

Copy link
Copy Markdown
Contributor

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

@HaystackBot HaystackBot added the cla-pending PR is in draft until the contributor signs the CLA label Nov 5, 2026
@HaystackBot
HaystackBot marked this pull request as draft November 5, 2026 08:27
@anakin87
anakin87 marked this pull request as ready for review November 5, 2026 08:31
@anakin87
anakin87 enabled auto-merge (squash) November 5, 2026 08:31
@anakin87
anakin87 merged commit 731c64a into deepset-ai:main Nov 5, 2026
38 of 39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-pending PR is in draft until the contributor signs the CLA topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants