Skip to content

Readability of API documentation with type annotations #8405

Description

@nicoddemus

Hey folks,

Recently on twitter a thread about type annotations in documentation gained traction, using pytest as an example:

image

I'm opening this to see if we can improve the situation.

Activity

  1. added
    type: docsdocumentation improvement, missing or needing clarification
    on Mar 5, 2021
  2. changed the title [-]Readability of API docstrings with type annotations[/-] [+]Readability of API documentation with type annotations[/+] on Mar 5, 2021
  3. davidism commented on Mar 5, 2021

    @davidism

    You're using the Sphinx autodoc extension, add autodoc_typehints = "description" to docs/conf.py to get the hints next to the parameter/return descriptions instead of in the signature.

    Screenshot_20210305_125317

  4. added a commit that references this issue on Mar 6, 2021
    7c792e9
  5. nicoddemus commented on Mar 6, 2021

    @nicoddemus
    MemberAuthor

    Thanks for the suggestion! Opened #8407 with this change, let's see what others think.

  6. Tinche commented on Mar 6, 2021

    @Tinche
    Member

    Tangential: optional[union] doesn't make sense anyway, just add None to the union. That's what happens in runtime anyway iirc

  7. bluetech commented on Mar 6, 2021

    @bluetech
    Member

    The _pytest.code.code.ExceptionInfo would hopefully become just ExceptionInfo once we sort out its typing.

    We can change the _E to E or Exc; I suppose legibility is more important here than the privacy designation.

    Other than that it looks good to me for an API reference. Maybe some syntax highlighting would help, but that's up to Sphinx.

  8. bluetech commented on Mar 6, 2021

    @bluetech
    Member

    Tangential: optional[union] doesn't make sense anyway, just add None to the union. That's what happens in runtime anyway iirc

    I myself at least prefer to use Optional when the None is just some dummy default/disable value, and Union[..., None] when the None is an actual "legitimate" value (e.g. JsonPrimitive = Union[str, float, bool, None]). Although the new | syntax will make this moot.

    Speaking of |, it would also make this a little clearer display wise. Maybe Sphinx can swap Union with | already somehow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: docsdocumentation improvement, missing or needing clarification

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions