You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[ENH] Create an option --warn-never-instance such that creation of instances of Never is treated as an error. #15818
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):
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.
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
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.
Activity
[-][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.[/+]JelleZijlstra commentedon Aug 5, 2023
This isn't what
Neveris for. I think you want python/typing#1043.randolf-scholz commentedon Aug 5, 2023
@JelleZijlstra It isn't what the original
NoReturnis for. But again the documentation states thatNeverisCreating instances of the bottom type must therefore be an error. Calling a callable
Callable[..., X]returns an instance ofX. Consequently, calling a function of signatureCallable[..., Never]must be an error.