Skip to main content

Python library to remove comments and docstrings from source code.

Project description

pycommremover — Python library to remove comments and docstrings from source code

pycommremover

CI PyPI version Python versions License: MIT DeepWiki

It analyses the code with the standard-library tokenize and ast modules, so a # inside a string is never mistaken for a comment and real string values (assignments, call arguments, f-strings) are never touched. Line numbers of the remaining code are preserved.

Why three functions?

At the language level these are different things, so they are removed separately:

Construct Example Runtime effect Removed by
Line comment x = 1 # note none (discarded by the tokenizer) remove_comments
Block comment a lone """note""" statement none (a discarded no-op) remove_comments
Docstring first string of a module/class/function kept as __doc__, seen by help()/doctest remove_docstrings

remove_comments is behaviour-preserving (it only deletes no-ops and keeps docstrings). remove_docstrings changes __doc__, so it is opt-in. remove_comments_and_docstrings does both in one pass.

When removing a string that is a block's only statement (e.g. def f(): """doc"""), a pass is inserted so the result stays valid Python.

Installation

Install with uv:

uv add pycommremover

Usage

import pycommremover

src = '''
def greet(name):
    """Return a greeting."""          # docstring: kept by remove_comments
    msg = f"Hi, {name}"  # build it   # line comment: removed
    """a block comment"""             # block comment: removed
    return msg
'''

# Line + block comments gone, docstring kept:
print(pycommremover.remove_comments(src))

# Only the docstring gone:
print(pycommremover.remove_docstrings(src))

# Everything gone:
print(pycommremover.remove_comments_and_docstrings(src))

The input must be syntactically valid Python.

Development

The dev dependency group (installed automatically by uv run) provides pytest:

uv run pytest

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

pycommremover-0.4.1.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

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

pycommremover-0.4.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file pycommremover-0.4.1.tar.gz.

File metadata

  • Download URL: pycommremover-0.4.1.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pycommremover-0.4.1.tar.gz
Algorithm Hash digest
SHA256 f30f49289d955a582e61a1fd3f2b6051b12dc8d07bb2510d17ffb147f4245dde
MD5 3feb9039c6b6d9dd4a1aac85f495544e
BLAKE2b-256 1c26e649ffdefb5ebec7fdb7a4c4e82531250aa5414a52492d3513a082e229a2

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycommremover-0.4.1.tar.gz:

Publisher: release.yml on carlosplanchon/pycommremover

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

File details

Details for the file pycommremover-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: pycommremover-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pycommremover-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 29333a52ec33ebb8ca524c1ec4d2d9d66459461bedb2b2b99dd82ad64445f8bc
MD5 6f7e7bbc51acd404aa0b56558f15d809
BLAKE2b-256 e163051cdf40cfcf6cc4599b652382ef8e30d4e932079d8aa095fba439d8610c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycommremover-0.4.1-py3-none-any.whl:

Publisher: release.yml on carlosplanchon/pycommremover

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