Another new library release: DocStrands lets you re-use segments of your docstrings between functions.

Copy-pasting docstrings has long frustrated me, so I came up with my own solution!

https://github.com/multimeric/DocStrands

#python #docstring

GitHub - multimeric/DocStrands: Re-use segments of your docstrings on different objects

Re-use segments of your docstrings on different objects - multimeric/DocStrands

GitHub
@multimeric interesting, thanks for sharing! Are you aware of PEP 727? It aims at providing a `Doc` class like your `Description` 🙂 It's cool to have the docstring style available in the decorator for static analysis tools 👍

@pawamoy No I evidently missed that PEP in my research, thanks for pointing me to it. I should probably align with that spec by using Doc instead of my own custom Description.

DocStrands still works a tad differently from griffe-typingdoc because it updates the docstring at runtime, and also has additional APIs for copying e.g. the description.

As an aside, I wanted to use griffe for this, but it doesn't have a deparse method for converting back to docstrings. Had you thought about making one?

@multimeric I could imagine a Griffe extension for DocStrands ☺️

Yes, I have actually started working on an unparser, which would unlock interesting use-case indeed. Specifically, parsing (and caching) docstrings too early can be problematic, and I want to provide an easy way for extension authors to modify the raw docstring instead of parsed sections. Ideally we'd have a complete unparser, as well as a more performant "injector" which just splits sections (no parsing) to insert text in between.

change: Stop caching parsed docstrings · Issue #340 · mkdocstrings/griffe

Is your change request related to a problem? Please describe. Cached parsed docstrings are causing more issues than they solve. See mkdocstrings/python#183. Cache invalidation of course! The second...

GitHub

@pawamoy Great! If that gets implemented then I'll likely use it. DocStrands shouldn't need a dedicated Griffe plugin as long as the dynamic parser is enabled. I have Griffe as part of the test suite for that reason.

Not exactly sure about the context of the caching issue but I solved it for DocStrands by lazily deparsing the docstring only when __doc__ was requested.