Skip to content

gh-148669: Clarify __reduce__() module lookup behavior#148670

Merged
JelleZijlstra merged 3 commits into
python:mainfrom
Viicos:gh-148669
May 12, 2026
Merged

gh-148669: Clarify __reduce__() module lookup behavior#148670
JelleZijlstra merged 3 commits into
python:mainfrom
Viicos:gh-148669

Conversation

@Viicos
Copy link
Copy Markdown
Contributor

@Viicos Viicos commented Apr 17, 2026

Comment thread Doc/library/pickle.rst
The :mod:`!pickle` module differs from :mod:`marshal` in several significant ways:

* :mod:`marshal` cannot be used to serialize user-defined classes and their
instances. :mod:`!pickle` can save and restore class instances transparently,
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.

Why was this removed? It's still accurate.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think the statement was true? It is perfectly fine to pickle an instance which lives in a separate module than the one storing the class definition right?

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.

This refers to cases where the class is no longer accessible at its assigned place. For example, if you pickle an instance of some_mod.ClassA, then do del some_mod.ClassA, you can't unpickle the instance any more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Seems like I deleted too much, we should just keep however the class definition must be importable. But the instance doesn't have to live in the same module of the class:

# module1.py

class Test: ...
# module2.py

import pickle
from module1 import Test

t = Test()
pickle.loads(pickle.dumps(t))  # fine

Copy link
Copy Markdown
Member

@JelleZijlstra JelleZijlstra May 10, 2026

Choose a reason for hiding this comment

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

I think this is referring to cases where the class definition is moved between the pickling and unpickling code. Not a common issue but that can be relevant if e.g. a new library (or stdlib) version moves a class from one file to another, and you try to pickle under one version and unpickle under another.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Indeed that makes sense. I think I misread when -> where.

Comment thread Doc/library/pickle.rst Outdated
Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
@read-the-docs-community
Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #32630732 | 📁 Comparing 0dfae55 against main (2a07ff9)

  🔍 Preview build  

4 files changed
± library/difflib.html
± library/functions.html
± library/pickle.html
± library/tkinter.messagebox.html

@JelleZijlstra JelleZijlstra added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels May 11, 2026
@JelleZijlstra JelleZijlstra merged commit 54a5fd4 into python:main May 12, 2026
40 checks passed
@miss-islington-app
Copy link
Copy Markdown

Thanks @Viicos for the PR, and @JelleZijlstra for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@github-project-automation github-project-automation Bot moved this from Todo to Done in Docs PRs May 12, 2026
@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 12, 2026

GH-149703 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label May 12, 2026
@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 12, 2026

GH-149704 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label May 12, 2026
@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 12, 2026

GH-149705 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label May 12, 2026
JelleZijlstra pushed a commit that referenced this pull request May 12, 2026
…48670) (#149705)

gh-148669: Clarify `__reduce__()` module lookup behavior (GH-148670)
(cherry picked from commit 54a5fd4)

Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
JelleZijlstra pushed a commit that referenced this pull request May 12, 2026
…48670) (#149704)

gh-148669: Clarify `__reduce__()` module lookup behavior (GH-148670)
(cherry picked from commit 54a5fd4)

Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
JelleZijlstra pushed a commit that referenced this pull request May 12, 2026
…48670) (#149703)

gh-148669: Clarify `__reduce__()` module lookup behavior (GH-148670)
(cherry picked from commit 54a5fd4)

Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
@Viicos Viicos deleted the gh-148669 branch May 12, 2026 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip news

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants