Skip to content
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

[RFE] Support union types specification using | (vertical bar/pipe) #1874

Closed
radekholy24 opened this issue May 5, 2015 · 4 comments
Closed
Labels
domains:py type:proposal a feature suggestion
Milestone

Comments

@radekholy24
Copy link

Please add a support for specifying multiple types acceptable for a parameter/attribute/variable.
Use case:
Imagine that there is a function that accepts both bytes and str. The docstring would look like:

def foo(text):
    """Bar

    :param text: a text
    :type text: bytes | str

    """

Such a syntax is already supported by e.g. PyCharm.

@zopieux
Copy link

zopieux commented Jul 11, 2017

From the docs:

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

So this feature is already available using this syntax:

def foo(foo):
    """
    Bla blu

    :type foo: str or int or None
    """

Instead of multiplying the possible syntaxes for this, why don't we try to adhere to PEP 484 and the typing module? These are already Python standards that are readily available in modern Python releases. I wouldn't take PyCharm as a reference for this kind of stuff, as they seem to enjoy never following the community's common practices.

def foo(foo):
    """
    Bla blu

    :type foo: Union[str, int, None]
    """

@radekholy24
Copy link
Author

A lot of time has passed since I filed this issue. I don't remember whether I specifically wanted the pipe syntax or whether it was just an example... I certainly didn't know about that PEP and that module didn't exist at that time as far as I know... As well as the paragraph in the documentation that you have mentioned... And that PyCharm's syntax was, for sure, the only syntax I knew... Maybe it was a mistake to suggest a particular implementation...

Now, it's enough for me that there is a support for union types in Sphinx already...

@ErikBjare
Copy link

ErikBjare commented May 21, 2021

The pipe syntax is now in the standards-track PEP 604.

I'm personally less interested in sphinx supporting type specification using pipes, but more so about the type annotations in the output using the pipe notation, saving the space taken by writing out the often-verbose Union, such that this:

image

would become this:

image

@tk0miya
Copy link
Member

tk0miya commented May 21, 2021

@ErikBjare Please check the latest Sphinx out with Python-3.10. The union types using pipes are displayed as-is.

@tk0miya tk0miya added domains:py type:proposal a feature suggestion labels May 21, 2021
@tk0miya tk0miya added this to the 4.1.0 milestone May 21, 2021
tk0miya added a commit that referenced this issue May 22, 2021
…list

Close #1874: py domain: Support union types using `|` in info-field-list
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
domains:py type:proposal a feature suggestion
Projects
None yet
Development

No branches or pull requests

4 participants