[issue30548] typo in documentation for create_autospec
New submission from Erik Byström: "a class" should most probably be replaced by "an instance" in the documentation for create_autospec. "You can use a class as the spec for an instance object by passing instance=True. The returned mock will only be callable if instances of the mock are callable." https://docs.python.org/3/library/unittest.mock.html#unittest.mock.create_au... ---------- assignee: docs@python components: Documentation messages: 294993 nosy: Erik Byström, docs@python priority: normal severity: normal status: open title: typo in documentation for create_autospec type: enhancement versions: Python 3.6 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue30548> _______________________________________
Mario Corchero <mariocj89@gmail.com> added the comment: I've always understood instance as a way to say "I am passing this class but I want to force the autospec on the instance" For example, given ``` class X: def __init__(self): raise ``` You can do `unittest.mock.create_autospec(X, instance=True)` to set a spec of the instance rather than the class. Also quite often you do autospec on a class but you want the interface of the instance. This parameter allows you to do so. Basically, `unittest.mock.create_autospec(X, instance=True)` will produce a non callable mock. I think the docs are correct, maybe misleading ---------- nosy: +mariocj89 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue30548> _______________________________________
Cheryl Sabella <cheryl.sabella@gmail.com> added the comment: Mario is right that this isn't a typo. Here's a code example to illustrate what he said:
class MyClass: ... a = 3 ... def foo(self): pass ... mock_class = create_autospec(MyClass) mock_class <MagicMock spec='MyClass' id='16678696'> mock_class() <NonCallableMagicMock name='mock()' spec='MyClass' id='16752016'> mock_class.foo <MagicMock name='mock.foo' spec='function' id='16751032'>
mock_instance = create_autospec(MyClass, instance=True) mock_instance <NonCallableMagicMock spec='MyClass' id='16757832'> mock_instance() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'NonCallableMagicMock' object is not callable mock_instance.foo <MagicMock name='mock.foo' spec='function' id='16750024'>
As per the docs, the instance object uses the class as the spec and it isn't callable, whereas the mock class is. Would adding this example to the docs help or would a different code example help make this less misleading? ---------- nosy: +cheryl.sabella _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue30548> _______________________________________
Change by Karthikeyan Singaravelan <tir.karthi@gmail.com>: ---------- nosy: +xtreak _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue30548> _______________________________________
Erik Byström <erik.bystrom@gmail.com> added the comment: Yes, you're right. I do think the docs are a bit misleading. Maybe something like this would make it more clear? "If a class is used as a spec then the returned object will be a mock of that class. When the constructor of the returned mock class is invoked an instance object is returned that has the same spec as a normal instance object would have. By passing instance=True you can directly create a mock instance of the class." If not, feel free to close this issue. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue30548> _______________________________________
participants (4)
-
Cheryl Sabella
-
Erik Byström
-
Karthikeyan Singaravelan
-
Mario Corchero