A concrete syntax tree with AST-like properties for Python 3.0 through 3.13 programs.
—
LibCST provides a powerful command-line interface for parsing, analyzing, and transforming Python code using concrete syntax trees. The CLI tool offers several commands for different operations.
The LibCST CLI tool is invoked as a Python module:
python -m libcst.tool --helpThe LibCST command-line tool provides four main commands:
print - Print the LibCST tree representation of Python filescodemod - Execute code transformations using codemod commandslist - List all available codemod commandsinitialize - Initialize a directory with default LibCST configurationAll commands support these global options:
--version Print current version of LibCST toolset
--help, -h Show help message
@file Read arguments from a file (one per line)Parse and display the LibCST concrete syntax tree representation of Python code.
python -m libcst.tool print [OPTIONS] INFILE
Arguments:
INFILE File to print tree for. Use "-" for stdin
Options:
--show-whitespace Show whitespace nodes in printed tree
--show-defaults Show values that are unchanged from the default
--show-syntax Show values that exist only for syntax (commas, semicolons)
--graphviz Display the graph in .dot format, compatible with Graphviz
--indent-string STRING String to use for indenting levels (default: " ")
-p, --python-version VER Override the version string used for parsing Python filesPrint the CST of a Python file:
python -m libcst.tool print my_script.pyParse from stdin and show whitespace nodes:
cat my_script.py | python -m libcst.tool print --show-whitespace -Generate Graphviz output for visualization:
python -m libcst.tool print --graphviz my_script.py | dot -Tpng -o tree.pngParse with specific Python version:
python -m libcst.tool print --python-version 3.9 my_script.pyExecute code transformations using LibCST's codemod framework. Codemods are automated code transformation tools that can refactor, update, or modify Python codebases systematically.
python -m libcst.tool codemod [OPTIONS] COMMAND PATH [PATH ...]
Arguments:
COMMAND Codemod command to execute (e.g., strip_strings_from_types.StripStringsCommand)
PATH Path(s) to transform. Can be files, directories, or "-" for stdin
Options:
-x, --external Interpret COMMAND as just a module/class specifier
-j, --jobs JOBS Number of jobs for parallel processing (default: number of cores)
-p, --python-version VER Override Python version for parsing (default: current Python version)
-u, --unified-diff [N] Output unified diff instead of contents (default context: 5 lines)
--include-generated Process generated files (normally skipped)
--include-stubs Process typing stub files (.pyi)
--no-format Skip formatting with configured formatter
--show-successes Print successfully transformed files with no warnings
--hide-generated-warnings Don't print warnings for skipped generated files
--hide-blacklisted-warnings Don't print warnings for skipped blacklisted files
--hide-progress Don't show progress indicatorRemove unused imports from a file:
python -m libcst.tool codemod remove_unused_imports.RemoveUnusedImportsCommand my_script.pyTransform an entire directory with diff output:
python -m libcst.tool codemod --unified-diff strip_strings_from_types.StripStringsCommand src/Run codemod from stdin to stdout:
cat my_script.py | python -m libcst.tool codemod noop.NOOPCommand -Use external codemod with custom arguments:
python -m libcst.tool codemod -x my_custom_codemods.MyTransform --old_name foo --new_name bar src/Process files in parallel:
python -m libcst.tool codemod --jobs 8 convert_format_to_fstring.ConvertFormatStringCommand src/Display all available codemod commands and their descriptions.
python -m libcst.tool listShow all available codemods:
python -m libcst.tool listExample output:
add_trailing_commas.AddTrailingCommasCommand - Add trailing commas to function calls, literals, etc.
convert_format_to_fstring.ConvertFormatStringCommand - Convert .format(...) strings to f-strings
noop.NOOPCommand - Does absolutely nothing.
rename.RenameCommand - Rename all instances of a local or imported object.
remove_unused_imports.RemoveUnusedImportsCommand - Remove unused imports from modules.Create a default LibCST configuration file in a directory to configure codemod behavior.
python -m libcst.tool initialize PATH
Arguments:
PATH Path to initialize with default LibCST configurationThe initialize command creates a .libcst.codemod.yaml file with these settings:
# String that LibCST should look for in code which indicates that the module is generated code.
generated_code_marker: "@generated"
# Command line and arguments for invoking a code formatter.
formatter: ["black", "-"]
# List of regex patterns which LibCST will evaluate against filenames to determine if the module should be touched.
blacklist_patterns: []
# List of modules that contain codemods inside of them.
modules:
- libcst.codemod.commands
# Absolute or relative path of the repository root, used for providing full-repo metadata.
repo_root: "."Initialize current directory:
python -m libcst.tool initialize .Initialize a specific project directory:
python -m libcst.tool initialize /path/to/my/projectLibCST includes several built-in codemod commands for common transformations:
convert_format_to_fstring.ConvertFormatStringCommand - Convert .format() calls to f-stringsconvert_percent_format_to_fstring.ConvertPercentFormatToFStringCommand - Convert % formatting to f-stringsunnecessary_format_string.UnnecessaryFormatStringCommand - Remove unnecessary f-string formattingconvert_type_comments.ConvertTypeComments - Convert type comments to annotationsconvert_union_to_or.ConvertUnionToOrCommand - Convert Union[X, Y] to X | Y (Python 3.10+)strip_strings_from_types.StripStringsCommand - Remove string quotes from type annotationsremove_unused_imports.RemoveUnusedImportsCommand - Remove imports that are not usedensure_import_present.EnsureImportPresentCommand - Add imports if they don't existrename.RenameCommand - Rename symbols and update importsadd_trailing_commas.AddTrailingCommasCommand - Add trailing commas to collectionsconvert_namedtuple_to_dataclass.ConvertNamedTupleToDataclassCommand - Convert NamedTuple to dataclassadd_pyre_directive.AddPyreDirectiveCommand - Add Pyre type checker directivesremove_pyre_directive.RemovePyreDirectiveCommand - Remove Pyre directivesfix_pyre_directives.FixPyreDirectivesCommand - Fix malformed Pyre directivesMany codemods accept additional arguments. For example:
# Rename a symbol across the codebase
python -m libcst.tool codemod rename.RenameCommand \
--old_name "mymodule.OldClass" \
--new_name "mymodule.NewClass" \
src/
# Ensure an import is present
python -m libcst.tool codemod ensure_import_present.EnsureImportPresentCommand \
--module "typing" \
--entity "List" \
src/LIBCST_TOOL_REQUIRE_CONFIG - If set, requires a configuration file to be presentLIBCST_TOOL_COMMAND_NAME - Override the command name in help textLibCST searches for .libcst.codemod.yaml configuration files by walking up the directory tree from the current working directory. This allows different parts of a repository to have different codemod configurations.
The configuration file specifies a code formatter command that will be automatically applied after codemod transformations. The default is Black, but any formatter that accepts code via stdin and outputs formatted code via stdout can be used.
You can create custom codemods by subclassing from CodemodCommand:
from libcst.codemod import CodemodCommand
import libcst as cst
class MyCustomCommand(CodemodCommand):
DESCRIPTION = "My custom transformation"
def transform_module_impl(self, tree: cst.Module) -> cst.Module:
# Your transformation logic here
return treeFor large codebases, use the --jobs option to process files in parallel:
python -m libcst.tool codemod --jobs 16 my_transform.MyCommand large_codebase/Use --unified-diff to generate patches for review:
python -m libcst.tool codemod --unified-diff my_transform.Command src/ > changes.patchThe CLI tool returns appropriate exit codes:
0 - Success, no failures1 - Some files failed to transform2 - Interrupted by user (Ctrl+C)The codemod command provides detailed reporting:
Finished codemodding 150 files!
- Transformed 145 files successfully.
- Skipped 3 files.
- Failed to codemod 2 files.
- 5 warnings were generated.Files may be skipped for several reasons:
generated_code_marker string)blacklist_patternsUse the various --hide-* flags to control warning verbosity, and --show-successes to see all successfully processed files.
Install with Tessl CLI
npx tessl i tessl/pypi-libcst