A tool for updating docstrings in Python files using the OpenAI API
Project description
autodocstrings
Source Code: https://github.com/cdesarmeaux/autodocstrings
Autodocstrings is a command-line tool with the following key features:
- Updates the docstrings in Python files using the OpenAI API.
- Can process a single file or a directory of files, including all subdirectories.
Autodocstrings uses the OpenAI api to generate docstrings, so these are not guaranteed to be perfect. However, they are a good starting point for writing your own docstrings.
The code-davinci-002 model is used to generate the docstrings. This model is trained on a large corpus of Python code, including docstrings. The model is able to generate docstrings that are similar to those found in the corpus.
Autodocstrings will work best for code that already has good type hints. Without type hints, the OpenAI API will have to guess input and return types, which may not be accurate.
Requirements
- Python 3.6+
- A valid openai api key. You can get one here. This is currently free.
Installation
$ pip install autodocstrings
Installation
To install the dependencies for this tool, run the following command:
pip install autodocstrings
Usage
To use this tool, run the following commands:
$ export OPENAI_API_KEY=1234567890
$ python update_docstrings.py INPUT `
[--replace-existing-docstrings] `
[--skip-constructor-docstrings] `
[--exclude-directories EXCLUDE_DIRECTORIES] `
[--exclude-files EXCLUDE_FILES]
Where INPUT is a Python file or directory containing Python files to update the docstrings in, API_KEY is your OpenAI API key, and the optional flags --replace-existing-docstrings and --skip-constructor-docstrings can be used to skip updating docstrings for constructors (init methods) and replacing existing docstirngs. EXCLUDE_DIRECTORIES and EXCLUDE_FILES are comma-separated lists of directories and files to exclude from the update.
Examples
Update the docstrings in all Python files in the my_code directory:
$ python update_docstrings.py my_code/
Update the docstrings in the my_file.py file:
$ python update_docstrings.py my_file.py
Update the docstrings in all Python files in the my_code directory and replace existing ones:
$ python update_docstrings.py my_code/ --replace-existing-docstrings
Update the docstrings in all Python files in the my_code directory, but skip updating docstrings for class constructors (init methods):
$ python update_docstrings.py my_code/ --skip-constructor-docstrings
Update the docstrings in all Python files in the my_code directory, but exlcude the exclude_dir directory and the exclude_file_1.py and exclude_file_2.py files:
$ python update_docstrings.py my_code/ --exclude-directories exclude_dir --exclude-files exclude_file_1.py,exclude_file_2.py
License
This project is licensed under the MIT License. See the LICENSE file for details.
Limitations
- The python functions are being passed to the OpenAI API as independent code blocks. This means that the docstrings are not aware of the context of the function. If functions are written independently of each other, then this should not be a problem.
- The format of the docstring is not always consistent, so you may need to manually fix some of the docstrings. You shouldn't use this in a ci/cd pipeline.
- Input length is limited to the maximum input length of the OpenAI API. This is currently 2048 characters. If your function is larger than this then the docstring will not be updated.
- OpenAI API can be slow.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file autodocstrings-0.1.3.tar.gz
.
File metadata
- Download URL: autodocstrings-0.1.3.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb7c981c76db0c13f6248d550d30a4e2e70be6ea6cb1631b30ad2c4d70f13334 |
|
MD5 | c33023afdd586112289a72eb3b143dd6 |
|
BLAKE2b-256 | 3e3cfdb198afcba8f4c92d5aa4561996e31878448443d7bfd81c4608d024f85c |
File details
Details for the file autodocstrings-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: autodocstrings-0.1.3-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 08dc895659625de6d01dddaeab898d409b41118b497cd1dd0f90674279e35c1e |
|
MD5 | 5d3af7268f49b7e100d84e9ef7e553e0 |
|
BLAKE2b-256 | 3427fd045d82e77d17ded9771af97da5ea5b467214de34af7879b793b9fb5866 |