Skip to main content

SKY-L024 · Logic and resilience

Stale mock target

High

The catalog gives no severity for this rule; the detector always reports HIGH.

mock.patch("app.payments.PaymentClient") replaces a name inside a module for the length of a test. SKY-L024 reports a patch target that names something the module no longer defines or imports, usually because the code was renamed and the test was not.

Languages
Python
Quality gate
counted toward the gate limits (full and diff scans)
Finding type
review
Catalog name
Stale mock
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

mock.patch raises AttributeError for a missing target, so the test breaks on its next run. Worse, the quick way to silence that error, create=True, makes the test patch a name nothing reads, and it then calls the real payment client.

What Skylos flags

These files are committed to a Git repository before the scan.

tests/test_payments.py · PythonReported on line 7
from unittest import mock from app.payments import charge_card  def test_charge_card_returns_charge_id():    with mock.patch("app.payments.PaymentClient") as client_cls:        client_cls.return_value.create_charge.return_value = "ch_123"        assert charge_card("cus_1", 500) == "ch_123"
app/payments.py · Python
"""Card payments through the provider SDK.""" from app.gateway import GatewayClient  def charge_card(customer_id: str, amount_cents: int) -> str:    client = GatewayClient()    return client.create_charge(customer_id, amount_cents)
app/gateway.py · Python
"""Thin wrapper around the payment provider's HTTP API."""  class GatewayClient:    def create_charge(self, customer_id: str, amount_cents: int) -> str:        raise NotImplementedError("network call")

What Skylos leaves alone

These files are committed to a Git repository before the scan.

tests/test_payments.py · Python
from unittest import mock from app.payments import charge_card  def test_charge_card_returns_charge_id():    with mock.patch("app.payments.GatewayClient") as client_cls:        client_cls.return_value.create_charge.return_value = "ch_123"        assert charge_card("cus_1", 500) == "ch_123"

app/payments.py: same as in the flagged example.

app/gateway.py: same as in the flagged example.

Why Skylos leaves it alone

GatewayClient is defined in app/gateway.py, but app.payments imports it, which creates a binding of that name in app.payments. That is the name charge_card looks up, so this is the right place to patch, and Skylos counts the import as a definition.

How Skylos decides

  1. Runs with --quality on Python test files whose name starts with test_ or conftest. Files named *_test.py are not checked.
  2. Looks at calls to patch(...) or <something>.patch(...) whose first argument is a string literal. Skips patch.object(...) and any call with create=True.
  3. Splits the target at its last dot into a module and a name, and finds the project root: the nearest folder above the test with a pyproject.toml, setup.py or .git. Looks for the module as a/b.py or a/b/__init__.py under that root, with exact letter case. A module it cannot find there (an installed package, a src/ layout, a class attribute such as app.payments.Client.send) is not checked.
  4. Collects the names the module binds: functions, classes, imports and assignments, also inside if, try, for and with blocks, but not inside function or class bodies. Stays quiet when the module has a module-level __getattr__, a from … import *, exec() or writes through globals(), since any name could then exist.
  5. Reports HIGH on the patch call when the name is not among those bindings.

How to fix it

Patch the name the code under test really looks up, here the new class name, in the module that uses it.

Fix · Python
with mock.patch("app.payments.GatewayClient") as client_cls:

When it can be wrong

  • A name that only exists at run time, for example one a plugin loader adds with setattr(module, ...) from another file, is reported.
  • Names bound only under if TYPE_CHECKING: count as defined, so a patch of such a name is not reported although it fails at run time.
  • Targets in installed packages and src/-layout projects are never checked.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • Put the marker on the line of the patch(...) call.

    Python
    with mock.patch("app.plugins.Exporter"):  # skylos: ignore[SKY-L024] added by loader
  • [tool.skylos] ignore = ["SKY-L024"] turns the rule off for the project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.