Skip to main content

Python refactoring CLI tool powered by LibCST

Project description

pycastic

Python refactoring CLI tool powered by LibCST.

Installation

pip install pycastic

Commands

Command Description
pycastic rename ROOT TARGET NEW_NAME Rename a symbol across the codebase
pycastic move ROOT TARGET DEST_FILE Move symbol(s) to another file
pycastic rename-file ROOT FILE NEW_NAME Rename a file and update imports
pycastic move-file ROOT FILE DEST_DIR Move a file and update imports

Target Formats

Format Example Description
file.py::Symbol utils.py::helper Symbol by name
file.py::A,B,C utils.py::foo,bar Multiple symbols
file.py:line:col utils.py:10:5 Symbol at position

Move Command Options

Option Effect
--dry-run / -n Preview changes without applying
--include-deps Auto-include shared dependencies in the move
--shared-file Extract shared deps to default file ({source}_common.py)
--shared-file-path PATH Extract shared deps to specified file

Move Behavior Rules

Scenario Behavior
Symbol uses external imports Imports copied to destination
Symbol uses internal dep (unused elsewhere) Dep moved automatically
Symbol uses internal dep (used elsewhere) Error: use --include-deps or --shared-file
Other files import moved symbol Imports updated to new location
Original file uses moved symbol Import added from new location
Original file has unused imports after move Unused imports removed

Examples

Rename a symbol

# Rename by name
pycastic rename . src/utils.py::old_function new_function

# Rename by position (line:column)
pycastic rename /path/to/project src/module.py:10:5 new_name

Move symbols

# Move a function (auto-includes unused internal dependencies)
pycastic move . src/utils.py::process_data src/processors.py

# Move multiple related functions together
pycastic move . src/utils.py::parse,validate,transform src/parsers.py

# Preview what would happen
pycastic move . src/utils.py::my_func dest.py --dry-run

Handling shared dependencies

When moving a symbol that depends on another symbol in the same file, pycastic checks if that dependency is used by other remaining code:

# If shared dependency detected, you have two options:

# Option 1: Include shared deps in the move
pycastic move . src/utils.py::func_a dest.py --include-deps

# Option 2: Extract shared deps to a common file (default: utils_common.py)
pycastic move . src/utils.py::func_a dest.py --shared-file

# Option 3: Extract shared deps to a specific file
pycastic move . src/utils.py::func_a dest.py --shared-file-path src/common.py

Rename and move files

# Rename a file (updates all imports)
pycastic rename-file . src/old_name.py new_name

# Move a file to a new directory (updates all imports)
pycastic move-file . src/utils.py src/lib/

How It Works

pycastic uses LibCST to parse and transform Python code while preserving formatting. When moving symbols:

  1. Dependency Analysis: Analyzes what imports and internal symbols the target depends on
  2. Smart Resolution: Determines which dependencies should move automatically vs. require user input
  3. Import Management: Updates imports in all affected files
  4. Cleanup: Removes unused imports from the original file

License

MIT

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

pycastic-0.0.2.tar.gz (40.1 kB view details)

Uploaded Source

Built Distribution

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

pycastic-0.0.2-py3-none-any.whl (24.2 kB view details)

Uploaded Python 3

File details

Details for the file pycastic-0.0.2.tar.gz.

File metadata

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

File hashes

Hashes for pycastic-0.0.2.tar.gz
Algorithm Hash digest
SHA256 f3cc125d885a8eb8b6dd4510ce501d7e9f277cca29934e32c308c158404f361e
MD5 6f073a6c5f4f39a4e0ec3b04fbbada3f
BLAKE2b-256 193d184e307bfd259e216919bec60cdd1e8ea4bd7a278f395408142a335c9d5b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycastic-0.0.2.tar.gz:

Publisher: bump-and-release.yml on tssweeney/pycastic

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

File details

Details for the file pycastic-0.0.2-py3-none-any.whl.

File metadata

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

File hashes

Hashes for pycastic-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 edd1220c1ad28f90392985bc2c8f1d742f094c004b9120e719226f48e2a2a41d
MD5 97894180e6bf580a22acf6f8ac63d8d6
BLAKE2b-256 731f82476d0557038db458e3c521a8c967568c5145e6666410c35343c8289285

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycastic-0.0.2-py3-none-any.whl:

Publisher: bump-and-release.yml on tssweeney/pycastic

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