import typing, cloudpickle
cloudpickle.dumps(typing.Optional[int])
gives:
_pickle.PicklingError: Can't pickle typing.Union[int, NoneType]: it's not the same object as typing.Union
While pickling annotations directly isn't common, this is a problem when trying to pickle a class with annotations whose __module__ == '__main__'. For example, this fails with the same error:
import typing, cloudpickle
class C:
prop: typing.Optional[int]
cloudpickle.dumps(C)