Skip to content

sections: declare sections alike in every style - #144

Merged
rr- merged 1 commit into
mainfrom
rework/6-sections
Nov 6, 2026
Merged

rr- merged 1 commit into
mainfrom
rework/6-sections

Conversation

@rr-

@rr- rr- commented Nov 6, 2026

Copy link
Copy Markdown
Owner

This PR adds docstring_parser.sections with SectionSpec, one way to declare sections for all four styles.

A spec gives a section's titles, the kind of its items, their key and their layout. In Google and Numpydoc the titles are section titles; in ReST and Epydoc they are field names. A list goes to parse(), parse_from_object(), each style module's parse(), GoogleParser and NumpydocParser as sections. default_sections(style) returns a style's own list to build on.

  • The spec's kind picks the items' kind and class, so a section no longer has to be keyed param to give params.
  • bare=True takes a title alone on its line, with no colon (Google) or underline (Numpydoc).
  • parse_item gets each item's meta, already split, to change or replace it.

Under the hood, ReST and Epydoc fields become tables of specs, and Google and Numpydoc builders go by the section's kind instead of its key. Epydoc's five header regexes become one, with every error and quirk kept; Epydoc parses in about 0.65 of the time on main.

The README has a section on it. The old ways to customise parsing (overriding titles_re, title_pattern, _build_meta, _parse_item, KV_REGEX) keep working; the next PR removes them.

No output change: passing a style's default sections gives the same result as passing none on every case file, and just parity finds no difference. The other styles stay within 3% of main.

Add `docstring_parser.sections` with `SectionSpec`, which gives a
section's titles, the kind of its items, their key and their layout.
The same spec works in every style: its titles are section titles in
Google and Numpydoc, and field names in ReST and Epydoc. A list of
specs goes to `parse()`, `parse_from_object()`, each style module's
`parse()`, `GoogleParser` and `NumpydocParser` as `sections`, and
`default_sections(style)` gives the ones a style uses by default.

A spec's kind decides its items' kind and class, so a section no longer
has to be keyed `param` to give params. `bare=True` takes a title alone
on its line, with no colon or underline. `parse_item` gets each item's
meta, already split, to change it or replace it.

ReST's and Epydoc's fields become tables of specs, and Google's and
Numpydoc's builders go by the kind of their section instead of by its
key. Epydoc's five header regexes become one and a check of how many
names each kind of field takes, which keeps every error and quirk.

The ways to customise parsing by overriding keep working. Passing a
style's default sections gives the same output as passing none on every
case file, and `just parity` finds no difference without sections.
@rr- rr- self-assigned this Nov 6, 2026
@rr-
rr- merged commit 1c7a902 into main Nov 6, 2026
26 checks passed
@rr-
rr- deleted the rework/6-sections branch November 6, 2026 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant