cloudpipe / cloudpipe/cloudpickle

`__init__` and `__call__` methods are not cloudpickled

Open
#464 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
1.9k
Forks
195
Avg merge
1d 10h
Merged PRs (30d)
1

Description

Overwriting the logic inside `__call__` method of the wrapper:
```
import cloudpickle
import wrapt
import pandas as pd

def _num_of_rows_wrapper() -> Any:
class FunctionWrapper(wrapt.FunctionWrapper):
def __call__(self, *args: Any, **kwargs: Any) -> Any:
dataframe = super(FunctionWrapper, self).__call__(*args, **kwargs)
num_of_rows = len(dataframe.index)
print(f"Num of rows: {num_of_rows}")

return dataframe

return FunctionWrapper

def num_of_rows():
@wrapt.decorator(proxy=_num_of_rows_wrapper())
def wrapper(wrapped, instance, args, kwargs) -> None:
return wrapped(*args, **kwargs)

return wrapper

@num_of_rows()
def prepare_data():
data = [["id1", 10], ["id2", 15], ["id3", 14]]
pandas_df = pd.DataFrame(data, columns=["id", "value"])
return pandas_df
```
Running the decorated function works as expected (num of rows printed):
```
prepare_data()
```
Once it is cloudpickled and loaded, `__call__` gets lost (num of rows is not printed)
```
dump = cloudpickle.dumps(prepare_data)
loaded = cloudpickle.loads(dump)
loaded()
```
Same happens for the `__init__` . Any suggestion for keeping those functions with cloudpickle?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.