Skip to content

Support for objects as return_value when patching #133

@laserprec

Description

@laserprec

Is it possible to patch an object and set its return_value to another object?

For example let's say a Jupyter Notebook defines a method that downloads some CSV from the internet and load it in as a pandas.DataFrame.

import pandas as pd
import requests

def load_csv_from_internet():
    csv_file = requests.get(....) # fetch some CSV from the internet
    ...
    return pd.read_csv(csv_file) # read it and return as pandas.DataFrame

When writing a unit test for such method, I would like to avoid the making the external call and probably patch the return_value of load_csv_from_internet() to some fake pandas.DataFrame I pre-computed. For example:

#setup a fake pandas.DataFrame
mock_df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)))
# patching the `load_csv_from_internet` method from the Notebook
with tb.patch('__main__.load_csv_from_internet', return_value=mock_df) as mock_data:
    .... # some assertion check

Unfortunately, at the moment when assigning the return_value to an object, the object is casted to string type leading to the following error:

from unittest.mock import patch
E                           _patcher_yrvzguhxnm = patch(
E                               "__main__.load_csv_from_internet",
E                               **{"return_value": "     0   1   2   3
E               0    2  66  60  22
E               1   87   1  65  54
E               2   95  99   7  20
E               3   45  84  48  66
E               4   85  96  85  23
E               ..  ..  ..  ..  ..
E               95  79  66  35  94
E               96  91  50  43  87
E               97   5  84  14  18
E               98  13  30  23  71
E               99  66  74  83  66
E               
E               [100 rows x 4 columns]"}
E                           )
E                           _mock_iqufzomgiw = _patcher_yrvzguhxnm.start()
E               
E               ------------------
E               
E                 File "<ipython-input-4-6c61a9b5d34f>", line 4
E                   **{"return_value": "     0   1   2   3
E                                                         ^
E               SyntaxError: EOL while scanning string literal
E               
E               SyntaxError: EOL while scanning string literal (<ipython-input-4-6c61a9b5d34f>, line 4)

Is there plan to support returning objects as it is?

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