Skip to content

Add and standardize a control sequence for non-rendering lines in code blocks #10555

Open
@tgross35

Description

@tgross35

Is your feature request related to a problem? Please describe.
There are handful of tools that help with the formatting and checking of .. code-block:: python, but sometimes context is needed to make those work. My idea is to add a simple character sequence to indicate that specific lines of code should not be rendered.

The sphinx doctest module allows for some things like this, but requires code that is quite verbose and only supports pycon style. The proposal here would be simpler to use, easier to read, and more terse without breaking anything.

Describe the solution you'd like

I would propose using a control character like ##!. The only thing necessary from sphinx would be to hide comment lines starting with those characters. Essentially just ensure that this:

.. code-block:: python

    ##! from typing import Optional, TYPE_CHECKING
    ##! if TYPE_CHECKING:
    ##! from mymodule.something import CoolClass
    def  fn(x: Optional[int]) -> CoolClass:
        return CoolClass(x)
    ##! assert fn(5) == CoolClass(5)

Displays identical to

.. code-block:: python

    def  fn(x: Optional[int]) -> CoolClass:
        return CoolClass(x)

Some example use cases and benefits:

  • Using asserts, like shown, to validate code. A super simple tool could strip the ##! from every line and just run the code. This sort of thing is doable with doctest, but this way is simpler and allow for python (not pycon) formatting.
  • The ##! could be stripped and the code passed to a linter (flake8/pylint) or static type checker (mypy)
  • A code formatter could take this code block as-is (unstripped) and format it, or optionally strip and re-add the ##! if it's smart enough
  • Nothing RST is broken, syntax highlighters will still display properly
  • If using an outdated version of sphinx that doesn't properly hide the comments, it's not the end of the world and things will still compile
  • Super easy for sphinx to implement, just remove lines that .startswith('##!') from the rendering. Could potentially add a conf.py flag to disable it (I'd propose being enabled by default)
  • Code execution can be easily verified and copied by users since it's python style. With doctest, either the users copy the whole thing (with sphinx-copybutton) or can copy a few lines where the >>> and ... need to be removed manually - this avoids that problem. any part of the code is copyable

Describe alternatives you've considered

Pycon-style doctests, but that is not a good solution for many cases as mentioned above. https://www.sphinx-doc.org/en/master/usage/extensions/doctest.html

Additional context

The way this works is inspired by rustdoc https://doc.rust-lang.org/rustdoc/write-documentation/documentation-tests.html#hiding-portions-of-the-example

For control characters - it has to start with # to be valid python, ## is pretty commonly used in comments so that's no good, #! is used by shebang, so ##! it is. Happy medium that perfectly hints it's an executable comment.

Thanks! I'd be happy to submit a PR if this gets positive feedback.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions