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
fromtypingimportNoReturn# Incompatible types in assignment (expression has type "object", variable has type "Type[NoReturn]") [assignment]foo: type[NoReturn] =NoReturn
NoReturn isn't a valid runtime type. You should think of it as equivalent to a Union with no subtypes. It doesn't make sense to use it within a runtime isinstance check, just like it would make sense to use Union, Any, or TypeGuard within an isinstance check.
That change was added in 3.10 to support isinstance(1, int | str), which is the equivalent of isinstance(1, (int, str)). Those both have valid meanings. But isinstance(1, Union) and isinstance(1, NoReturn) do not.
That change was added in 3.10 to support isinstance(1, int | str), which is the equivalent of isinstance(1, (int, str)). Those both have valid meanings. But isinstance(1, Union) and isinstance(1, NoReturn) do not.
true, though i would expect isinstance(1, NoReturn) to be equivalent to isinstance(1, ()), but obviously that's out of mypy's control
You're attempting to use NoReturn well outside of the use case for which it was designed, so it's not surprising that it doesn't work in these cases. NoReturn is a construct that is meant to be used at type checking time to designate the return type of a function that never returns — e.g., executes an infinite loop, raises exceptions on all code paths, or terminates the process. If you use it for its intended purpose, it works fine.
as you said on microsoft/pyright#2299 (comment) there are more meanings to NoReturn, such as empty unions. do you think that Never type you mentioned, if added, would support use cases like mine?
I can't say because I don't understand what you're trying to do, and I can't tell from the sample above. Could you provide more context? The best I can discern is that you're trying to use NoReturn as a runtime sentinel of sorts, but I'm not sure why None wouldn't be a logical choice.
In the previous discussions about Never, the thought was that it would simply be an alias for NoReturn, so the two would work in the same manner.
not that big a deal i guess. in my case i can easily just use None, it's just that i felt NoReturn was more suitable given the context - a class with a method that instantiates a T or throws an error
Maybe this should be reconsidered, as Never, is now officially both considered the bottom type and an alias of NoReturn.
The bottom type itself is supposed to have no members, so, one might consider raising an error whenever an instance of Never is produced, but using the type in and of itself should just work like any other type, no? x: type[Any] = ... is also allowed, after all.
Never might also result from incompatible intersections, as is currently discussed (e.g. int & str=Never). So possibly in the future, isinstance(x, A & B) and issubclass(x, A & B) will be allowed, just like isinstance(x, A | B) and issubclass(x, A | B) are today.
Activity
KotlinIsland commentedon Oct 7, 2021
NoReturnis a type realm type, not an instance of the runtimetype. It's actually an instance of_SpecialForm.JelleZijlstra commentedon Oct 9, 2021
We should perhaps reject
type[NoReturn]. I'm not sure what that would even mean.DetachHead commentedon Oct 9, 2021
i don't see why it'd be any different to something like this, which works
my use case is a class with reified generics at runtime. something like this:
erictraut commentedon Oct 9, 2021
NoReturnisn't a valid runtime type. You should think of it as equivalent to a Union with no subtypes. It doesn't make sense to use it within a runtimeisinstancecheck, just like it would make sense to useUnion,Any, orTypeGuardwithin anisinstancecheck.DetachHead commentedon Oct 9, 2021
you can use
Unionin anisinstancecheck though (as of 3.10 i believe)erictraut commentedon Oct 9, 2021
That change was added in 3.10 to support
isinstance(1, int | str), which is the equivalent ofisinstance(1, (int, str)). Those both have valid meanings. Butisinstance(1, Union)andisinstance(1, NoReturn)do not.erictraut commentedon Oct 9, 2021
For what it's worth, the runtime type of the expression
type(NoReturn)is actuallytyping._SpecialForm.[-]`NoReturn` type incorrectly widened to `object`[/-][+]Disallow `type[NoReturn]`[/+]JelleZijlstra commentedon Oct 9, 2021
I am going to repurpose this issue to track warning against usage of
type[NoReturn].For what it's worth, pyright doesn't warn against this either (or perhaps I don't have the right option on).
DetachHead commentedon Oct 9, 2021
true, though i would expect
isinstance(1, NoReturn)to be equivalent toisinstance(1, ()), but obviously that's out of mypy's controlerictraut commentedon Oct 9, 2021
You're attempting to use
NoReturnwell outside of the use case for which it was designed, so it's not surprising that it doesn't work in these cases.NoReturnis a construct that is meant to be used at type checking time to designate the return type of a function that never returns — e.g., executes an infinite loop, raises exceptions on all code paths, or terminates the process. If you use it for its intended purpose, it works fine.DetachHead commentedon Oct 9, 2021
as you said on microsoft/pyright#2299 (comment) there are more meanings to
NoReturn, such as empty unions. do you think thatNevertype you mentioned, if added, would support use cases like mine?erictraut commentedon Oct 9, 2021
I can't say because I don't understand what you're trying to do, and I can't tell from the sample above. Could you provide more context? The best I can discern is that you're trying to use
NoReturnas a runtime sentinel of sorts, but I'm not sure whyNonewouldn't be a logical choice.In the previous discussions about
Never, the thought was that it would simply be an alias forNoReturn, so the two would work in the same manner.DetachHead commentedon Oct 9, 2021
not that big a deal i guess. in my case i can easily just use
None, it's just that i feltNoReturnwas more suitable given the context - a class with a method that instantiates aTor throws an errorKotlinIsland commentedon Oct 9, 2021
also disallow
type[Union]type[Generic]type[ClassVar]etc.randolf-scholz commentedon Jul 29, 2023
Maybe this should be reconsidered, as
Never, is now officially both considered the bottom type and an alias ofNoReturn.The bottom type itself is supposed to have no members, so, one might consider raising an error whenever an instance of Never is produced, but using the type in and of itself should just work like any other type, no?
x: type[Any] = ...is also allowed, after all.Nevermight also result from incompatible intersections, as is currently discussed (e.g.int & str=Never). So possibly in the future,isinstance(x, A & B)andissubclass(x, A & B)will be allowed, just likeisinstance(x, A | B)andissubclass(x, A | B)are today.