python-mock-patching-location

python-mock-patching-location is a skill for Claude Code, Codex from fabioc-aloha/Alex_Skill_Mall. It costs 20 tokens per session (585 once invoked), scanned A, original, MIT.

A guide to placing Python mocks correctly in tests. A mock is a test replacement for a real function or service.

In plain words
What is it for?
Use it when mock.patch appears not to work, especially when one Python file imports a function from another file.
Why use it?
It explains why changing the original function's location may not affect the copy that another module actually uses.

Skill for Claude CodeCodex

Written for no agent in particular: nothing here depends on one.

Good fit Use it when mock.patch appears not to work, especially when one Python file imports a function from another file.

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/fabioc-aloha/alex_skill_mall/python-mock-patching-location
Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Any agent
npx skills add fabioc-aloha/Alex_Skill_Mall --skill python-mock-patching-location
Clone the repo
git clone --depth 1 https://github.com/fabioc-aloha/Alex_Skill_Mall

Made for: Claude Code, Codex.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for python-mock-patching-location

README.md
[![agentmods](https://agentmods.dev/badge/skills/fabioc-aloha/alex_skill_mall/python-mock-patching-location/github.svg)](https://agentmods.dev/skills/fabioc-aloha/alex_skill_mall/python-mock-patching-location)
Your own site
<a href="https://agentmods.dev/skills/fabioc-aloha/alex_skill_mall/python-mock-patching-location"><img src="https://agentmods.dev/badge/skills/fabioc-aloha/alex_skill_mall/python-mock-patching-location/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for python-mock-patching-location

Your own site · 80×15
<a href="https://agentmods.dev/skills/fabioc-aloha/alex_skill_mall/python-mock-patching-location"><img src="https://agentmods.dev/badge/skills/fabioc-aloha/alex_skill_mall/python-mock-patching-location.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 20 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 585 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00020 $0.00585
Opus 5 $0.00010 $0.00293
Sonnet 5 $0.00004 $0.00117
Haiku 4.5 $0.00002 $0.00059

Measured 8d ago against content hash 1d92bfc54b11, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-11, from the pricing page.

Security

Grade A, and why

python-mock-patching-location scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 8d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

plugins/code-quality/python-mock-patching-location/skills/python-mock-patching-location/SKILL.md · 87 lines

How it starts

The opening of the file, as written. The whole thing — 87 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Python Mock Patching Location

Category: Testing Time Saved: 30+ minutes debugging "mock not working" Battle-tested: Yes — ChessCoach, multiple Python projects


The Problem

You're writing a test that needs to mock get_client() from module_a. You patch module_a.get_client. The mock doesn't work — the real function still runs.

Why It Happens

Python's import system creates a reference to the function in the importing module. When module_b does from module_a import get_client, it creates module_b.get_client as a separate reference. Patching the original doesn't affect the copy.

The Rule

Patch where the function is USED, not where it's DEFINED

# module_a.py defines get_client()
# module_b.py imports and calls it

# ❌ WRONG — patches the definition, not the usage
@patch('module_a.get_client')
def test_something(mock_client):
    result = module_b.do_work()  # Still calls real get_client!

# ✅ CORRECT — patches where it's called
@patch('module_b.get_client')
def test_something(mock_client):
    result = module_b.do_work()  # Uses mock

Decision Table

Import Style in Target Module Patch Target
from module_a import func target_module.func
import module_a then module_a.func() module_a.func
from module_a import func as alias target_module.alias

Async Context Manager Trap

MagicMock doesn't support async with by default:

# ❌ FAILS — MagicMock can't handle async context manager
mock_lock = MagicMock()
async with mock_lock:  # TypeError

# ✅ WORKS — proper async mock
class MockAsyncLock:
    async def __aenter__(self):
        return self
    async def __aexit__(self, *args):
        pass

mock_lock = MockAsyncLock()

Verification

Test passes AND you've confirmed the mock was actually called:

@patch('module_b.get_client')
def test_something(mock_client):
    mock_client.return_value = fake_response
    result = module_b.do_work()
    
    mock_client.assert_called_once()  # Verify mock was hit

Read the full file on GitHub · 87 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 8d ago First seen · 87 lines · 20 tokens per session scan A 1d92bfc54b11

Subscribe to this mod's changes

python-mock-patching-location is a skill published in the GitHub repository fabioc-aloha/Alex_Skill_Mall (4 stars, last pushed yesterday), licensed MIT. It adds 20 tokens to every session and 585 once invoked, about $0.0001 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-03.

Related

Other skills, from other repositories

flutter-bloc-state-management

Implement BLoC/Cubit state, events, transitions, and async concurrency in Flutter. Use for BLoC/Cubit feature logic, debounced/cancellable events, state rendering, or bloc tests—not generic widget-only work.

HoangNguyen0403/agent-skills-standard · 52 tokens

unit-test

A Go testing workflow for writing unit tests: small tests that check individual functions or components. It supports table-driven cases, where many inputs and expected results are organised in one test, and subtests.

johnqtcg/awesome-skills · 100 tokens

fuzzing-test

A Go testing guide for generating fuzz tests, which repeatedly try varied inputs to find crashes and unexpected behavior. It first checks whether the code is suitable for fuzzing.

johnqtcg/awesome-skills · 74 tokens

test-component

Write React Testing Library tests for an existing component, following the project's RTL conventions (prefer role/label queries, mock at network boundary, no act()). Invoke when the user asks to test, add tests for, or cover a component.

MuhammadUsmanGM/claude-code-best-practices · 50 tokens

adept-writing-tests

How to write tests in the adept Go codebase — table-driven tests with testify, golden fixtures under testdata/, the cmd/adept e2e harness, temp-dir/HOME isolation, and coverage gates. Apply when adding or changing Go tests here. (matches: /test.go).

itaywol/adeptability · 61 tokens

qt-qml-test-run

Builds and runs Qt Quick Test (qmltestrunner / CTest) for a QML project, then writes a Markdown report. Use for "run qml tests", "run qmltestrunner".

TheQtCompanyRnD/agent-skills · 50 tokens