kaste / mockito-python

Mockito is a spying framework
MIT License
123 stars 12 forks source link

no attributes in dummy objects since 1.3.2 #57

Closed vladak closed 2 years ago

vladak commented 2 years ago

Normally I'd ask about this in Discussions, however they do not seem to be enabled for this project so I am misusing the issue. Since 1.3.2 (including) there has been some change in behavior.

In 1.3.0 everything is fine:

>>> import requests
>>> from mockito import mock
>>> m = mock(spec=requests.Response)
>>> m.status_code
functools.partial(<function remembered_invocation_builder at 0x107096790>, <mockito.mocking.Mock object at 0x1063ebe80>, 'status_code')
>>> 

however in 1.3.2 there was some change:

>>> import requests
>>> from mockito import mock
>>> m = mock(spec=requests.Response)
>>> m.status_code
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/private/tmp/venv/lib/python3.9/site-packages/mockito/mocking.py", line 286, in __getattr__
    raise AttributeError(
AttributeError: 'Dummy' has no attribute 'status_code' configured

which prevents the usual use cases. This is on Python 3.9.13 on macOS however happens also elsewhere.

kaste commented 2 years ago

Yeah. Well that was actually a bug (or surprise) in 1.3.0. mock(spec=request.Response) actually did nothing. It was like calling mock(). (519c9c4) (What was advertised was mock(request.Response).)

Don't know what you're trying to do, but maybe mock({"status_code": 200}, spec=request.Response) is what you're looking for. Or mock(request.Response, strict=False) just to suppress the early throwing.

Aside that, mockito returning functools.partial... is not useful here and leaky. Maybe we should return None for all unconfigured attributes. (At least something falsy.)

kaste commented 2 years ago

I'm closing. Sorry for the unfortunate breaking change 💇‍♂️

vladak commented 2 years ago

I'm closing. Sorry for the unfortunate breaking change 💇‍♂️

No worries and thanks !

kaste commented 2 years ago

Oops, I did not close.