sphinx-doc / sphinx-doc/sphinx

Support desc_parameter outside of desc_parameterlist

Open
#9,692 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type:enhancement
Dominant language
Python
Stars
8k
Forks
2.6k
PR merge metrics
No merged PRs in 30d

Description

Note: I am actively working on contributing this request. I just need some feedback and maybe a little guidance.

Is your feature request related to a problem? Please describe.

I'm working on improving on sphinxcontrib-prettyspecialmethods, and a roadblock I'm running into is that there's no good way to emulate the formatting generated by desc_parameter. Even if there were, I think that desc_parameter semantically represents what I'm working with (a parameter that ultimately goes to a function) even though I'm trying to represent the function using language syntax rather than the function itself.

Describe the solution you'd like

It seems like the most modest change is to just update the translators to behave sensibly when they encounter a desc_parameter outside of a desc_parameterlist.

I've already begun work doing this. I believe that all that's required is to make the parameter-list based state more consistent by setting it up in __init__ and resetting it on depart and detecting whether the writer is inside a parameter list before trying to write out separators.

Aside from reviewing whether my changes above make sense or if there are other use cases I need to consider, I also need some help identifying where to put tests and maybe what sorts of tests I should write. Since this change is primarily to support manipulating or generating the document tree directly in a way that existing directives don't currently support, it cannot be tested using any existing directives. I wasn't able to locate any tests that work directly with the document tree being fed into a writer or translator. What would be a reasonable way to test these changes? (My intention is to write the tests for failure and then rebase the commits above on top of them.)

Describe alternatives you've considered

  • Trying to use a desc_parameter without a parent list is not very useful in Sphinx currently. Examples:
    • If the desc_parameter instance appears before any instance of desc_parameterlist, some translators throw an error due to a missing attribute on the translator itself. The attribute is set in visit_desc_parameterlist and does not appear in __init__.
    • A previous desc_parameterlist's child separator will be reused for the separator, since this is only ever set in visit_desc_parameterlist.
    • The writer inserts commas (or whatever the last separator is) both before and after the parameter (at the same time) in some cases, due to the logic depending on counts set in visit_desc_parameterlist.
      Note that these examples indicate that the behavior is somewhat unpredictable or at least surprising, since the output depends on the order of evaluation of nodes within the document tree.
  • I can't use the desc_parameterlist node because the parentheses are hard coded in the translators, and I'd like more control over the formatting of the separating symbols than it offers anyway. I suppose strictly speaking, the translator could be modified to give more flexibility to desc_parameterlist handling, but that seems like it would be a larger change with much wider reaching impact and potential for bugs and breaking other functionality. Given that the behavior for a desc_parameter outside of a desc_parameterlist is already unsupported, unpredictable, and not particularly useful with the present behavior, it is highly unlikely that anyone is depending on this behavior already, and that means this change is unlikely to break other extensions or customizations. As a bonus, simply enabling a bare desc_parameter will grant much greater flexibility to other extension authors and future development work on Sphinx itself than merely modifying desc_parameterlist behavior.
  • I cannot just use an emphasis node. Some translators do not apply emphasis when translating desc_parameter nodes. I wish to emulate desc_parameter's behavior exactly to ensure future consistency with Sphinx's output, and the best way to do that is to just actually use the class.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the translator init methods and the visit_desc_parameterlist entry point, then trace how desc_parameter nodes are handled outside a parameter list. Add focused document-tree-to-writer tests covering bare parameters, separators, and state reset; done means translators produce predictable output without relying on prior parameter-list state.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.