Skip to content

typing.Generic-based type annotations cannot be loaded #196

@marcbllv

Description

@marcbllv

Cloud pickle 0.5.5 now supports pickling of type annotations, but some of them cannot be pickled and loaded. Especially generic types that are based on base class Generic, like Sequence, Tuple, List, ...

Here is the issue:

import typing
import cloudpickle

cloudpickle.loads(cloudpickle.dumps(typing.Sequence[int]))

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/marcbeillevaire/anaconda/envs/test/lib/python3.6/typing.py", line 948, in __new__
    raise TypeError("Cannot inherit from plain Generic")
TypeError: Cannot inherit from plain Generic

Note that this works fine:

import typing
import cloudpickle
cloudpickle.loads(cloudpickle.dumps(typing.Sequence))  # Sequence, not Sequence[int]

It looks like the arguments of __getitem__ get lost, and only the generic class is pickled -- here: Sequence, and not Sequence[int].

This leads to loading a class that inherits from typing.Generic, not typing.Generic[int] which raises the error (see typing.py line 948 where the error above is raised).

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