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:

Why is this an issue?

By 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.

Mock without autospec

from unittest.mock import Mock

client = Mock()
client.fetsh("id")  # typo is accepted; returns another Mock

Mock with autospec

from unittest.mock import create_autospec

client = create_autospec(ApiClient)
client.fetch("id")  # AttributeError if fetch does not exist on ApiClient

Exceptions

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.

What is the potential impact?

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.

How to fix it

Create the mock from the real collaborator so it mirrors that API:

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.

Code examples

Noncompliant code example

from unittest.mock import Mock

def test_checkout():
    payments = Mock()  # Noncompliant
    checkout(payments, order)
    payments.charge.assert_called_once_with(order.total)

Compliant solution

from unittest.mock import create_autospec

def test_checkout():
    payments = create_autospec(PaymentGateway)
    checkout(payments, order)
    payments.charge.assert_called_once_with(order.total)

Noncompliant code example

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")

Compliant solution

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")

Resources

Documentation

Related rules