Skip to main content

Precision import refactoring tool — rewrite, migrate, and sanitize Python imports project-wide with safety and accuracy.

Project description

import-surgeon logo

PyPI version Python Wheel Release

Build status Codecov Test Coverage Code style: black Ruff Security

Downloads OS Python Versions

License: MIT

Docs


import-surgeon 🩺⚙️

Precision Python import refactoring — safe, atomic, AST-exact, rollback-friendly.


🧠 Overview

import-surgeon is an Elite import refactoring engine for Python codebases.

It precisely updates imports and moves symbols across modules without breaking your code, using full LibCST AST guarantees, backup files, atomic writes, and optional auto-formatting.

Think of it as libcst + git-protection + atomic rollback-safe refactor surgery

Use it for:

✅ Package restructures ✅ Module renames ✅ Moving functions/classes between files ✅ Gradual API migrations ✅ Org-wide import cleanup ✅ CI-safe automated refactors

No regex. No AST guessing. No broken imports.


✨ Features

Capability Description

Accurate import rewrites LibCST powered symbol movement (AST-exact) Dotted name rewrites old.module.Foo → new.module.Foo if --rewrite-dotted Atomic file updates Guaranteed atomic writes + metadata restore Auto backup & rollback --no-backup optional; --rollback supported Supports aliases from A import Foo as Bar handled correctly Respects relative imports --force-relative + auto base-package detection Batch migrations YAML config for multi-module migrations Safe in CI --require-clean-git to prevent dirty changes Git auto-commit --auto-commit "msg" Optional format Black + isort applied after changes Warnings for risky spots Wildcards, dotted patterns, skipped rel imports Progress bar tqdm fallback built-in


📦 Installation

pip install import-surgeon

Optional but recommended:

pip install black isort chardet tqdm


🛠 Usage

Basic dry-run (default)

import-surgeon --old-module utils --new-module core.utils --symbols load,save

Apply changes

import-surgeon --old-module old.pkg --new-module new.pkg --symbols Foo,Bar --apply

Rewrite dotted usages too

import-surgeon --apply --rewrite-dotted
--old-module old.mod --new-module new.mod --symbols Client

Use YAML migrations file

migrate.yml

migrations:

  • old_module: old.auth new_module: services.auth symbols: [User, Token]

Run:

import-surgeon --config migrate.yml --apply

Rollback a refactor

import-surgeon --rollback --summary-json summary.json


🧪 Example Output

✔️ New imports inserted ✔️ Old imports removed ✔️ Diff preview in dry-run ✔️ JSON report with file list, change lines & risks


🔒 Safety Guarantees

Dry run by default

File backups by default

Atomic writes (no corruption)

Git-clean check option

Per-file encoding detection

Refuses unsafe wildcard rewrites unless configured


🧠 CLI Options (summary)

Flag Meaning

--old-module Old module to move from --new-module New module to move to --symbols Comma-separated symbol list --apply Actually write changes --no-backup Skip file backups --config FILE YAML batch config --rollback Rollback via summary JSON --rewrite-dotted Rewrite dotted uses too --format Run isort + black --auto-commit Auto-commit in git repo --require-clean-git Ensure repo clean before write


🚦 Exit Codes

Code Meaning

0 Success 1 Changes had errors 2 CLI/config error


📁 Project Structure

src/import_surgeon/ cli.py


🧩 Roadmap

Symbol dependency graph warnings

VSCode / PyCharm integration

Preview UI with selectable edits

Interactive TUI surgeon console

NeoVim LSP hooks


🤝 Contributing

PRs welcome, especially for:

Editor plugins

Safety analyzers

Batch migration assistants

Code mod multi-tool integration


📜 License

MIT — commercial and open use welcome.


⭐ Support

Star the repo — every ⭐ funds more time for DevTools research 🙏

https://github.com/dhruv13x/import-surgeon


🩺 Your imports deserve precision surgery — not blind search-replace. Run import-surgeon and refactor with confidence.


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

import_surgeon-1.0.0.tar.gz (22.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

import_surgeon-1.0.0-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file import_surgeon-1.0.0.tar.gz.

File metadata

  • Download URL: import_surgeon-1.0.0.tar.gz
  • Upload date:
  • Size: 22.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for import_surgeon-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8eedcdfa671bcb24a261eea5b4cc009bec458e20a92e29536e08e7e4c039ca68
MD5 3a9293cd8a58a1309bddc3a521fe167b
BLAKE2b-256 cef1f97f048d2f8508214ebf14e7f71c8e7fc992f52dbfd321cb5103c6225ed7

See more details on using hashes here.

Provenance

The following attestation bundles were made for import_surgeon-1.0.0.tar.gz:

Publisher: publish.yml on dhruv13x/import-surgeon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file import_surgeon-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: import_surgeon-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 16.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for import_surgeon-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c08feb7992c67817ce6555d936ca0b849b0c7f76653c50d021ec8f6089f14403
MD5 e70170daf61e2daa04e427d76ecf68ac
BLAKE2b-256 bab01e8928916535994e4a59fc66f759f85afde6bb427778fb87c3dbbebb44fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for import_surgeon-1.0.0-py3-none-any.whl:

Publisher: publish.yml on dhruv13x/import-surgeon

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page