-
Notifications
You must be signed in to change notification settings - Fork 1
Description
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 Noneis 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
-
examples: https://docs.python.org/3/library/functions.html#eval and https://docs.python.org/3/library/sqlite3.html#sqlite3.connect ↩
-
https://www.sphinx-doc.org/en/master/tutorial/describing-code.html#id5 ↩
-
https://www.sphinx-doc.org/en/master/usage/domains/python.html#info-field-lists ↩
-
https://github.com/python/cpython/pull/116389#issuecomment-1979766838 ↩ ↩2
-
https://github.com/python/cpython/pull/116389#issuecomment-1986203040 ↩