[issue45756] mock raises exception when using a spec with an attribute that raises exception on access

2022-02-14 Thread hongweipeng


Change by hongweipeng :


--
nosy: +hongweipeng
nosy_count: 5.0 -> 6.0
pull_requests: +29497
pull_request: https://github.com/python/cpython/pull/31348

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45756] mock raises exception when using a spec with an attribute that raises exception on access

2021-12-03 Thread Alex Waygood


Change by Alex Waygood :


--
nosy: +lukasz.langa

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45756] mock raises exception when using a spec with an attribute that raises exception on access

2021-12-03 Thread Nikita Sobolev


Change by Nikita Sobolev :


--
keywords: +patch
nosy: +sobolevn
nosy_count: 3.0 -> 4.0
pull_requests: +28125
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/29901

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45756] mock raises exception when using a spec with an attribute that raises exception on access

2021-11-16 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  added the comment:

This seems to be similar to https://bugs.python.org/issue41768 .

--
nosy: +xtreak

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45756] mock raises exception when using a spec with an attribute that raises exception on access

2021-11-13 Thread Jaap Roes


Jaap Roes  added the comment:

I think I encountered this when trying to mock `requests.Response`:  
https://github.com/psf/requests/issues/5944

--
nosy: +jaap3

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45756] mock raises exception when using a spec with an attribute that raises exception on access

2021-11-08 Thread Kevin Jamieson


New submission from Kevin Jamieson :

In Python 3.8 and later creating a mock with a spec specifying an object 
containing a property that happens to raise an exception when accessed will 
fail, because _mock_add_spec calls getattr() on every attribute of the spec. 
This did not happen in Python 3.6/3.7.

This is likely a fairly unusual scenario (and in the particular case where I 
encountered this I could just use a class instead of an instance for the spec), 
but it was surprising.

For example:

# cat test.py
from unittest import mock

class Foo:
@property
def bar(self) -> str:
raise Exception('xxx')

m = mock.MagicMock(spec=Foo())

# python3.11 test.py
Traceback (most recent call last):
  File "/root/test.py", line 8, in 
m = mock.MagicMock(spec=Foo())
^^
  File "/usr/lib/python3.11/unittest/mock.py", line 2069, in __init__
_safe_super(MagicMixin, self).__init__(*args, **kw)
^^^
  File "/usr/lib/python3.11/unittest/mock.py", line 1087, in __init__
_safe_super(CallableMixin, self).__init__(
^^
  File "/usr/lib/python3.11/unittest/mock.py", line 442, in __init__
self._mock_add_spec(spec, spec_set, _spec_as_instance, _eat_self)
^
  File "/usr/lib/python3.11/unittest/mock.py", line 497, in _mock_add_spec
if iscoroutinefunction(getattr(spec, attr, None)):
   ^
  File "/root/test.py", line 6, in bar
raise Exception('xxx')
^^
Exception: xxx

--
messages: 405982
nosy: kjamieson
priority: normal
severity: normal
status: open
title: mock raises exception when using a spec with an attribute that raises 
exception on access
type: behavior
versions: Python 3.10, Python 3.11, Python 3.8, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com