GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Classes and instances produced by `dataclasses.make_dataclass` are not pickleable · Issue #102103 · python/cpython

Classes and instances produced by dataclasses.make_dataclass are not pickleable #102103

New issue
Copy link
New issue
Copy link
Closed
Closed
Classes and instances produced by dataclasses.make_dataclass are not pickleable#102103
Copy link
Assignees
sobolevn
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@sobolevn
sobolevn
opened on Feb 21, 2023
Issue body actions

Repro:

## Setup

>>> import pickle
>>> import dataclasses
>>> A = dataclasses.make_dataclass('A', [])
>>> @dataclasses.dataclass
... class B: pass
... 

## Correct

>>> pickle.loads(pickle.dumps(B))
<class '__main__.B'>
>>> pickle.loads(pickle.dumps(B()))
B()

## Wrong

>>> pickle.load(pickle.dump(A))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
_pickle.PicklingError: Can't pickle <class 'types.A'>: attribute lookup A on types failed

>>> pickle.loads(pickle.dumps(A()))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
_pickle.PicklingError: Can't pickle <class 'types.A'>: attribute lookup A on types failed

I think that this happens because of this:

>>> A.__module__
'types'
>>> B.__module__
'__main__'

After a manual fix it works as it should:

>>> A.__module__ = '__main__'
>>> pickle.loads(pickle.dumps(A()))
A()

So, I propose to do it for all dataclasses that we create in make_dataclass.

I have a simple PR ready, it is inspired by namedtuple's logic :)

Linked PRs

  • gh-102103: add module argument to dataclasses.make_dataclass #102104
Reacted by Alexander Tikhonov

Activity

  1. sobolevn
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Feb 21, 2023
  2. sobolevn
    self-assigned this
    on Feb 21, 2023
  3. sobolevn
    added a commit that references this issue on Feb 21, 2023

    pythongh-102103: add `module` argument to `dataclasses.make_dataclass`

    58743b1
  4. sobolevn commented on Feb 21, 2023

    @sobolevn
    sobolevn
    on Feb 21, 2023
    MemberAuthor
    More actions

    @Fatal1ty great timing, happy to help! 🙂

    Reacted by Alexander Tikhonov
  5. carljm
    added a commit that references this issue on Mar 11, 2023

    gh-102103: add `module` argument to `dataclasses.make_dataclass` (#10…

    b48be8f
  6. hauntsaninja
    closed this as completedon Mar 11, 2023
  7. iritkatriel
    added a commit that references this issue on Mar 12, 2023

    pythongh-102103: add `module` argument to `dataclasses.make_dataclass` (

    cdf9e3a
  8. endlisnis commented on Mar 18, 2023

    @endlisnis
    endlisnis
    on Mar 18, 2023
    More actions

    Funny timing. I just ran into this bug myself yesterday.

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

Metadata

Metadata

Assignees

  • @sobolevn
    sobolevn

Labels

stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.