Skip to content

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

Merged
charliermarsh merged 1 commit intomainfrom
charlie/no-return
Dec 20, 2023
Merged

Add support for NoReturn in auto-return-typing#9206
charliermarsh merged 1 commit intomainfrom
charlie/no-return

Conversation

@charliermarsh
Copy link
Copy Markdown
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
@charliermarsh charliermarsh deleted the charlie/no-return branch December 20, 2023 05:06
@github-actions
Copy link
Copy Markdown
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
Copy Markdown

tooruu commented Dec 20, 2023

Does this use typing.Never in Python 3.11+?

@max-muoto
Copy link
Copy Markdown
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
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in 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