Skip to content

Generic type annotation error due to old version of CloudPickle #392

Description

@LachlanStuart

The bundled version of CloudPickle (1.2.2) has an issue with pickling type annotations that subclass from Generic. This bug was fixed in version 1.4.0.

Reproduction code (should be run with Python 3.8 - earlier versions use a different codepath in CloudPickle and may not have the same issue):

from typing import TypeVar, Generic
from lithops import function_executor

T = TypeVar('T')
class MyType(Generic[T]):
    pass

def my_func(foo: MyType[int]):
    return True

fexec = function_executor()
future = fexec.call_async(my_func, [None])
fexec.get_result(future)

Raises exception:

  ...
  File "/action/lithops/worker/jobrunner.py", line 257, in run
  File "/action/lithops/worker/jobrunner.py", line 132, in _unpickle_function
  File "/action/lithops/libs/cloudpickle/cloudpickle.py", line 1323, in _make_skeleton_class
  File "/usr/local/lib/python3.8/typing.py", line 908, in __init_subclass__
TypeError: Cannot inherit from plain Generic

Full logs

For now I have a workaround, which is to add the following line to every file:

from __future__ import annotations

This flag (which won't be activated by default until Python 3.10) makes the Python parser store the type annotations as strings until they're requested by code, which means the above would pickle the string "Generic[T]" instead of an actual Generic[T] class reference.

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