This rule raises an issue when a unittest.mock.Mock or MagicMock is created without an autospec (or equivalent
spec / spec_set), including patch / pytest-mock calls that omit autospec=True.
In Python, this includes:
Mock() / MagicMock() without spec / spec_setpatch("target") / patch.object(...) without autospec=True (or an explicit spec)mocker.patch(...) calls from pytest-mockBy default, unittest.mock.Mock accepts any attribute name and any call signature. That hides mistakes: typos on methods under test,
wrong argument lists, and misspelled assertion helpers all succeed until something else fails later.
Autospeccing means building the mock from a real object or class so that it mirrors that API. Missing attributes raise AttributeError,
and calls are checked against the real signature. That turns silent mock mistakes into loud test failures.
from unittest.mock import Mock
client = Mock()
client.fetsh("id") # typo is accepted; returns another Mock
from unittest.mock import create_autospec
client = create_autospec(ApiClient)
client.fetch("id") # AttributeError if fetch does not exist on ApiClient
This rule does not raise when spec, spec_set, autospec, or create_autospec is already used.
A bare Mock can be acceptable when there is no useful real collaborator to copy — for example a throwaway callable stub, or a test
double invented only for this test with no production type. In those cases, review manually before changing the code; prefer autospec whenever a real
class or function exists.
Without autospec, tests can pass while still exercising incorrect interactions with dependencies. API renames and signature changes may go unnoticed, and assertion typos on the mock can pass for the same reason.
Create the mock from the real collaborator so it mirrors that API:
create_autospec(SomeClass) or create_autospec(some_function) when you hold the mock in a variable.patch("module.target", autospec=True) (or mocker.patch(…, autospec=True)) when replacing an import path for the
duration of the test.spec= / spec_set= on Mock / MagicMock when you need a limited mirror of an instance
without full autospec behaviour; prefer autospec / create_autospec when you want signature checking as well.Enabling autospec may cause existing tests to fail if they called methods that do not exist on the real API or used the wrong signature. That failure is intended: update the test (or the call under test) to match the real collaborator. Production behaviour should stay the same; only the test double becomes stricter.
Use a plain Mock only when there is no useful real API to copy. If the bare mock is intentional for that reason, treat the issue as an
accepted trade-off after review rather than a false positive of the analysis.
from unittest.mock import Mock
def test_checkout():
payments = Mock() # Noncompliant
checkout(payments, order)
payments.charge.assert_called_once_with(order.total)
from unittest.mock import create_autospec
def test_checkout():
payments = create_autospec(PaymentGateway)
checkout(payments, order)
payments.charge.assert_called_once_with(order.total)
from unittest.mock import patch
def test_notify():
with patch("app.notify.send") as send: # Noncompliant
notify_user("u1")
send.assert_called_once_with("u1")
from unittest.mock import patch
def test_notify():
with patch("app.notify.send", autospec=True) as send:
notify_user("u1")
send.assert_called_once_with("u1")
unittest.mock.create_autospecunittest.mock.patch