Skip to main content

Generate, fix and convert docstrings.

Project description

Create, update or convert docstrings in existing Python files, managing several styles.

Project Status

Test Status

Build Documentation Status Downloads

License: GPL v3 Code style: black linting: pylint Ruff Checked with pyright pre-commit

Supported Versions

Supports Python39 Supports Python310 Supports Python311 Supports Python312

Description

Command-line program to generate, update or transform docstrings python source code.

The app will parse the requested source files for docstrings as well as function signatures and class bodies.

This information is combined to build up complete docstrings for every function and class including place holders for types and descriptions where none could be found elsewhere.

The output format of the docstrings can be chosen between google, numpy, reST and epydoc. This means that the tool can also be used to transform docstrings in the file from one format into another.

Note however that not all section types are supported for all docstring styles.

Partially because they have not been added yet, but also because not every style officially supports the sections from all others.

To get further information please refer to the documentation.

The tool offers the choice between generating patch files or directly overwriting the python source files.

Start quickly

  • install from PyPi

$ pip install pymend
  • install from sources:

$ pip install git+https://github.com/JanEricNitschke/pymend.git
or
$ git clone https://github.com/JanEricNitschke/pymend.git
$ cd pymend
$ python setup.py install
  • run from the command line:

$ pymend  myfile.py    # will generate a patch
$ pymend -w myfile.py  # will overwrite the file

or

$ pymend  my/folder/
  • get help:

$ pymend -h

Example

To see how pymend looks in action lets consider this example file example.py:

"""_summary_."""
 def my_func(param0, param01: int, param1: str = "Some value", param2: List[str] = {}):
     """_summary_.

     Args:
         param0 (_type_): _description_
         param01 (int): _description_
         param1 (str, optional): _description_. Defaults to "Some value".
         param2 (List[str], optional): _description_. Defaults to {}.
     """
     pass


 def my_single_return_func1() -> str:
     """_summary_.

     Returns
     -------
     int
         Wrong
     """
     pass


 def my_multi_return_func() -> Tuple[int, str, bool]:
     """_summary_.

     Returns
     -------
     x :
         Some integer
     y : str
         Some string
     z : bool
         Some bool
     """
     pass

class A:
   def method(self, param1, param2=None) -> int:
         pass

Now let’s use Pyment:

$ pyment example.py

This produces the following patch file example.py.patch:

# Patch generated by Pymend v1.0.0

--- a/example.py
+++ b/example.py
@@ -2,11 +2,16 @@
def my_func(param0, param01: int, param1: str = "Some value", param2: List[str] = {}):
   """_summary_.

-    Args:
-        param0 (_type_): _description_
-        param01 (int): _description_
-        param1 (str, optional): _description_. Defaults to "Some value".
-        param2 (List[str], optional): _description_. Defaults to {}.
+    Parameters
+    ----------
+    param0 : _type_
+        _description_
+    param01 : int
+        _description_
+    param1 : str
+        _description_. Defaults to "Some value".
+    param2 : List[str]
+        _description_. Defaults to {}.
   """
   pass

@@ -16,7 +21,7 @@

   Returns
   -------
-    int
+    str
         Wrong
   """
   pass
@@ -27,7 +32,7 @@

   Returns
   -------
-    x :
+    x : _type_
         Some integer
   y : str
         Some string
@@ -37,5 +42,21 @@
   pass

class A:
+    """_summary_.
+
+    Methods
+    -------
+    method(param1, param2=None)
+        _description_
+    """
   def method(self, param1, param2=None) -> int:
+        """_summary_.
+
+        Parameters
+        ----------
+        param1 : _type_
+            _description_
+        param2 : _type_
+            _description_ (Default value = None)
+        """
         pass

Calling pyment directly with

$ pyment --write example.py

print outs this information about changed files

– code-block: sh

$ Modified docstrings of elements (my_func, my_single_return_func1, my_multi_return_func, A, method) in file example.py.

and results in the final file (the same we would have gotten when applying the patch):

"""_summary_."""
def my_func(param0, param01: int, param1: str = "Some value", param2: List[str] = {}):
   """_summary_.

   Parameters
   ----------
   param0 : _type_
      _description_
   param01 : int
      _description_
   param1 : str
      _description_. Defaults to "Some value".
   param2 : List[str]
      _description_. Defaults to {}.
   """
   pass


def my_single_return_func1() -> str:
   """_summary_.

   Returns
   -------
   str
      Wrong
   """
   pass


def my_multi_return_func() -> Tuple[int, str, bool]:
   """_summary_.

   Returns
   -------
   x : _type_
      Some integer
   y : str
      Some string
   z : bool
      Some bool
   """
   pass

class A:
   """_summary_.

   Methods
   -------
   method(param1, param2=None)
      _description_
   """
   def method(self, param1, param2=None) -> int:
      """_summary_.

      Parameters
      ----------
      param1 : _type_
            _description_
      param2 : _type_
            _description_ (Default value = None)
      """
      pass

Pre-commit

To use pymend in a pre-commit hook just add the following to your .pre-commit-config.yaml

repos:
-   repo: https://github.com/JanEricNitschke/pymend
    rev: "v1.0.0"
    hooks:
    -   id: pymend
        language: python
        args: ["--write", "--output=numpydoc"]

Acknowledgements

This project was inspired by and is originally based upon pyment. The intended functionality as well as the main entry point remain largerly unchanged. However additional functionality has been added in the form of ast traversal for extracting function and class information.

The docstring parsing has been replaced completely with code taken from the awesome docstring_parser project, specifically this fork.

So far only minor modifications have been made to the docstring parsing functionality. Mainly the addition of the “Methods” section for numpydoc style docstrings. Additionally the the code has been linted as well as type hinted.

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

pymend-1.0.2.tar.gz (68.6 kB view details)

Uploaded Source

Built Distribution

pymend-1.0.2-py3-none-any.whl (49.8 kB view details)

Uploaded Python 3

File details

Details for the file pymend-1.0.2.tar.gz.

File metadata

  • Download URL: pymend-1.0.2.tar.gz
  • Upload date:
  • Size: 68.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for pymend-1.0.2.tar.gz
Algorithm Hash digest
SHA256 bbc0889cbb9be035e185a7df79c3c993cc0a19182c897df3202d5d8e882b1b3f
MD5 99ac6790f621d8d305f2be8a711818c3
BLAKE2b-256 48a65988dea7b5294f1450a4a42827419389d0d6521587d6ecd11b8f5b1bad5a

See more details on using hashes here.

File details

Details for the file pymend-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: pymend-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 49.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for pymend-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2ab6487a063f36c26602a3b4a704a292786590d9ee91204318c54b0ddc631602
MD5 a64aff7c0be88e4ede293c08f2f8e641
BLAKE2b-256 779c264e80b9aae8f8a2d2c57b0137c800be13a4bc6bb47a23bbf4c566fbde2c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page