Skip to content

fix: avoid in-place mutation of ExtractedAnswer in ExtractiveReader#11296

Merged
sjrl merged 2 commits into
deepset-ai:mainfrom
HamidOna:fix/dataclass-inplace-mutation
May 12, 2026
Merged

fix: avoid in-place mutation of ExtractedAnswer in ExtractiveReader#11296
sjrl merged 2 commits into
deepset-ai:mainfrom
HamidOna:fix/dataclass-inplace-mutation

Conversation

@HamidOna
Copy link
Copy Markdown
Contributor

@HamidOna HamidOna commented May 11, 2026

Related Issues

Proposed Changes

ExtractiveReader._add_answer_page_number did answer.meta = {} when the
incoming answer had meta=None. That triggers the in-place mutation warning
added in #10650. Replaced with dataclasses.replace(answer, meta={}).

The earlier sweep in #10659 caught extractive.py:393 but missed line 323,
so this fills that gap.

How did you test it?

Added test_add_answer_page_number_with_none_meta which calls the method
with meta=None inside warnings.simplefilter("error"), so any mutation
warning fails the test. Also ran hatch run test:unit test/components/readers/test_extractive.py
locally.

Notes for the reviewer

While looking around I noticed a few sites that mutate dict contents on
input parameters (e.g. doc.meta["x"] = y). The current warning only catches
attribute reassignment, so these don't fire it:

  • haystack/components/preprocessors/hierarchical_document_splitter.py:78
    mutates document.meta on an input Document
  • haystack/components/generators/chat/hugging_face_api.py:617,688
    mutates message.meta["usage"] on an input ChatMessage
  • haystack/components/audio/whisper_remote.py:155 mutates source.meta
    (This one is probably fine since source is constructed two lines above but just mentioning it)

Not sure if dict-content mutations are in scope for #10564. Happy to open
follow-up PRs if any of these are worth fixing.

Checklist

  • Read contributing guidelines and code of conduct
  • Added unit tests
  • Conventional commit title (fix:)
  • Release note added
  • Ran pre-commit hooks / quality checks locally

@
fix: avoid in-place mutation of ExtractedAnswer in ExtractiveReader

Replace `answer.meta = {}` with `dataclasses.replace(answer, meta={})` in
`_add_answer_page_number` to avoid triggering the dataclass mutation
warning when an incoming answer has `meta=None`.

Follow-up to epic deepset-ai#10564; fills a site missed by the sweep in deepset-ai#10659
(which listed line 393 but not line 323).
@
@HamidOna HamidOna requested a review from a team as a code owner May 11, 2026 19:11
@HamidOna HamidOna requested review from sjrl and removed request for a team May 11, 2026 19:11
@vercel
Copy link
Copy Markdown

vercel Bot commented May 11, 2026

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

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added topic:tests type:documentation Improvements on the docs labels May 11, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 11, 2026

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/readers
  extractive.py
Project Total  

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

Comment thread test/components/readers/test_extractive.py Outdated
@sjrl sjrl self-assigned this May 12, 2026
Copy link
Copy Markdown
Contributor

@sjrl sjrl left a comment

Choose a reason for hiding this comment

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

Thanks!

@sjrl sjrl enabled auto-merge (squash) May 12, 2026 06:37
@sjrl sjrl merged commit 00613a8 into deepset-ai:main May 12, 2026
26 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants