Skip to content

fix: preserve dict subclass types in _deepcopy_with_exceptions - #13097

Draft
Jah-yee wants to merge 1 commit into
deepset-ai:mainfrom
Jah-yee:fix/dict-subclass-copy
Draft

Jah-yee wants to merge 1 commit into
deepset-ai:mainfrom
Jah-yee:fix/dict-subclass-copy

Conversation

@Jah-yee

@Jah-yee Jah-yee commented Oct 3, 2026

Copy link
Copy Markdown

Summary

When copying dict subclasses (defaultdict, OrderedDict, Counter), the _deepcopy_with_exceptions function was returning a plain dict instead of preserving the type.

Bug

  • defaultdict loses its default_factory
  • OrderedDict becomes a regular dict
  • Counter becomes a regular dict

Fix

The fix checks if the dict is a plain dict (type is dict) and uses the fast path. For subclasses, it creates a shallow copy, clears it, and repopulates with deep-copied values, preserving the subclass type.

if isinstance(obj, dict):
    if type(obj) is dict:
        return {k: _deepcopy_with_exceptions(v) for k, v in obj.items()}
    try:
        new = copy.copy(obj)
        new.clear()
        for k, v in obj.items():
            new[k] = _deepcopy_with_exceptions(v)
        return new
    except Exception:
        return {k: _deepcopy_with_exceptions(v) for k, v in obj.items()}
``"

Fixes #13092

When copying dict subclasses (defaultdict, OrderedDict, Counter),
the function was returning a plain dict instead of preserving the type.
A defaultdict would lose its default_factory, and an OrderedDict
would become a regular dict.

The fix checks if the dict is a plain dict (type is dict) and uses
the fast path. For subclasses, it creates a shallow copy, clears it,
and repopulates with deep-copied values, preserving the subclass type.
@Jah-yee
Jah-yee requested a review from a team as a code owner October 3, 2026 14:30
@Jah-yee
Jah-yee requested review from anakin87 and removed request for a team October 3, 2026 14:30
@vercel

vercel Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

@Jah-yee is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@HaystackBot

Copy link
Copy Markdown
Contributor

Hi @Jah-yee, 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 3, 2026 15:55
@HaystackBot HaystackBot added the cla-pending PR is in draft until the contributor signs the CLA label Oct 3, 2026
@HaystackBot
HaystackBot marked this pull request as draft October 3, 2026 15:55

This branch has not been deployed

No deployments
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:core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants