Skip to content

Failure to pickle with type annotations (version 1.2.2, with repro) #312

@adamklein

Description

@adamklein

Even with #299, there seems to be a corner case being hit in the below repro on version 1.2.2 that gives rise to the following error:

PicklingError: Can't pickle typing.Union[pandas.core.frame.DataFrame, pandas.core.series.Series, numpy.ndarray, str, NoneType]: it's not the same object as typing.Union

Repro is following:

from typing import Optional, Union

import pandas as pd
import numpy as np
import inspect

import cloudpickle


class AddStuff:
    def __init__(self, other: Optional[Union[pd.DataFrame, pd.Series, np.ndarray, str]]=None):
        self._other = other

    def add(self, data):
        return self._other + data
        

def class_to_func(klass):
    sig = inspect.signature(klass.__init__)

    def wrapped(data, *args, **kwargs):
        t = klass(*args, **kwargs)
        return t.add(data)
        
    data_param = inspect.Parameter(
        'data', inspect.Parameter.POSITIONAL_OR_KEYWORD)
        
    new_parameters = [data_param] + list(sig.parameters.values())[1:]
    # ***** uncomment next line out, for things to work *****
    # new_parameters = _remove_annotations(new_parameters)
    
    new_sig = sig.replace(parameters=tuple(new_parameters))
    
    wrapped.__signature__ = new_sig

    return wrapped
    
def _remove_annotations(parameters):
    return list(map(lambda p: p.replace(annotation=inspect.Parameter.empty),
                    parameters))
					
					
df = pd.DataFrame(np.random.rand(2,2))
adder = class_to_func(AddStuff)			

v2 = AddStuff(df).add(df)
v1 = adder(df, df)	

# prints True
print(v1.equals(v2))

# ok
cloudpickle.dumps(AddStuff)   

# blows up! unless we use _remove_annotations
cloudpickle.dumps(adder)	 

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