Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pickle docs are wrong about nested classes #91362

Closed
JelleZijlstra opened this issue Apr 3, 2022 · 1 comment
Closed

pickle docs are wrong about nested classes #91362

JelleZijlstra opened this issue Apr 3, 2022 · 1 comment
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error

Comments

@JelleZijlstra
Copy link
Member

BPO 47206
Nosy @avassalotti, @JelleZijlstra, @maggyero

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2022-04-03.15:10:59.776>
labels = ['3.11', 'type-bug', '3.9', '3.10', 'docs']
title = 'pickle docs are wrong about nested classes'
updated_at = <Date 2022-04-04.16:30:12.057>
user = 'https://github.com/JelleZijlstra'

bugs.python.org fields:

activity = <Date 2022-04-04.16:30:12.057>
actor = 'maggyero'
assignee = 'docs@python'
closed = False
closed_date = None
closer = None
components = ['Documentation']
creation = <Date 2022-04-03.15:10:59.776>
creator = 'JelleZijlstra'
dependencies = []
files = []
hgrepos = []
issue_num = 47206
keywords = []
message_count = 1.0
messages = ['416625']
nosy_count = 4.0
nosy_names = ['alexandre.vassalotti', 'docs@python', 'JelleZijlstra', 'maggyero']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue47206'
versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

@JelleZijlstra
Copy link
Member Author

https://docs.python.org/3.10/library/pickle.html#what-can-be-pickled-and-unpickled says that only "classes that are defined at the top level of a module" can be pickled. But in fact these work fine in current Python, probably since 3.3 when __qualname__ was added (https://docs.python.org/3/library/stdtypes.html#definition.\_\_qualname__). Similarly, the docs claim only top-level functions can be pickled, but in fact methods nested in classes work fine.

Example script demonstrating that these work:

import pickle


class X:
    class Y:
        pass

    def method(self):
        pass


print(pickle.dumps(X.Y))
print(pickle.loads(pickle.dumps(X.Y)))

print(pickle.dumps(X.Y()))
print(pickle.loads(pickle.dumps(X.Y())))

print(pickle.dumps(X.method))
print(pickle.loads(pickle.dumps(X.method)))

@JelleZijlstra JelleZijlstra added 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes labels Apr 3, 2022
@JelleZijlstra JelleZijlstra added 3.10 only security fixes docs Documentation in the Doc dir 3.11 only security fixes type-bug An unexpected behavior, bug, or error labels Apr 3, 2022
@JelleZijlstra JelleZijlstra added docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error labels Apr 3, 2022
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
hauntsaninja added a commit to hauntsaninja/cpython that referenced this issue May 7, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 21, 2022
…honGH-92429)

Fixes pythonGH-91362
(cherry picked from commit f9d6c59)

Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 21, 2022
…honGH-92429)

Fixes pythonGH-91362
(cherry picked from commit f9d6c59)

Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
miss-islington added a commit that referenced this issue May 21, 2022
Fixes GH-91362
(cherry picked from commit f9d6c59)

Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
miss-islington added a commit that referenced this issue May 21, 2022
Fixes GH-91362
(cherry picked from commit f9d6c59)

Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error
Projects
Status: Done
Development

No branches or pull requests

1 participant