derwiki-adroll / mock

Automatically exported from code.google.com/p/mock
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

Confusing error when using spec *and* new_callable arguments to patch #132

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
{{{
>>> from StringIO import StringIO
>>> from mock import *
>>> a = 3
>>> p = patch('__main__.a', spec=True, new_callable=StringIO)
>>> p.start()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "mock.py", line 1305, in __enter__
    new = Klass(**_kwargs)
TypeError: __init__() got an unexpected keyword argument 'spec'
}}}

It would be better for patch to refuse to allow spec / spec_set args with a 
less confusing argument. 

(Or is it useful to be able to pass in a function to new_callable that 
optionally takes spec / spec_set keyword arguments? For this use case should 
other keyword arguments be allowed? Currently using new_callable with arbitrary 
keyword arguments is not allowed.)

Original issue reported on code.google.com by fuzzyman on 22 Jan 2012 at 10:33

GoogleCodeExporter commented 9 years ago
Actually patch can be used with new_callable and arbitrary keyword arguments - 
the keyword arguments are passed to new_callable when the patch is started. 
spec and spec_set are special because they are unlikely to be useful to any 
new_callable *except* a mock. However, one use case maybe to create a wrapper 
function that delegates to a real mock.

Original comment by fuzzyman on 23 Jan 2012 at 1:35

GoogleCodeExporter commented 9 years ago
I don't think the failure message is confusing - I think it's the right error 
message and as the comment above notes there are legitimate reasons to *want* 
to do this so forbidding it would be unhelpful.

Original comment by fuzzyman on 19 Nov 2013 at 3:54