Skip to content

new_callable arg to unittest.mock.patch seems not to support partial #137145

@filip-komarzyniec

Description

@filip-komarzyniec

Bug report

Bug description:

As in the title. The following code seems to disregard the partial and proceed to call the wrapped function right away (instead of calling the wrapping partial object).

from functools import partial
from unittest.mock import patch

def wrapped():
  """an exemplary response from `some.directory.module.my_function` function"""
    return [
        {
            "some_key": "some_value",
        }]

def test_some_func():
    with patch(
        "some.directory.module.my_function", new_callable=partial(wrapped)
    ):

    # calls `my_function` internally
    some.directory.module.another_function()

Running the above will produce: TypeError: 'list' object is not callable.
Acc. to the docs an object passed as new_callable argument should be called by patch to create a new dummy object replacing the target (some.directory.module.my_function). However it seems that instead of partial being called and returning a wrapped function, a wrapped function itself is called returning the list.

CPython versions tested on:

3.11

Operating systems tested on:

macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedstdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions