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 — Frozen dataclasses with slots cannot override __getstate__ or __setstate__ · Issue #104035 · python/cpython

Frozen dataclasses with slots cannot override __getstate__ or __setstate__ #104035

New issue
Copy link
New issue
Copy link
Closed
#104044
Closed
Frozen dataclasses with slots cannot override __getstate__ or __setstate__#104035
#104044
Copy link
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

@drhagen
drhagen
opened on May 1, 2023
Issue body actions

You are supposed to be able to override how pickle pickles an object with __getstate__ and __setstate__. However, these methods are ignored in a dataclass when specifying both frozen=True and slots=True.

See this example:

import pickle
from dataclasses import dataclass


@dataclass(frozen=True, slots=True)
class Foo:
    bar: int

    def __getstate__(self):
        print("getstate")
        return {"bar": self.bar}

    def __setstate__(self, state):
        print("setstate")
        object.__setattr__(self, "bar", state["bar"])

b = pickle.dumps(Foo(1))
foo = pickle.loads(b)

The expected "getstate" and "setstate" lines are never printed because the supplied methods are never called. If either frozen or slots is removed, the expected lines are printed.

From the source code, it is pretty clear why this is happening. If frozen and slots are both True, then special versions of __getstate__ and __setstate__ are unconditionally attached to the dataclass. The dataclass decorator should probably treat this way that it does other methods—only add a method if it is not already present.

Linked PRs

  • gh-104035: Do not ignore user-defined __{get,set}state__ in slotted frozen dataclasses #104041
  • [3.11] gh-104035: Do not ignore user-defined __{get,set}state__ in slotted frozen dataclasses (GH-104041) #104044

Activity

  1. drhagen
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on May 1, 2023
  2. ericvsmith commented on May 1, 2023

    @ericvsmith
    ericvsmith
    on May 1, 2023
    Member
    More actions

    It does seem reasonable to ignore __getstate__ and __setstate__ if they're already set. Have you tested this? And in particular, does it break any existing tests?

    Reacted by David Hagen
  3. AlexWaygood
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on May 1, 2023
  4. sobolevn
    added a commit that references this issue on May 1, 2023

    pythongh-104035: Do not ignore user-defined `__{get,set}state__` in s…

    b6a09a4
  5. sobolevn commented on May 1, 2023

    @sobolevn
    sobolevn
    on May 1, 2023
    Member
    More actions

    Oh, I just went for fixing this in #104041 :)

    New logic will allow users to customize __getstate__ and __setstate__ method.
    Thanks a lot, @drhagen for finding this :)

    It is funny that I've spent the whole day optimizing these two in #103032, but I haven't seen this! 👍

    Reacted by David Hagen
  6. carljm
    added a commit that references this issue on May 1, 2023

    gh-104035: Do not ignore user-defined `__{get,set}state__` in slotted…

    99aab61
  7. miss-islington
    added a commit that references this issue on May 1, 2023

    pythongh-104035: Do not ignore user-defined `__{get,set}state__` in s…

    9141fed
  8. carljm
    linked a pull request that will close this issue[3.11] gh-104035: Do not ignore user-defined `__{get,set}state__` in slotted frozen dataclasses (GH-104041) #104044on May 1, 2023
  9. carljm
    added a commit that references this issue on May 1, 2023

    [3.11] gh-104035: Do not ignore user-defined `__{get,set}state__` in …

    0df7c3a
  10. carljm
    closed this as completedon May 1, 2023
  11. carljm
    added a commit that references this issue on May 1, 2023

    Merge branch 'main' into inlinecomp2

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

Metadata

Metadata

Assignees

No one assigned

    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.