Skip to content

FIX metadata routing shouldn't deep copy estimator - #33827

Merged
thomasjpfan merged 8 commits into
scikit-learn:mainfrom
adrinjalali:slep6/deepcopy
May 26, 2026
Merged

thomasjpfan merged 8 commits into
scikit-learn:mainfrom
adrinjalali:slep6/deepcopy

Conversation

@adrinjalali

Copy link
Copy Markdown
Member

This came up here: skorch-dev/skorch#1139 (comment)

At some point we changed owner from being a string to the class itself, but forgot that this does a deep copy of the estimator many times. This PR fixes the issue.

cc @BenjaminBossan

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

@adrinjalali adrinjalali added the Metadata Routing all issues related to metadata routing, slep006, sample props label Apr 22, 2026
@adrinjalali adrinjalali added this to the 1.9 milestone Apr 22, 2026

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

LGTM. To be sure, owner being the estimator itself is not actually used currently but will be with #31535, that's correct ?

@adrinjalali

Copy link
Copy Markdown
Member Author

This is already the case, the PR for it specifically was extracted from #31535 into #31534 and merged

@jeremiedbb

Copy link
Copy Markdown
Member

I meant currently we don't leverage the fact that owner is the estimator itself; #31535 will.

@adrinjalali

Copy link
Copy Markdown
Member Author

Comment thread sklearn/utils/_metadata_requests.py Outdated
Comment on lines +354 to +358
# `owner` is a reference to the estimator and is only used by
# `_routing_repr` for display; deep copying it would drag the full
# estimator state (and fail for non-picklable attributes) for no benefit.
new = self.__class__.__new__(self.__class__)
memo[id(self)] = new

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.

At a glance, overriding __deepcopy__ feels like a bad code smell.

If the underlying goal is to have a nicer repr for routing, can the owner's repr be passed directly to MetadataRouter?

MetadataRouter(owner_repr=...)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I did think about this pattern and changing it to passing the repr, but in *SearchCV kind of places, we tend to get the routing objects, then change parameters, and then there might be an issue (which admittedly shouldn't happen), and then the error message would be very misleading.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thinking about this, I agree __deepcopy__ manipulation smells, so I've replaced it with a _clone now. This should be nicer.

Comment thread sklearn/base.py Outdated
new_object = klass(**new_object_params)
try:
new_object._metadata_request = copy.deepcopy(estimator._metadata_request)
new_object._metadata_request = estimator._metadata_request._clone()

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.

should we then call clone(estimator._metadata_request) and have it implement __sklearn_clone__ ? it looks like that what's _clone is reproducing here

Comment thread sklearn/utils/_metadata_requests.py Outdated
Comment on lines +354 to +361
# `owner` is a reference to the estimator and is only used by
# `_routing_repr` for display; deep-copying it would drag the full
# estimator state (and fail for non-picklable attributes) for no benefit.
new = self.__class__.__new__(self.__class__)
new.owner = self.owner
new.method = self.method
new._requests = deepcopy(self._requests)
return new

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.

Can this be the following to avoid self.__class__.__new__?

return MethodMetadataRequest(
    owner=self.owner,
    method=self.method
    requests=deepcopy(self._requests)
)

Same for the other implementations.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

call sites in this file use __sklearn_clone__ instead of calling clone on the object, since base.py imports this file, and if we want to use clone in those call sites, we'd need to do a local import of the function.

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'm okay with both.

@thomasjpfan
thomasjpfan merged commit d07a6d7 into scikit-learn:main May 26, 2026
33 checks passed
@adrinjalali
adrinjalali deleted the slep6/deepcopy branch May 26, 2026 13:15
@adrinjalali adrinjalali added the To backport PR merged in master that need a backport to a release branch defined based on the milestone. label May 26, 2026
@jeremiedbb jeremiedbb mentioned this pull request Jun 1, 2026
16 tasks done
prady0t pushed a commit to prady0t/scikit-learn that referenced this pull request Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Metadata Routing all issues related to metadata routing, slep006, sample props module:utils To backport PR merged in master that need a backport to a release branch defined based on the milestone.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants