Skip to content

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

Closed as not planned

Description

@randolf-scholz

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.

Activity

  1. 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.[/+] on Aug 5, 2023
  2. JelleZijlstra commented on Aug 5, 2023

    @JelleZijlstra
    Member

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

  3. randolf-scholz commented on Aug 5, 2023

    @randolf-scholz
    ContributorAuthor

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions