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
sphinx.domains.python._parse_annotation should improve formatting of Union, Optional, Literal, constants #9643
Comments
I think the former proposal is cool. It's good for me. +1 for adding the option. At this moment, some libraries and applications must support old intepreters that does not supporting type union operator. So it's useful until the deprecation of them. On the other hand, I'm not sure another proposal is really good. The converted code is not valid in Python. I agree "Literal" text is not meaningless in the document. But I'm afraid the invalid code confuses readers. Could you show any rendered example? I'd like to see a real example. |
Here is an example of the https://google.github.io/tensorstore/python/api/tensorstore.TensorStore.read.html#p-order The While it is invalid Python syntax, it is unambiguous, except in the case of |
Thank you for the pointer. Indeed, it's not so bad. Personally, I prefer using @shimizukawa Do you have any comment for this? |
+1 from me too.
+0. |
@jbms #11072 does this for Optional and Union; though avoids changing Literal as no "short" display mechanism has been adopted for upstream Python use. I'd be interested in your view here of what (if anything) we should do re literals -- add an option for the short display format you propose, do nothing, etc. Thanks! A |
Thanks for adding the optional/union change. For literal I think it would make sense to add a config option that defaults to false, as we've done in sphinx-immaterial: Note that the concise literal formatting is unambiguous when used in documentation, because we never represent type names as string literals. However, in Python source code, a real string literal would be ambiguous with specifying a type name as a string literal. More generally, it would also be nice if there were a mechanism for extensions to manipulate the ast of type annotations before they are formatted, either a sphinx event or something else, since it is somewhat difficult to accomplish that with monkey patching. |
This is a sub-issue of #9523 split off here.
When displayed normally,
Union
,Optional
, andLiteral
add a lot of noise to the type signature and obscure the important information. Instead, it is much cleaner to display them using the PEP 604 (https://www.python.org/dev/peps/pep-0604/) syntax:Union[X, Y, Z]
->X | Y | Z
Optional[X]
->X | None
Additionally, for
Literal
it is cleaner to strip the text "Literal" and just display the literal value with normal Python syntax highlighting:Literal["X"]
->"X"
This is implemented in the tensorstore documentation via an ast transformation:
https://github.com/google/tensorstore/blob/1a59fcb310bc1feb13569f03f7134b4c3a5fa5f4/docs/tensorstore_sphinx_ext/autodoc.py#L259
This should be supported in Sphinx via a config option. The other improvement, of using syntax highlighting for constants, should also be integrated.
The text was updated successfully, but these errors were encountered: