Skip to content

Request for decision: how should we mark up types in parameter lists #7

Description

@erlend-aasland

Recently, we've marked up some functions and methods using parameter lists1. When a parameter can take multiple types, we've used | to separate the types, instead of using or, as recommended by the Sphinx docs23:

Multiple types in a type field will be linked automatically if separated by the word “or”:

:type an_arg: int or None
:vartype a_var: str or int
:rtype: float or str

I created a PR to align our markup with the Sphinx recommendation, but it turned out slightly controversial.

I'll try and summarise the arguments in favour of |:

  • brevity
  • similar to type hints, so it is easy to grasp if you're into typing

Arguments against |:

  • similar, but not equal, to type hints, so it is confusing4

Arguments in favour of or:

  • easier for beginners: int or None is clear and contains no "weird symbols"4
  • aligns with "the Sphinx way"5

My personal preference would be or. I don't think the extra character is too verbose compared with |, I think it is easier to grasp for beginners (and I don't think it will be a hindrance for more experienced users), and I think it is wise to avoid the similarity with type hints because that's not what it is.

An example, where I think or is superior: "path-like object or None"

Footnotes

  1. examples: https://docs.python.org/3/library/functions.html#eval and https://docs.python.org/3/library/sqlite3.html#sqlite3.connect

  2. https://www.sphinx-doc.org/en/master/tutorial/describing-code.html#id5

  3. https://www.sphinx-doc.org/en/master/usage/domains/python.html#info-field-lists

  4. https://github.com/python/cpython/pull/116389#issuecomment-1979766838 2

  5. https://github.com/python/cpython/pull/116389#issuecomment-1986203040

Activity

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions