Skip to content
New issue

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

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

Already on GitHub? # to your account

Add support for NoReturn in auto-return-typing #9206

Merged
merged 1 commit into from
Dec 20, 2023
Merged

Conversation

charliermarsh
Copy link
Member

Summary

Given a function like:

def func(x: int):
    if not x:
        raise ValueError
    else:
        raise TypeError

We now correctly use NoReturn as the return type, rather than None.

Closes #9201.

@charliermarsh charliermarsh changed the title Add support for NoReturn in auto-return-typing Add support for NoReturn in auto-return-typing Dec 20, 2023
@charliermarsh charliermarsh added the bug Something isn't working label Dec 20, 2023
@charliermarsh charliermarsh merged commit 5ccc21a into main Dec 20, 2023
17 checks passed
@charliermarsh charliermarsh deleted the charlie/no-return branch December 20, 2023 05:06
Copy link
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@tooruu
Copy link

tooruu commented Dec 20, 2023

Does this use typing.Never in Python 3.11+?

@max-muoto
Copy link
Contributor

max-muoto commented Dec 20, 2023

Does this use typing.Never in Python 3.11+?

They're effectively identical in regards to type-checkers. Pyright (for Python 3.11+) infers the correct return type for a function like the one given in the example as NoReturn. typing.Never is used as a bottom type in situations like this:

def int_or_str(arg: int | str) -> None:
    never_call_me(arg)  # type checker error
    match arg:
        case int():
            print("It's an int")
        case str():
            print("It's a str")
        case _: # type checkers considers this unreachable
            never_call_me(arg)  # Pyright would say `arg` is of type `Never`.

@tooruu
Copy link

tooruu commented Dec 22, 2023

Does this use typing.Never in Python 3.11+?

They're effectively identical in regards to type-checkers. Pyright (for Python 3.11+) infers the correct return type for a function like the one given in the example as NoReturn. typing.Never is used as a bottom type in situations like this:

def int_or_str(arg: int | str) -> None:
    never_call_me(arg)  # type checker error
    match arg:
        case int():
            print("It's an int")
        case str():
            print("It's a str")
        case _: # type checkers considers this unreachable
            never_call_me(arg)  # Pyright would say `arg` is of type `Never`.

Official Python docs state that starting in 3.11 typing.Never should be used instead of typing.NoReturn. Looks like this has been addressed by #9213.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Autofix for ANN 201 incorrectly adds None Return Type
3 participants