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

[ENH] Create an option --warn-never-instance such that creation of instances of Never is treated as an error. #15818

Closed
randolf-scholz opened this issue Aug 5, 2023 · 2 comments
Labels

Comments

@randolf-scholz
Copy link
Contributor

randolf-scholz commented Aug 5, 2023

Feature

Originally, NoReturn was introduced to type hint functions that raise an Exception unconditionally. In python 3.11, the typing.Never was introduced as

The bottom type, a type that has no members.

Since the type has no member, a warning should be produced whenever an instance of Never is created. To not disrupt the alternative usage, this may be first introduced as an optional flag.

Pitch

So why is this useful? A notorious example is that current type hinting has issues to represent the idea of a sequence of strings, because str itself is a subtype of Sequence[str], but typically in such applications we do not want bare strings. (cf. python/typing#256).

Raising an error on creation of instances of Never allows us to circumvent this problem, at least for the case of function arguments (mypy-play):

from typing import Never, overload, Sequence

@overload
def foo(x: str) -> Never:...
@overload
def foo(x: Sequence[str]) -> str: ...

def foo(x):
    return " ".join(x)
    
x = foo("abc")
y = foo(["abra", "cadabra"])
reveal_type(x)  # <nothing>
reveal_type(y)  # str

With the --warn-never-instance, mypy would flag x = foo("abc") with a message like

function foo retuned Never, idindicating illegal usage

What to do about the original intention of NoReturn?

I am not sure, but here are some ideas spitballed, but this is possibly better to discuss in python/typing, or typing/ideas. As long as the flag is optional at first, it shouldn't matter too much.

  • One could attempt to make NoReturn and Never two separate things again
  • Never could be made generic such that one specialize Never[TypeError], Never[ValueError], Never[SystemExit], etc. (or, introduce such a construct, e.g. Raises[TypeError])Then, if say foo(args) is annotated as ValueError, a type-checker could decide to not raise if the instance creation happened within the context of an appropriate try-except-block.
@randolf-scholz randolf-scholz changed the title [ENH] Create a flag --warn-never-instance that raises a warning whenever an instance of Never is created. [ENH] Create an option --warn-never-instance such that creation of instances of Never is treated as an error. Aug 5, 2023
@JelleZijlstra
Copy link
Member

This isn't what Never is for. I think you want python/typing#1043.

@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Aug 5, 2023
@randolf-scholz
Copy link
Contributor Author

randolf-scholz commented Aug 5, 2023

@JelleZijlstra It isn't what the original NoReturn is for. But again the documentation states that Never is

The bottom type, a type that has no members.

Creating instances of the bottom type must therefore be an error. Calling a callable Callable[..., X] returns an instance of X. Consequently, calling a function of signature Callable[..., Never] must be an error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants