Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.12.0a7 changes behavior of PropertyMock #103329

Closed
freakboy3742 opened this issue Apr 7, 2023 · 5 comments · Fixed by #103358
Closed

3.12.0a7 changes behavior of PropertyMock #103329

freakboy3742 opened this issue Apr 7, 2023 · 5 comments · Fixed by #103358
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@freakboy3742
Copy link
Contributor

freakboy3742 commented Apr 7, 2023

Bug report

3.12.0a7 changed the behavior of unittest.mock.PropertyMock

Setup:

from unittest.mock import MagicMock, PropertyMock
m = MagicMock()
p1 = PropertyMock(return_value=3)
p2 = PropertyMock(side_effect=ValueError)
type(m).foo = p1
type(m).bar = p2

In Python 3.12.0a6 and earlier, m.foo evaluates as 3, and m.bar raises a ValueError.

In Python 3.12.0a7, m.foo evaluates as 3, and m.bar returns <MagicMock name='mock.bar' id=...>

Your environment

  • CPython versions tested on: 3.12.0a7, 3.12.0a6, 3.11.2, 3.10.10, 3.9.16, 3.8.16
  • Operating system and architecture:
    • macOS Ventura 13.2.1, on an M1 MacBook Pro.
    • GitHub Actions macOS 12 x86_64
    • GitHub Actions Ubuntu 22.04
    • GitHub Actions Windows Server 2022

Linked PRs

@freakboy3742 freakboy3742 added the type-bug An unexpected behavior, bug, or error label Apr 7, 2023
@arhadthedev arhadthedev added the stdlib Python modules in the Lib dir label Apr 7, 2023
@sunmy2019
Copy link
Member

A known on-going failure was introduced in the related components: aa0a73d

See #103272

Can anyone test if it starts here?

@freakboy3742
Copy link
Contributor Author

@sunmy2019 I'm not sure aa0a73d is the cause here (or, at least, it isn't the only thing in play); in my testing, that commit still raises a ValueError - although the presentation is slightly different, as it also includes

>>> m.bar
Traceback (most recent call last):
  File "/Users/rkm/projects/python/cpython/github/Lib/unittest/mock.py", line 2957, in __get__
    return self()
           ^^^^^^
  File "/Users/rkm/projects/python/cpython/github/Lib/unittest/mock.py", line 1128, in __call__
    return self._mock_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rkm/projects/python/cpython/github/Lib/unittest/mock.py", line 1132, in _mock_call
    return self._execute_mock_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rkm/projects/python/cpython/github/Lib/unittest/mock.py", line 1187, in _execute_mock_call
    raise effect
ValueError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/rkm/projects/python/cpython/github/Lib/unittest/mock.py", line 656, in __getattr__
    if name.startswith(('assert', 'assret', 'asert', 'aseert', 'assrt')) or name in _ATTRIB_DENY_LIST:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SystemError: <method 'startswith' of 'str' objects> returned a result with an exception set

whereas 3.11 et al raise:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/unittest/mock.py", line 2932, in __get__
    return self()
           ^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/unittest/mock.py", line 1118, in __call__
    return self._mock_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/unittest/mock.py", line 1122, in _mock_call
    return self._execute_mock_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/unittest/mock.py", line 1177, in _execute_mock_call
    raise effect
ValueError

@sunmy2019
Copy link
Member

sunmy2019 commented Apr 7, 2023

Looks like it was fixed by #103332

With current main, I got the same output as in your python3.11. @freakboy3742

@carljm
Copy link
Member

carljm commented Apr 7, 2023

Before closing this issue, we should add a test for this case that would have failed on the original breaking PR.

@freakboy3742
Copy link
Contributor Author

Confirming this has been fixed by #103332. I've submitted some regression tests in #103358.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants