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

stubgen: use X | Y / X | None instead of Union[X, Y] / Union[X, None] #12920

Closed
eggplants opened this issue Jun 1, 2022 · 4 comments · Fixed by #16519
Closed

stubgen: use X | Y / X | None instead of Union[X, Y] / Union[X, None] #12920

eggplants opened this issue Jun 1, 2022 · 4 comments · Fixed by #16519
Labels

Comments

@eggplants
Copy link
Contributor

eggplants commented Jun 1, 2022

Feature

I would like to let stubgen use X | Y / X | None instead of Union[X, Y] / Union[X, None].

Pitch

Now:

# Python 3.10.4, mypy 0.960
$ cat test.py
import random


def a() -> int | str:
    if random.random() > 0.5:
        return 1
    else:
        return "hey!"

def b() -> str | None:
    if random.random() > 0.5:
        return "hey!"

$ stubgen test.py
Processed 1 modules
Generated out/test.pyi

$ cat out/test.pyi
def a() -> Union[int, str]: ...
def b() -> Union[str, None]: ...

Expect:

def a() -> int | str: ...
def b() -> str | None: ...
@AlexWaygood AlexWaygood added topic-stubgen topic-pep-604 PEP 604 (union | operator) labels Jun 1, 2022
@eggplants eggplants changed the title stubgen: use X | Y / X | None instead of Union[X, Y] / Optional[X] stubgen: use X | Y / X | None instead of Union[X, Y] / Union[X, None] Jun 1, 2022
@eggplants
Copy link
Contributor Author

eggplants commented Jun 2, 2022

Anyway, it is weird that from typing import Union is not inserted by stubgen...

@JelleZijlstra
Copy link
Member

@eggplants if stubgen doesn't import Union when it should, please open a separate issue with a reproducer. We should definitely fix that, and I suspect it'll be easy.

I'm a little more uncertain about this issue, since mypy still has a few bugs where it doesn't accept |, and it would be bad if stubgen generated stubs that mypy itself rejects.

@eggplants
Copy link
Contributor Author

@JelleZijlstra Thank you. Created: #12929

@AlexWaygood
Copy link
Member

I'm a little more uncertain about this issue, since mypy still has a few bugs where it doesn't accept |, and it would be bad if stubgen generated stubs that mypy itself rejects.

We could safely teach stubgen to use PEP 604 in non-runtime contexts. But that might be more complicated than it's worth; it might be better to wait until mypy has full PEP-604 support.

hamdanal added a commit to hamdanal/mypy that referenced this issue Feb 3, 2023
When a PEP 604 Union exists in the runtime, stubgen was generating a
`Union[...]` syntax without importing `Union` from `typing`. With this
change, stubgen preserves the ` | `-unions in the output.

Fixes python#12929
Closes python#13428
Ref python#12920
hauntsaninja pushed a commit that referenced this issue Feb 7, 2023
When a PEP 604 Union exists in the runtime, stubgen was generating a
`Union[...]` syntax without importing `Union` from `typing`. With this
change, stubgen preserves the ` | `-unions in the output.

Fixes #12929
Closes #13428
Ref #12920
@AlexWaygood AlexWaygood added topic-pep-604 PEP 604 (union | operator) and removed topic-pep-604 PEP 604 (union | operator) deferred labels Feb 10, 2023
hamdanal added a commit to hamdanal/mypy that referenced this issue Nov 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants