Skip to content

[3.13.0] New logged calls in MagicMock mock_calls attribute #420

Closed

Description

@ofx53

Hi!
Just had some tests fail from which I was looking through mock.mock_calls. Have no seen this documented in the changelogs.

image (3)

image

Was this an intended change?

FYI I have found a workaround for the project's tests, just wanted to notify the repo here directly.

Thanks!
Xavier

Activity

  1. kkroening commented on Mar 21, 2024

    @kkroening

    I ran into a possibly related issue that happens in 3.13.0 but not 3.12.0: mocker.patch.object doesn't seem to fully reset between tests, leading to patches bleeding into unrelated tests AFAICT - at least in certain cases.

    I don't have much detail to add about this at the moment but maybe it's at least a clue.

  2. nicoddemus commented on Mar 21, 2024

    @nicoddemus
    Member

    Hmm seems like an unintended regression.

    I have not been able to reproduce the issue however. Any chance of someone providing a MWE? I would be happy to fix it then and make a new release.

  3. kkroening commented on Mar 21, 2024

    @kkroening

    Here's an isolated example:

    class Class1:
        @staticmethod
        def get():
            return 1
    
    
    class Class2:
        @staticmethod
        def get():
            return 2
    
    
    def test1(mocker):
        def handle_get():
            return 3
    
        mocker.patch.object(Class1, "get", handle_get)
        mocker.patch.object(Class2, "get", handle_get)
    
    
    def test2():
        assert Class2.get() == 2
    python -m venv venv
    venv/bin/activate
    pip install pytest==8.1.1 pytest-mock==3.13.0
    pytest

    Output:

        def test2():
    >       assert Class2.get() == 2
    E       assert 3 == 2
    E        +  where 3 = <function test1.<locals>.handle_get at 0x101950900>()
    E        +    where <function test1.<locals>.handle_get at 0x101950900> = Class2.get
    
    test_thing.py:22: AssertionError
    

    It fails intermittently, so you may need to run it a few times to reproduce the issue. Oddly enough, it only seems to happen when doing two patches: if you try commenting out either patch in the first test, then the test seems to succeed 100% of the time. Very strange.

    (Python 3.11.4)

    Note: I'm not sure if this is related to the original issue that @ofx53 reported, but seems coincident anyways.

  4. nicoddemus commented on Mar 21, 2024

    @nicoddemus
    Member

    Thanks @kkroening!

    I suspected the cause of the problem, but was not able to reproduce because it was working when installed in normal methods, but using staticmethod triggered the problem!

    I will make a new release shortly. 👍

  5. rkechols commented on Mar 21, 2024

    @rkechols

    I've also run into a problem with pytest-mock==3.13.0

    Using kkroening's minimal example, this env runs into the bug:

    $ python --version
    Python 3.10.13
    $ pip freeze | grep pytest
    pytest==7.4.4
    pytest-base-url==2.1.0
    pytest-cov==3.0.0
    pytest-mock==3.13.0

    This environment works fine on the given example:

    $ python --version
    Python 3.10.13
    $ pip freeze | grep pytest
    pytest==7.4.4
    pytest-base-url==2.1.0
    pytest-cov==3.0.0
    pytest-mock==3.12.0

    For anyone looking for a temporary fix until the new release is out, just add ,!=3.13.0 to your version specifier of pytest-mock, something like pytest-mock~=3.12,!=3.13.0 or pytest-mock = "^3.12.0,!=3.13.0" depending on your packaging tool

    @nicoddemus it'd be good if 3.13.0 were also yanked from PyPI

  6. added a commit that references this issue on Mar 21, 2024
    4faf92a
  7. nicoddemus commented on Mar 21, 2024

    @nicoddemus
    Member

    @nicoddemus it'd be good if 3.13.0 were also yanked from PyPI

    In general I understand, but we recently yanked pytest from PyPI and the result was not good, it breaks some workflows (which IMO are not correct anyway, but still).

    I'm preparing a new release right now.

  8. nicoddemus commented on Mar 21, 2024

    @nicoddemus
    Member

    Release 3.14 is out, thanks everyone for the help, and sorry for the regression!

  9. 31 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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