python-module-to-package-refactor

Update unittest.mock.patch targets to runtime lookup paths during Python module-to-package refactors.

Updated Aug 23, 2026
One-click install
npx skills add https://github.com/shimo4228/claude-code-learned-skills --skill python-module-to-package-refactor
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-module-to-package-refactor
Source: https://github.com/shimo4228/claude-code-learned-skills/tree/main/skills/python-module-to-package-refactor
Command: npx skills add https://github.com/shimo4228/claude-code-learned-skills --skill python-module-to-package-refactor

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Splitting a large Python module into a package often breaks unit tests because unittest.mock.patch targets still refer to the old flat module path. This skill provides clear rules to update patch targets so tests mock the correct lookup path at runtime, preserving test reliability during refactors.

Core Features & Use Cases

  • Update patch targets to reflect the actual lookup path after refactoring the module into a package.
  • Provide a bottom-up migration checklist for creating the package structure with init.py re-exports.
  • Ensure test compatibility by validating patch paths across multiple modules and test suites.

Quick Start

Split the module into a package and update all patch targets to reflect where names are looked up at runtime.

Frequently Asked Questions about python-module-to-package-refactor

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
Why do my unittest.mock.patch targets break after splitting a Python module into a package?

When splitting a Python module into a package, unittest.mock.patch targets break because they still refer to the old flat module path instead of the actual runtime lookup path. You must update patch targets to reflect where names are now looked up within the restructured package.

How do I update mock.patch targets when refactoring a Python module into a package?

To update mock.patch targets during a module-to-package refactor, change the patch paths to point to the actual module attribute location where the name is looked up at runtime. This ensures patches resolve correctly after restructuring.

What is the best way to migrate a large Python module into a package without breaking tests?

The best way to migrate a large Python module into a package without breaking tests is using a bottom-up migration approach. Create the package structure with __init__.py re-exports and comprehensively verify patch paths across all test suites.

Do I need to update __init__.py re-exports when refactoring a Python module into a package?

Yes, you need to wire re-exports in __init__.py when refactoring a Python module into a package. This preserves test compatibility and ensures existing imports resolve correctly while you update patch targets across multiple modules.

Can I use a bottom-up migration approach for Python package refactors with multiple interdependencies?

Yes, a bottom-up migration approach works for Python package refactors with multiple interdependencies. It enforces creating the package structure first, wiring __init__.py re-exports, and then validating patch paths across test suites to preserve behavior.

What happens if patch paths do not resolve to the correct module attributes after a package refactor?

If patch paths do not resolve to the correct module attributes after a package refactor, unit tests will fail because mocks target the wrong lookup path. Comprehensive patch-path verification across all tests is required to preserve test reliability.