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.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-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pycommremover-0.4.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pycommremover-0.4.tar.gz
Algorithm Hash digest
SHA256 4e4aa12504f5c210b1340f9ab809b9328dd8c224b9f646d2aaee6b51dae23762
MD5 2c1108d31a5be37ed0cd9cccab92d56f
BLAKE2b-256 9a5e1a0867594c94a3efbcec959005c5f0ec92d90e81c71ff90b57a3051b2f7c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pycommremover-0.4-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pycommremover-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 27aceb83928e12a307c86cfeb4a0b92e1de0fa9211122c65fd90fb50e1112210
MD5 5e2f640ff830f57acda195bc5e307e41
BLAKE2b-256 7c98324ab9ff9a09d61857a6c53f5fc1c10483ee9449ba38329d7a174fac5cb9

See more details on using hashes here.

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