Skip to main content

ast-workers-py

中文文档

Python AST manipulation CLI tool. Edit Python code at the Abstract Syntax Tree level with guaranteed syntax correctness.

Installation

pip install ast-workers-py

Usage

ast-py <operation> [options]

Operations

Command Alias Description
insert-function if Insert a function
update-function uf Update a function
insert-class ic Insert a class
insert-class-variable icv Insert a class variable
insert-slots is Insert __slots__ into a class
insert-dunder-all iall Insert or update __all__
insert-import ii Insert an import statement
list-functions lf List functions in a module/class
list-classes lc List classes in a module
list-imports li List imports in a module
find-symbol fs Find a symbol in a module
delete-function df Delete a function
delete-class dc Delete a class
rename-symbol rn Rename a symbol
show s Show symbol with context
validate v Validate Python syntax
format fmt Format Python code
batch b Execute multiple operations from JSON

Examples

Query Operations

# List functions in a module
ast-py list-functions -m src/auth.py

# List methods in a class
ast-py list-functions -m src/auth.py -c AuthService

# List classes in a module
ast-py list-classes -m src/models.py

# Show a symbol with context (supports scoped naming)
ast-py show -m src/auth.py -n AuthService.login

# Find a symbol
ast-py find-symbol -m src/auth.py -n validate_token -t function

Insert Operations

# Insert a simple function
ast-py insert-function -m src/auth.py -n validate_token -p "token:str" -r bool -b "return len(token) > 10"

# Insert a method into a class
ast-py insert-function -m src/auth.py -c AuthService -n check_permissions -p "user:User, action:str" -r bool

# Insert with advanced parameters
ast-py insert-function -m src/utils.py -n process -p "data, /, strict:bool=True, *, encoding:str='utf-8', **kwargs"

# Insert with decorators
ast-py insert-function -m src/api.py -n get_user -p "user_id:int" -d "@route('/users/<int:user_id>'), @login_required"

# Insert async function
ast-py insert-function -m src/async_ops.py -n fetch_data --is-async -p "url:str" -r "dict"

# Insert with docstring
ast-py insert-function -m src/auth.py -n login -p "user:str, password:str" --docstring "Authenticate user and return session"

# Insert a class
ast-py insert-class -m src/models.py -n User -b "BaseModel" --docstring "User model" --class-vars "id:int, name:str"

# Insert a class variable
ast-py insert-class-variable -m src/models.py -c User -n count -t int -v 0

# Insert __slots__
ast-py insert-slots -m src/models.py -c User --slots id name email

# Insert __all__
ast-py insert-dunder-all -m src/auth.py --names login logout authenticate

# Insert an import
ast-py insert-import -m src/auth.py --from typing --name "Optional, Dict"

Update Operations

# Update function body
ast-py update-function -m src/auth.py -n validate_token -b "return token is not None"

# Replace entire parameter list
ast-py update-function -m src/auth.py -n login -p "self, user_id:int, timeout:int=30"

# Add parameters
ast-py update-function -m src/auth.py -n login --add-params "remember:bool=False"

# Remove parameters
ast-py update-function -m src/auth.py -n login --remove-params deprecated_param

# Change return type
ast-py update-function -m src/auth.py -n get_user --return-type "Optional[User]"

# Add decorators
ast-py update-function -m src/api.py -n endpoint --add-decorators "@cache(3600)"

# Remove decorators
ast-py update-function -m src/api.py -n endpoint --remove-decorators old_decorator

# Update docstring
ast-py update-function -m src/auth.py -n login --docstring "Authenticate user"

Delete Operations

# Delete a function
ast-py delete-function -m src/auth.py -n old_function

# Delete a class
ast-py delete-class -m src/models.py -n DeprecatedModel

Rename Operations

# Rename a function
ast-py rename-symbol -m src/auth.py -o old_name -n new_name -t function

# Rename a class
ast-py rename-symbol -m src/models.py -o OldUser -n User -t class

Utility Operations

# Validate syntax
ast-py validate -m src/auth.py

# Format code
ast-py format -m src/auth.py

# Batch operations from JSON
ast-py batch -m src/auth.py --json ops.json

Parameter Syntax

The -p/--params argument supports full Python parameter syntax:

positional, /, pos_or_kw, *, kw_only, **kwargs

Examples:

  • Basic: -p "x, y, z"
  • With types: -p "x:int, y:str, z:float"
  • With defaults: -p "x:int=0, y:str='hello'"
  • Positional-only: -p "a, b, /, c, d" (a, b are positional-only)
  • Keyword-only: -p "*, c, d=1" (c, d must be keyword args)
  • *args/**kwargs: -p "*args:int, **kwargs:dict"

Structured Body Format

For multi-line code with proper indentation, use JSON arrays:

# Nested lists represent indentation levels
ast-py insert-function -m src/auth.py -n process -p "data:dict" -b '["if data:", ["result = process(data)", "return result"], "return None"]'

Result:

def process(data: dict):
    if data:
        result = process(data)
        return result
    return None

Scoped Naming

Use dot notation for nested symbols:

# Class method
ast-py show -m src/auth.py -n AuthService.login

# Nested class
ast-py show -m src/models.py -n OuterClass.InnerClass

License

Apache 2.0

Metadata

Release files for ast-workers-py 0.1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ast-workers-py 0.1.7
File Size Uploaded
ast_workers_py-0.1.7.tar.gz 37.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ast-workers-py 0.1.7
File Interpreter ABI Platform
ast_workers_py-0.1.7-py3-none-any.whl Python 3 none any Details

Total release size: 80.4 kB

Release files / ast_workers_py-0.1.7.tar.gz

Download URL ast_workers_py-0.1.7.tar.gz
Size 37.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d115a5dc4015c4f1013f47f0d2c9c10cb95df2d56467296c79964255b97a7e19
BLAKE2b-256 checksum
How to use checksums
0dd7aff73fb3dd575d3e345ef4bf91b8bb60a71e34301a663f028b986ae6747c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 4, 2026.

Transparency log

Release files / ast_workers_py-0.1.7-py3-none-any.whl

Download URL ast_workers_py-0.1.7-py3-none-any.whl
Size 43.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d9af27cdc7066dbea671dc748b49826beaaedb5a733462ce585d6b86ff1593f8
BLAKE2b-256 checksum
How to use checksums
f3dc42afc076d645d1bd61bca6577c844b4d09fa016f2d8e311133c852e1814c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 4, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.7 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page