SKY-L024 · Logic and resilience
Stale mock target
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
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
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""""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)"""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
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"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
- Runs with
--qualityon Python test files whose name starts withtest_orconftest. Files named*_test.pyare not checked. - Looks at calls to
patch(...)or<something>.patch(...)whose first argument is a string literal. Skipspatch.object(...)and any call withcreate=True. - 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.pyor.git. Looks for the module asa/b.pyora/b/__init__.pyunder that root, with exact letter case. A module it cannot find there (an installed package, asrc/layout, a class attribute such asapp.payments.Client.send) is not checked. - Collects the names the module binds: functions, classes, imports and assignments, also inside
if,try,forandwithblocks, but not inside function or class bodies. Stays quiet when the module has a module-level__getattr__, afrom … import *,exec()or writes throughglobals(), since any name could then exist. - Reports HIGH on the
patchcall 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.
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.