Skip to content

Fix Python 3.14 update_wrapper pickling with lazy annotations#594

Open
BioGeek wants to merge 1 commit into
cloudpipe:masterfrom
BioGeek:python3.14
Open

Fix Python 3.14 update_wrapper pickling with lazy annotations#594
BioGeek wants to merge 1 commit into
cloudpipe:masterfrom
BioGeek:python3.14

Conversation

@BioGeek

@BioGeek BioGeek commented Jun 8, 2026

Copy link
Copy Markdown

Fixes #585.

Python 3.14 adds __annotate__ to functools.WRAPPER_ASSIGNMENTS, so functools.update_wrapper can copy a wrapped callable’s lazy annotation function onto wrapper instances. When the wrapped callable is a method on an annotated ABC, that copied __annotate__ can close over the class dict and reach _abc_impl, causing:

TypeError: cannot pickle '_abc._abc_data' object

This PR detects the specific redundant __annotate__ copied from __wrapped__ and removes it from the object reduction state before pickling.

This PR detects the redundant __annotate__ copied by functools.update_wrapper onto wrapper instance state and removes it before pickling. The detection does not rely on object identity with __wrapped__.__annotate__, so it also covers CPython 3.14.6 builds where the copied annotation function can be distinct from the currently exposed wrapped annotation function.

Changes

  • Strip copied lazy-annotation __annotate__ functions from wrapper instance state.
  • Keep the check scoped to function-valued __annotate__ attributes on objects with __wrapped__.
  • Add a stdlib-only regression test using abc and `functools.update_wrapper.
  • Extend the regression test to simulate a copied-but-distinct __annotate__ function.
  • Add a changelog entry.

Validation

Tested the issue-comment reproducer from #585 on CPython 3.14.4, 3.14.6 and 3.15.0a7.

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.

TypeError: cannot pickle '_abc._abc_data' object with python 3.14 and cloudpickle 3.1.2

1 participant