Skip to content

Docs: In param lists mark up multiple types correctly #116389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

erlend-aasland
Copy link
Contributor

@erlend-aasland erlend-aasland commented Mar 5, 2024

Use the word 'or' instead of the symbol '|'.

See also the Sphinx docs:


📚 Documentation preview 📚: https://cpython-previews--116389.org.readthedocs.build/

Use the word 'or' instead of the symbol '|'.
@erlend-aasland erlend-aasland changed the title Docs: In param lists Docs: In param lists mark up multiple types correctly Mar 5, 2024
@AlexWaygood
Copy link
Member

My first reaction was "but these aren't valid Python type hints anymore!"

My second reaction was to go... "Wait, he's right, these are probably much more readable for casual readers of the documentation who might not be familiar with Python's typing system" :)


:param exc_info: An exception tuple with the current exception information,
as returned by :func:`sys.exc_info`,
or ``None`` if no exception information is available.
:type exc_info: tuple[type[BaseException], BaseException, types.TracebackType] | None
:type exc_info: tuple[type[BaseException], BaseException, types.TracebackType] or None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this one still perhaps a bit too complex for the docs? Not sure how it could be simplified, though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is definitely complex. In this case, I'd say readability is definitely improved using or iso. |.

@hugovk
Copy link
Member

hugovk commented Mar 6, 2024

I checked the first one:

Before After
image image

Both are linked and marked up the same. Are there some instances where there's a difference (other than "|" vs. "or")?

Should we be formatting None as None? https://devguide.python.org/documentation/markup/#quick-reference

@erlend-aasland
Copy link
Contributor Author

Both are linked and marked up the same. Are there some instances where there's a difference (other than "|" vs. "or")?

@hugovk: indeed, it looks like they are all the same. I also tried just using a comma (:type name: str, dict), which is also marked up like this. Looks like it just boils down to a style issue. Do we want it written as str or dict, or str | dict? See also @AlexWaygood's comment: #116389 (comment).

@hugovk
Copy link
Member

hugovk commented Mar 8, 2024

No strong preference either way.

In theory, if Sphinx says to do it one way, it makes sense to do that. In practice, if it makes no difference, let's pick the one we prefer. So I'll leave it up to you ;)

@erlend-aasland
Copy link
Contributor Author

erlend-aasland commented Mar 9, 2024

In theory, if Sphinx says to do it one way, it makes sense to do that. In practice, if it makes no difference, let's pick the one we prefer. So I'll leave it up to you ;)

It's a minor style issue to decide on, nevertheless it feels wrong for me to decide on the convention in a PR. I think at least a heads-up in the Documentation category on Discourse would be nice :)

UPDATE: I created a topic on Discourse.

@erlend-aasland erlend-aasland marked this pull request as draft March 12, 2024 13:54
@erlend-aasland
Copy link
Contributor Author

Marked as draft until a decision has been made.

@hugovk hugovk removed the needs backport to 3.11 only security fixes label Apr 11, 2024
@serhiy-storchaka serhiy-storchaka added the needs backport to 3.13 bugs and security fixes label May 9, 2024
@picnixz
Copy link
Member

picnixz commented Jun 6, 2024

In theory, if Sphinx says to do it one way, it makes sense to do that

Well... I don't remember whether there are corner cases where we fail to render correctly the docs (I know that there is an open issue in Sphinx about something related and I know it's something I've replied to but I don't remember the issue :D).

Personally, I tend to use | for the sole reason that it's shorter on the docs side. However, using "or" may be clearer for users in general because some of your types might have "|" inside them, e.g., list[int | str] | str and you need a bit more effort to detect which | is the one actually separating the two types.

For CPython, I'd say that using or is fine and possibly preferrable for end-users and beginners.

@erlend-aasland
Copy link
Contributor Author

We discussed this at the docs meet-up recently, and there was many of arguments in favour of or. We landed on that we'll create an issue on the EB repo and let them decide.

@erlend-aasland erlend-aasland marked this pull request as ready for review November 5, 2024 22:26
@erlend-aasland erlend-aasland requested a review from a team November 5, 2024 22:26
@erlend-aasland
Copy link
Contributor Author

Based on python/editorial-board#7 (comment), I suspect the EB is ok with this PR. If so, let's land this and follow up with a devguide PR, to document this recommended practise.

@erlend-aasland
Copy link
Contributor Author

Take a look at the CI failure for commit d464f3f. It seems like Sphinx do not support its own convention for attributes. If this is really the case, we should definitely not land this PR, and instead recommend continued use of |, or to fix Sphinx.

cc. Sphinx wizard @AA-Turner 🪄

@picnixz
Copy link
Member

picnixz commented Nov 6, 2024

Huh, maybe it requires Napoleon's extension for that to work? I don't remember where we parse the or or |.

AA-Turner and others added 2 commits January 14, 2025 22:08
This reverts commit 577c52b.

This should trigger the error I saw earlier.
@electric-coder
Copy link

electric-coder commented Jan 21, 2025

@erlend-aasland basically what you're asking for is an historical disambiguation (initially motivated by a syntactic error in this PR) that can't be arbitrated by Sphinx devs alone because the acceptable values in those fields are multi-language and predate PEP 484 type expressions as we know them.

The closest things is the napoleon extension intro on docstrings and a comprehensive historical rewrite to bring it up to date has not yet been proposed. Also, the specific numpy docstring style syntax is sufficiently ambiguous/underspecified (you'll rarely find full explanations of default values in APIs - or a complete example in their docs) and more or less independent.

Every beginner who ever tried to write a docstring fell into this problem, but it's an extremely hard document to write because even the use of prose like types (int or str) is kind of an historical artifact that lacks an authoritative document to explain it (I've commented on the Sphinx repo it would take an archeologist), be that in the Sphinx docs, the PEPs, or numpydoc.

@erlend-aasland
Copy link
Contributor Author

The EB landed on preferring | iso. or. Closing this long-lived PR.

@erlend-aasland erlend-aasland deleted the docs/param-list-type-markup branch February 22, 2025 21:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

8 participants