Skip to content

AttributeError when pickling generic type annotations on Python 3.6 #347

Description

@pjwerneck

I understand that type annotations are not supported for Python 3.6, but I'm getting an AttributeError for abc._get_dump when pickling generic type annotations.

  • Python 3.6.8
  • cloudpickle 1.3.0:
 File "/home/pedro/.virtualenvs/KZG8XYVK/lib/python3.6/site-packages/cloudpickle/cloudpickle.py", line 899, in save_global
self.save_dynamic_class(obj)
File "/home/pedro/.virtualenvs/KZG8XYVK/lib/python3.6/site-packages/cloudpickle/cloudpickle.py", line 642, in save_dynamic_class
(registry, _, _, _) = abc._get_dump(obj)
AttributeError: module 'abc' has no attribute '_get_dump'

Here's a minimal example to reproduce the error:

importtypingimportcloudpickleX=typing.TypeVar("X")
classInput(typing.Generic[X]):
passclassLero:
a: Input[int]
obj=Lero()
cloudpickle.dumps(obj)

My workaround for it was patching the abc module with a copy of the pure Python implementation of _get_dump at https://github.com/python/cpython/blob/3.7/Lib/test/libregrtest/refleak.py#L8

try:
importabcabc._get_dumpexceptAttributeError:
importweakrefdef_get_dump(cls):
# Reimplement _get_dump() for pure-Python implementation of# the abc module (Lib/_py_abc.py)registry_weakrefs=set(weakref.ref(obj) forobjincls._abc_registry)
return (registry_weakrefs, cls._abc_cache,
cls._abc_negative_cache, cls._abc_negative_cache_version)
abc._get_dump=_get_dump

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions