[ENH] Create an option --warn-never-instance
such that creation of instances of Never
is treated as an error.
#15818
Labels
--warn-never-instance
such that creation of instances of Never
is treated as an error.
#15818
Feature
Originally,
NoReturn
was introduced to type hint functions that raise an Exception unconditionally. In python 3.11, thetyping.Never
was introduced asSince 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 ofSequence[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 flagx = foo("abc")
with a message likeWhat 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.
NoReturn
andNever
two separate things againNever
could be made generic such that one specializeNever[TypeError]
,Never[ValueError]
,Never[SystemExit]
, etc. (or, introduce such a construct, e.g.Raises[TypeError]
)Then, if sayfoo(args)
is annotated asValueError
, a type-checker could decide to not raise if the instance creation happened within the context of an appropriatetry
-except
-block.The text was updated successfully, but these errors were encountered: