Skip to main content

A lightweight, efficient parser for Google-style Python docstrings that converts them into structured dictionaries.

Project description

Google Docstring Parser

[!IMPORTANT] This package requires a PAID LICENSE for all users EXCEPT the Albumentations Team. Contact iglovikov@gmail.com to obtain a license before using this software.

A Python package for parsing Google-style docstrings into structured dictionaries.

License Information

This package is available under a custom license:

See the LICENSE file for complete details.

Installation

pip install google-docstring-parser

Usage

from google_docstring_parser import parse_google_docstring

docstring = '''Apply elastic deformation to images, masks, bounding boxes, and keypoints.

This transformation introduces random elastic distortions to the input data. It's particularly
useful for data augmentation in training deep learning models, especially for tasks like
image segmentation or object detection where you want to maintain the relative positions of
features while introducing realistic deformations.

Args:
    alpha (float): Scaling factor for the random displacement fields. Higher values result in
        more pronounced distortions. Default: 1.0
    sigma (float): Standard deviation of the Gaussian filter used to smooth the displacement
        fields. Higher values result in smoother, more global distortions. Default: 50.0

Example:
    >>> import albumentations as A
    >>> transform = A.ElasticTransform(alpha=1, sigma=50, p=0.5)

References:
    - Original paper: Simard, P. Y., et al. "Best practices for convolutional neural networks applied to visual document analysis." ICDAR 2003
    - Implementation details: https://example.com/elastic-transform
Returns:
    dict[str, Any]: Some info here
'''

parsed = parse_google_docstring(docstring)
print(parsed)

Output:

{
    'Description': 'Apply elastic deformation to images, masks, bounding boxes, and keypoints.\n\nThis transformation introduces random elastic distortions to the input data. It\'s particularly\nuseful for data augmentation in training deep learning models, especially for tasks like\nimage segmentation or object detection where you want to maintain the relative positions of\nfeatures while introducing realistic deformations.',
    'Args': [
        {
            'name': 'alpha',
            'type': 'float',
            'description': 'Scaling factor for the random displacement fields. Higher values result in\nmore pronounced distortions. Default: 1.0'
        },
        {
            'name': 'sigma',
            'type': 'float',
            'description': 'Standard deviation of the Gaussian filter used to smooth the displacement\nfields. Higher values result in smoother, more global distortions. Default: 50.0'
        }
    ],
    'Example': '>>> import albumentations as A\n>>> transform = A.ElasticTransform(alpha=1, sigma=50, p=0.5)',
    'References': [
        {
            'description': 'Original paper',
            'source': 'Simard, P. Y., et al. "Best practices for convolutional neural networks applied to visual document analysis." ICDAR 2003'
        },
        {
            'description': 'Implementation details',
            'source': 'https://example.com/elastic-transform'
        }
    ],
    'Returns':
        {
            "type": "dict[str, Any]",
            "description": "Some info here"
        }
}

Features

  • Parses Google-style docstrings into structured dictionaries
  • Extracts parameter names, types, and descriptions
  • Preserves other sections like Examples, Notes, etc.
  • Handles multi-line descriptions and indentation properly
  • Properly parses and validates References sections with special handling for URLs

References

The parser can handle reference sections in Google-style docstrings. References can be formatted in two ways:

  1. Single reference format:
"""
Reference:
    Paper title: https://example.com/paper
"""
  1. Multiple references format (requires leading dashes):
"""
References:
    - First paper: https://example.com/paper1
    - Second paper: https://example.com/paper2
"""

Each reference is parsed into a dictionary with description and source keys. URLs in the source are properly handled, ensuring colons in URLs are not confused with the separator colon.

Pre-commit Hook

This package includes a pre-commit hook that checks if Google-style docstrings in your codebase can be parsed correctly.

Usage in Other Projects

To use this hook in another project, add the following to your .pre-commit-config.yaml:

- repo: https://github.com/ternaus/google-docstring-parser
  rev: v0.0.1  # Use the latest version
  hooks:
    - id: check-google-docstrings
      additional_dependencies: ["tomli>=2.0.0"]  # Required for pyproject.toml configuration

Configuration

The hook is configured via pyproject.toml, following modern Python tooling conventions like those used by mypy, ruff, and other tools.

Add a [tool.docstring_checker] section to your pyproject.toml:

[tool.docstring_checker]
paths = ["src", "tests"]                     # Directories or files to scan
require_param_types = true                   # Require parameter types in docstrings
check_references = true                      # Check references for proper format
check_type_consistency = true                # Compare docstring types with annotations
exclude_files = ["conftest.py", "__init__.py"] # Files to exclude from checks
min_short_description_length = 10            # Minimum summary length; set to 0 to disable
verbose = false                              # Enable verbose output

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

google_docstring_parser-0.0.10.tar.gz (24.1 kB view details)

Uploaded Source

Built Distribution

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

google_docstring_parser-0.0.10-py3-none-any.whl (23.6 kB view details)

Uploaded Python 3

File details

Details for the file google_docstring_parser-0.0.10.tar.gz.

File metadata

  • Download URL: google_docstring_parser-0.0.10.tar.gz
  • Upload date:
  • Size: 24.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for google_docstring_parser-0.0.10.tar.gz
Algorithm Hash digest
SHA256 45f26bd65ed458df6de6a2a78e9190e8016aa52ce70f09036c8a06f7e71f0f9a
MD5 c714e9cb00bcb9ff79c80820a1af0344
BLAKE2b-256 3ee7b267de205e07e80f645c9c4bac8e01738b34cb4cb2d1f3ea1d1b3049d7a7

See more details on using hashes here.

File details

Details for the file google_docstring_parser-0.0.10-py3-none-any.whl.

File metadata

File hashes

Hashes for google_docstring_parser-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 7bda6cda73a31c234a1ae87e1e78e61fb74a05471b7b06f31c2d7b67e0c43116
MD5 c1b3ab52bb058509f25aa68bc91846af
BLAKE2b-256 9a192b57f46b96861dc59c8b1d74e81677cac7696c5cac74ebcebc09d1032aab

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