Skip to content

AttributeError when pickling generic type annotations on Python 3.6 #347

@pjwerneck

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:

import typing
import cloudpickle

X = typing.TypeVar("X")

class Input(typing.Generic[X]):
    pass

class Lero:
    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:
    import abc
    abc._get_dump
except AttributeError:
    import weakref

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

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