Skip to main content

Add your description here

Project description

papertrail

PyPI Downloads

API Reference

papertrail API docs

Motivation

  • Documentation can get out of date quickly
  • Sometimes its harder to explain what a function does in plain english than it is to just show what the input and outputs would be:
    • e.g:
      • "Multiply the value strings by the amount that is the key".
    • vs
      • f({"a": 1, "b": 2, "c": 4}) == ['a', 'bb', 'cccc']
  • Tests are the best form of documentation because they complain when they go out of sync
  • Doctests are an interesting way of writing tests however they can be fiddly and harder to set up than proper testing frameworks
  • Resorting to only using doctests means missing out on features like fixtures and parameterisation
  • Having some tests that are written in tests and some that are in doctests means keeping track of two places as the "source of truth"

Installation

uv add papertrail

Example

Use the example function to capture an example from a test to add to the docstring.

Wrap the example around the function before the equality operation and the function result will be captured and added to the docstring of the function. This only happens if the tests pass!

from papertrail import example

def test_add():
    assert example(add, 2, 3) == 5

This has captured the example and now once the tests are finished the docstring of the function will be updated like this:

def add(a: int, b: int) -> int:
    """Papertrail examples:

        >>> add(2, 3) == 5
        True
    ::
    """
    return a + b

You can use it with parametrized tests too:

@pytest.mark.parametrize(
    ("args", "kwargs", "expected_result"),
    [
        pytest.param((2, 2), {}, 0),
        pytest.param((2,), {"b": 3}, -1),
    ],
)
def test_func_b(args, kwargs, expected_result):
    assert example(func_b, *args, **kwargs) == expected_result

This captures all the cases and adds them to the functions docstring:

def func_b(a: float, b: float) -> float:
    """Simple docstring

    Args:
        a (float)
        b (float)

    Returns:
        float

    Papertrail examples:

        >>> func_b(2, 2) == 0
        True

        >>> func_b(2, b=3) == -1
        True
    ::
    """
    return a - b

Repo map

├── .github
│   └── workflows
│       ├── ci_tests.yaml
│       └── publish.yaml
├── src
│   └── papertrail
│       ├── adapters
│       │   ├── __init__.py
│       │   └── io_funcs.py              # The IO functions for the IO adapters
│       ├── core
│       │   ├── collection
│       │   │   ├── __init__.py
│       │   │   ├── example.py           # User entrypoint `example` and it's inner class `Example`
│       │   │   ├── record.py
│       │   │   └── recorder.py
│       │   ├── transformation
│       │   │   ├── __init__.py
│       │   │   ├── ast_editing.py
│       │   │   ├── format_examples.py
│       │   │   └── transform.py         # Updates the the docstrings for all the funcs in the files in the example cache.
│       │   ├── __init__.py
│       │   └── logger.py
│       ├── __init__.py
│       └── __main__.py                  # the pytest hook for sessionfinish
├── tests
│   ├── _mock_data
│   │   ├── mock_src
│   │   │   ├── __init__.py
│   │   │   ├── mod_a.py
│   │   │   └── mod_b.py
│   │   └── tests
│   │       ├── __init__.py
│   │       ├── conftest.py
│   │       ├── test_mod_a.py
│   │       └── test_mod_b.py
│   ├── core
│   │   ├── collection
│   │   │   ├── __init__.py
│   │   │   ├── test_example.py
│   │   │   ├── test_record.py
│   │   │   └── test_recorder.py
│   │   └── transformation
│   │       ├── __init__.py
│   │       ├── test_ast_editing.py
│   │       └── test_format_examples.py
│   └── test_main.py
├── .pre-commit-config.yaml
├── README.md
├── pyproject.toml
├── ruff.toml
└── uv.lock

(generated with repo-mapper-rs)
::

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

papertrail-0.1.2.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

papertrail-0.1.2-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file papertrail-0.1.2.tar.gz.

File metadata

  • Download URL: papertrail-0.1.2.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for papertrail-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f4497fd35f8fb102612112dc8231f0bab9a6d8244ac2ab14650e905dabed2ddd
MD5 24da868957201c715d5faa048325674d
BLAKE2b-256 46455299ca269c96ae46c58a4e51f1d4a44941eabd5fbede9d56c06637fb312f

See more details on using hashes here.

Provenance

The following attestation bundles were made for papertrail-0.1.2.tar.gz:

Publisher: publish.yaml on second-ed/papertrail

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

File details

Details for the file papertrail-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: papertrail-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for papertrail-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d45260e555c641525248d6ed7e84a6d37a58b61e1e29bda6d538729ca9f0cbd3
MD5 fb02063f5309d09446c850d1a1c7ee59
BLAKE2b-256 41c5fc531f78c9bc5e8cc29fe62ad1b6a7f454a2e32ecf6842b4cb6fea0fcaad

See more details on using hashes here.

Provenance

The following attestation bundles were made for papertrail-0.1.2-py3-none-any.whl:

Publisher: publish.yaml on second-ed/papertrail

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