Skip to content

Python 3.12.3 -> 3.12.4 breaks use of create_autospec(X, spec_set=True, name="X")  #120732

Closed
@ShapingView

Description

@ShapingView

Bug report

Bug description:

Hello, thanks for the work you do!

My issue: Updating from 3.12.3 to 3.12.4 breaks many elements in my test suite. We use the name= argument to better understand log messages once a test with many objects fails.

This works with 3.12.3, but crashes with 3.12.4. The issue might have been introduced with this fix: 23ba96e

Code example

File autospec_test.py:

import unittest
from unittest.mock import create_autospec

class X: ...

class TestX(unittest.TestCase):
    def test_x(self):
        mock = create_autospec(X, spec_set=True, name="X")


if __name__ == "__main__":
    unittest.main()

Observed behavior

python 3.12.3

python autospec_test.py
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

python 3.12.4

python autospec_test.py
E
======================================================================
ERROR: test_x (__main__.TestX.test_x)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".../autospec_test.py", line 10, in test_x
    mock = create_autospec(X, spec_set=True, name="X")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/unittest/mock.py", line 2823, in create_autospec
    mock.configure_mock(**kwargs)
  File "/usr/local/lib/python3.12/unittest/mock.py", line 650, in configure_mock
    setattr(obj, final, val)
  File "/usr/local/lib/python3.12/unittest/mock.py", line 774, in __setattr__
    raise AttributeError("Mock object has no attribute '%s'" % name)
AttributeError: Mock object has no attribute 'name'

----------------------------------------------------------------------
Ran 1 test in 0.003s

FAILED (errors=1)

CPython versions tested on:

3.12

Operating systems tested on:

Linux, (CI pipelines failed on windows, too – didn't test this example, though.)

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13bugs and security fixes3.14bugs and security fixesstdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions