[WIP] Add support for custom section names#691
Conversation
| from functools import cached_property | ||
| from warnings import warn | ||
|
|
||
| from sphinx.util import logging |
There was a problem hiding this comment.
We're trying to move away from having a Sphinx dependency, so probably don't want this.
There was a problem hiding this comment.
I'd be fine removing it if there's a replacement that has already been agreed on.
| "index": {}, | ||
| } | ||
|
|
||
| def __init__(self, docstring, config=None): |
There was a problem hiding this comment.
We need to ensure that config is documented somewhere.
There was a problem hiding this comment.
I agree. I am not 100% sure what the type of config is everywhere, but I believe it's just a simple dict. I am more sure that it is at least supposed to support the MutableMapping ABC.
|
|
||
| if config is not None: | ||
| extra_sections = config.get("extra_sections", dict()) | ||
| for section, fmt in extra_sections.items(): |
There was a problem hiding this comment.
Can probably handle this with set intersections.
| self.template = config.get("template") | ||
| self.template_file = config.get("template_file") |
There was a problem hiding this comment.
Are these changes related to the PR (looks like they are)? If so, document along with other config options.
There was a problem hiding this comment.
Yes. I originally had put the code for the extra templating information there, but realized that it is additionally set in get_doc_object() at the end of that module. I am unsure if it needs to be in both places, but I think it is probably good to have it in both since the one you commented on sets the attributes of SphinxDocString, which already has attributes for the other config options.
| } | ||
|
|
||
| for section, fmt in self.extra_sections.items(): | ||
| if not self.get(section): |
There was a problem hiding this comment.
This change doesn't look quite right; ignores the dictionary above?
There was a problem hiding this comment.
The dictionary above it is the standard numpydoc sections, which I suppose can be hard-coded for now. After those are set, I iterate over the extra sections that the user provides, and use them to update the above dictionary.
There was a problem hiding this comment.
OK, but do we need to re-handle existing sections in there, or can those be removed and handled via the standard mechanism?
There was a problem hiding this comment.
The existing sections aren't touched there. The reason it looks like it is because I am re-using the names of the _str_<section_format>() functions to select the format, which are also the identifiers in numpydoc_docstring.rst.
Motivated by #202.
WIP Implementation
The implementation I've gotten to work for now is quite rough, but basically can be boiled down to two steps for users. The first is to provide a custom template file, which follows the form of
numpydoc_docstring.rst, and just requires that the user add in whichever new section names they want. This template file is then picked up by the variable inconf.pycallednumpydoc_template_file.The other part of this is adding in the desired formatting of the new section. This is done through setting
numpydoc_extra_sectionsinconf.py, which is a dictionary with the keys being the section names and the values being the desired format. For example, you could haveThese format values map to the existing string functions in
docscrape_sphinx.SphinxDocString, and are matched like so:To Do
I am not a huge fan of users needing to supply two separate values to get a new section in, however I haven't figured out a way to add in extra sections that I like.
Another thing to do is to update the validation code to recognize custom sections, if that is desired.
Example
I have a minimal example in a branch in my own repository, where all I've done is very simply change
NotestoNotein theElementenum.