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:
- Dependency Analysis: Analyzes what imports and internal symbols the target depends on
- Smart Resolution: Determines which dependencies should move automatically vs. require user input
- Import Management: Updates imports in all affected files
- Cleanup: Removes unused imports from the original file
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f3cc125d885a8eb8b6dd4510ce501d7e9f277cca29934e32c308c158404f361e
|
|
| MD5 |
6f073a6c5f4f39a4e0ec3b04fbbada3f
|
|
| BLAKE2b-256 |
193d184e307bfd259e216919bec60cdd1e8ea4bd7a278f395408142a335c9d5b
|
Provenance
The following attestation bundles were made for pycastic-0.0.2.tar.gz:
Publisher:
bump-and-release.yml on tssweeney/pycastic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pycastic-0.0.2.tar.gz -
Subject digest:
f3cc125d885a8eb8b6dd4510ce501d7e9f277cca29934e32c308c158404f361e - Sigstore transparency entry: 819830347
- Sigstore integration time:
-
Permalink:
tssweeney/pycastic@ed5cf46119d0488430f1bca0440f44b0886d6fc9 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/tssweeney
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
bump-and-release.yml@ed5cf46119d0488430f1bca0440f44b0886d6fc9 -
Trigger Event:
workflow_dispatch
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
edd1220c1ad28f90392985bc2c8f1d742f094c004b9120e719226f48e2a2a41d
|
|
| MD5 |
97894180e6bf580a22acf6f8ac63d8d6
|
|
| BLAKE2b-256 |
731f82476d0557038db458e3c521a8c967568c5145e6666410c35343c8289285
|
Provenance
The following attestation bundles were made for pycastic-0.0.2-py3-none-any.whl:
Publisher:
bump-and-release.yml on tssweeney/pycastic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pycastic-0.0.2-py3-none-any.whl -
Subject digest:
edd1220c1ad28f90392985bc2c8f1d742f094c004b9120e719226f48e2a2a41d - Sigstore transparency entry: 819830354
- Sigstore integration time:
-
Permalink:
tssweeney/pycastic@ed5cf46119d0488430f1bca0440f44b0886d6fc9 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/tssweeney
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
bump-and-release.yml@ed5cf46119d0488430f1bca0440f44b0886d6fc9 -
Trigger Event:
workflow_dispatch
-
Statement type: