Skip to main content

Pystrip

CI PyPI Python Issues Pull Requests Ruff Ty Docs

Banner

Python tool to safely remove comments, docstrings, and type annotations from source files using libcst.

Documentation: pystrip.github.io/pystrip

Features

  • Removes comments (inline and standalone)
  • Removes docstrings (module, class, function)
  • Removes type annotations (parameter hints, return types, variable annotations)
  • Keeps regular string literals untouched
  • Inline pragmas: # pystrip: keep protects a comment, docstring, or annotated line; # pystrip: skip-file opts a file out
  • --keep-header preserves shebang, encoding cookie, and license banners
  • ruff-style --select / --ignore over the three rules, alongside the --keep-* flags
  • Handles .pyi stubs (annotations kept) and .ipynb notebooks (code cells only)
  • --respect-gitignore and --stdin-filename for editor integration
  • Supports output formats: text, json, sarif, gitlab, github
  • Works in CI with --check
  • Supports config discovery and parallel processing
  • Try it in the browser on the playground

What pystrip will not remove

  • String literals that are not docstrings. A string only counts as a docstring when it is the first statement in a module, class, or function body.
  • String literals that merely look like comments, such as "value # not a comment".
  • Arbitrary strings later in a body, even if they are standalone expression statements.
  • Shebang lines like #!/usr/bin/env python3 unless --remove-shebang is set.
  • Files that fail to parse as Python. By default the run stops on the first such error; with --continue-on-error, pystrip reports the failure and keeps processing other files.

Docstring detection is syntax-based, not text-based. That means pystrip does not try to guess intent from quote style or wording; it only removes a literal string expression in the docstring position.

Installation

pip install pystrip

Usage

# Check mode (CI)
pystrip . --check

# Apply changes in place
pystrip ./src/ --in-place

# Read from stdin and write stripped code to stdout
pystrip - < input.py > output.py
usage: pystrip [-h] [--version] [--exclude PATH] [--exclude-glob PATTERN] [--keep-docstrings] [--keep-comments] [--keep-type-annotations] [--keep-blank] [--remove-shebang] [--use-pass] [--check]
               [--diff] [--in-place] [--output-dir DIR] [--no-recursive] [--jobs N] [--config PATH] [--format {text,json,sarif,gitlab,github}] [--quiet] [--verbose]
               [--continue-on-error]
               [paths ...]

Remove comments and docstrings from Python source files.

positional arguments:
  paths                 Files or directories to process (default: ['.'])

options:
  -h, --help            show this help message and exit
  --exclude PATH        Exclude a file or directory path (repeatable) (default: None)
  --exclude-glob PATTERN
                        Exclude paths by glob pattern (repeatable) (default: None)
  --keep-docstrings     Keep docstrings and only strip comments (default: None)
  --keep-comments       Keep comments and only strip docstrings (default: None)
  --keep-type-annotations
                        Keep type annotations and only strip comments/docstrings (default: None)
  --keep-blank          Keep blank lines introduced by comment removal (default: None)
  --remove-shebang      Remove shebang lines (#!/...) from files (kept by default) (default: None)
  --use-pass            Use 'pass' instead of '...' for empty body placeholders (default: None)
  --check               Do not write files; exit with code 1 if any file would change (default: False)
  --diff                Print unified diffs for changed files (default: False)
  --in-place            Write stripped output back to each input file (default: False)
  --output-dir DIR      Write changed files into DIR instead of modifying inputs (default: None)
  --no-recursive        Process only direct child files of each directory path (default: True)
  --jobs N              Number of worker processes to use (default: None)
  --config PATH         Load configuration from a specific TOML file (default: None)
  --format {text,json,sarif,gitlab,github}
                        Output format for violations (default: None)
  --quiet               Suppress progress and summary output (default: False)
  --verbose             Print detailed removal diagnostics (default: False)
  --continue-on-error   Continue processing remaining files when a file fails to parse/process (default: False)

Output example:

⠇ Processing 10 file(s)...
src/pystrip/__init__.py:1:0: DOCSTRING_REMOVED Module docstring removed
src/pystrip/__main__.py:1:0: DOCSTRING_REMOVED Module docstring removed
...
src/pystrip/visitor.py:1:0: DOCSTRING_REMOVED Module docstring removed
Changed 10 file(s), 63 violation(s), 26 docstring(s), 37 comment(s), 0 annotation(s).

When using - as the sole input path, pystrip reads source from stdin. In normal mode it writes stripped code to stdout; in --check mode it emits violations instead. --in-place, --output-dir, and --diff are not available with stdin input.

Configuration

Use either pyproject.toml ([tool.pystrip]) or .pystrip.toml ([pystrip]). pyproject.toml is recommended when pystrip is part of a project; .pystrip.toml is useful for standalone usage. Invalid config values (for example jobs = "4") fail fast with exit code 2 and a clear error message.

[tool.pystrip]
remove_comments = true
remove_docstrings = true
remove_blank_lines = true
remove_type_annotations = true
exclude = ["tests/"]
exclude_glob = ["*.generated.py"]
jobs = 4

Output and CI

# GitHub annotations
pystrip . --check --format github

# GitLab code-quality report
pystrip . --check --format gitlab > gl-code-quality-report.json

Integrations

pre-commit

repos:
  - repo: https://github.com/pystrip/pystrip
    rev: v1.5.0
    hooks:
      - id: pystrip        # strip in place
      # - id: pystrip-check  # verify only, writes nothing

GitHub Actions

- uses: pystrip/pystrip@v1
  with:
    paths: "src"
    args: "--check --format github"

More, including GitLab CI, in the integrations guide.

Documentation

Full documentation lives at pystrip.github.io/pystrip: usage, configuration, output formats, integrations, the Python API, FAQ, and the roadmap.

Development

Developer setup, quality checks, and contribution workflow are documented in docs/development.md and CONTRIBUTING.md.

Exit Codes

Code Meaning
0 Clean (no changes needed)
1 Changes would be made in --check mode
2 Runtime or CLI error

Metadata

Release files for pystrip 1.5.0

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

Source distribution (sdist)

Source distribution for pystrip 1.5.0
File Size Uploaded Provenance Actions
pystrip-1.5.0.tar.gz 695.6 kB With provenance Details for pystrip-1.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pystrip 1.5.0
File Interpreter ABI Platform Actions
pystrip-1.5.0-py3-none-any.whl Python 3 none any Details for pystrip-1.5.0-py3-none-any.whl

Total release size: 719.0 kB

Release files / pystrip-1.5.0.tar.gz

Download URL pystrip-1.5.0.tar.gz
Size 695.6 kB
Tags Source
SHA-256 checksum
How to use checksums
50495b1732f2862d18394389c3495bbd900697f026adec7bf621909b034f449b
BLAKE2b-256 checksum
How to use checksums
819b6cf2841a829b8cfdb7f238ea793d6861cf88cd296e5c1520b467403e1f44
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 9, 2026.

Transparency log

Release files / pystrip-1.5.0-py3-none-any.whl

Download URL pystrip-1.5.0-py3-none-any.whl
Size 23.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e7082f874f8f8f8dc8e22bca0a320e540d638f3cfe9708b818bd5f7c2eba344a
BLAKE2b-256 checksum
How to use checksums
c3de55e5c75de7ec79949675dc9cae175de84552d17af25dca322e9dddba4f67
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 9, 2026.

Transparency log

Security for pystrip 1.5.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pystrip-1.5.0.tar.gz

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 Nov 9, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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