-
-
Notifications
You must be signed in to change notification settings - Fork 32k
doc: PropertyMock refuses to raise AttributeErrror as a side effect #65453
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
Comments
What steps will reproduce the problem?
What is the expected output? What do you see instead? I would expect the above to raise an AttributeError. Instead it returns a MagicMock instance. >>> a_mock.property
<MagicMock name='mock.property' id='140165240345424'> I would expect it to have the same effect as calling a PropertyMock with any other exception as a side effect: >>> mock_value_error = mock.PropertyMock(side_effect=ValueError)
>>> type(a_mock).other_property = mock_value_error
>>> a_mock.other_property
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ahammel/bin/python/mock-1.0.1-py2.6.egg/mock.py", line 2365, in __get__
return self()
File "/home/ahammel/bin/python/mock-1.0.1-py2.6.egg/mock.py", line 955, in __call__
return _mock_self._mock_call(*args, **kwargs)
File "/home/ahammel/bin/python/mock-1.0.1-py2.6.egg/mock.py", line 1010, in _mock_call
raise effect
ValueError What version of the product are you using? On what operating system? Using version mock-1.0.1-py2.6 on CentOS 6.4 Please provide any additional information below. PropertyMock objects apparently won't raise sublcasses of AttributeError either: >>> class MockAttributeError(AttributeError): pass
...
>>> no_attr = mock.PropertyMock(side_effect=MockAttributeError)
>>> type(a_mock).property = no_attr
>>> a_mock.property
<MagicMock name='mock.property' id='140165240345424'> Works fine for subclasses of other Exceptions: >>> class MockKeyError(KeyError): pass
...
>>> no_key = mock.PropertyMock(side_effect=MockKeyError)
>>> type(a_mock).property = no_key
>>> a_mock.property
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ahammel/bin/python/mock-1.0.1-py2.6.egg/mock.py", line 2365, in __get__
return self()
File "/home/ahammel/bin/python/mock-1.0.1-py2.6.egg/mock.py", line 955, in __call__
return _mock_self._mock_call(*args, **kwargs)
File "/home/ahammel/bin/python/mock-1.0.1-py2.6.egg/mock.py", line 1010, in _mock_call
raise effect
__main__.MockKeyError |
Perhaps related to bpo-1615? |
We're poking at this at the kiwipycon sprints |
This is in fact, working entirely as expected. If we define a property that raises an AttributeError, then __getattr__() will be called, since the descriptor protocol says that an attribute error is a missing descriptor. >>> class Foo(object):
... def __getattr__(self, attr):
... return 42
... @property
... def bacon(self):
... print(1)
... return int.lala
...
>>> Foo().bacon
1
42 If we then follow a similar pattern using mock: >>> from unittest import mock
>>> a_mock = mock.MagicMock()
>>> def foo():
... print(1)
... raise AttributeError()
...
>>> no_attribute = mock.PropertyMock(side_effect=foo)
>>> type(a_mock).property = no_attribute
>>> a_mock.property
1
<MagicMock name='mock.property' id='139971099507232'> You can see that the method is called, since we print one, but then what is going on is that MagicMock.__getattr__ is called, which has the behavior to return a new mock, like so: >>> a_mock.b
<MagicMock name='mock.b' id='139971099646776'> |
I could imagine doing some complex things to let you get at the AttributeError in this case but *not* when a different descriptor was added to type(a_mock), but I think it would confusing overall. This might be worth a doc tweak to cover it? |
Problem still exists in 3.14. I'm updating the docs on EuroPython 24 sprints. |
Fixed at EuroPython 24 sprints.
…-121666) Fixed at EuroPython 24 sprints.
…ck (pythonGH-121666) Fixed at EuroPython 24 sprints. (cherry picked from commit 94e6644) Co-authored-by: Vlastimil Zíma <[email protected]>
…ck (pythonGH-121666) Fixed at EuroPython 24 sprints. (cherry picked from commit 94e6644) Co-authored-by: Vlastimil Zíma <[email protected]>
…ock (GH-121666) (GH-121968) Fixed at EuroPython 24 sprints. (cherry picked from commit 94e6644) Co-authored-by: Vlastimil Zíma <[email protected]>
…ock (GH-121666) (GH-121969) Fixed at EuroPython 24 sprints. (cherry picked from commit 94e6644) Co-authored-by: Vlastimil Zíma <[email protected]>
Thank you for the fix! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: