Skip to content

[Download] Fix ResolvedRevision string value after pickle/copy - #4692

Merged
hanouticelina merged 1 commit into
mainfrom
fix-resolved-revision-pickle
Aug 17, 2026
Merged

hanouticelina merged 1 commit into
mainfrom
fix-resolved-revision-pickle

Conversation

@hanouticelina

@hanouticelina hanouticelina commented Aug 17, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #4688.

ResolvedRevision is a str subclass, and pickle/copy rebuild str subclasses through __new__ with only the string value. So unpickling called __new__(cls, "3.0bpw") — the string value landed in the resolved slot, initial fell back to None, and the (immutable) string value silently became "main". The __dict__ restore afterwards put .initial/.resolved back, which is why repr() still looked correct and this was easy to miss.

Adding __reduce__ makes pickle and copy/deepcopy rebuild via __new__(cls, resolved, initial).

Impact

hf_hub_download and snapshot_download were unaffected since they read .resolved. Every other HfApi method uses the string value, so after a round-trip they silently operated on main:

rev = HfApi().resolve_revision("turboderp/Qwen3-0.6B-exl3", revision="3.0bpw")
rev = pickle.loads(pickle.dumps(rev))  # or copy.deepcopy(rev)

before:

>>> str(rev)
'main'
>>> HfApi().model_info("turboderp/Qwen3-0.6B-exl3", revision=rev).sha
'ae608a6054100aa0a60ec63f7cabb45d44a35a3a'  # main
>>> hf_hub_url("turboderp/Qwen3-0.6B-exl3", "config.json", revision=rev)
'.../resolve/main/config.json'

after:

>>> str(rev)
'3.0bpw'
>>> HfApi().model_info("turboderp/Qwen3-0.6B-exl3", revision=rev).sha
'd46a12a64d5a0a9a546d862855c8f390b16b7436'  # 3.0bpw
>>> hf_hub_url("turboderp/Qwen3-0.6B-exl3", "config.json", revision=rev)
'.../resolve/3.0bpw/config.json'

Notes

  • __reduce__ rather than __getnewargs__ (as proposed in Fix ResolvedRevision silently becoming "main" after pickling #4689): the latter widens str.__getnewargs__'s return type from tuple[str] to tuple[str, str | None], which ty check src rejects as an invalid override.
  • Verified on pickle protocols 0-5 plus copy.copy/copy.deepcopy, for both initial="3.0bpw" and initial=None. Protocols 0 and 1 were already fine — they reconstruct through str.__new__ rather than the reduce protocol.

🤖 Generated with Claude Code


Note

Cursor Bugbot is generating a summary for commit f61a575. Configure here.

`str` subclasses are rebuilt through `__new__` with only their string value, so
unpickling passed the string value as `resolved` and let `initial` fall back to
its default, silently turning the string value into "main".
@hanouticelina
hanouticelina marked this pull request as ready for review August 17, 2026 09:00

@hanouticelina hanouticelina left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

✔️ looks good!

@bot-ci-comment

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@hanouticelina
hanouticelina merged commit aa40a65 into main Aug 17, 2026
26 checks passed
@hanouticelina
hanouticelina deleted the fix-resolved-revision-pickle branch August 17, 2026 09:21
@huggingface-hub-bot

Copy link
Copy Markdown
Contributor

This PR has been shipped as part of the v1.28.0 release.

@Wauplin

Wauplin commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

thanks for fixing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ResolvedRevision silently becomes "main" after pickling

2 participants