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
generic fails to narrow, something to do with using NoReturn on contravariant generic #11508
changed the title [-]dict value type fails to narrow when it can't be inferred for all values in the dict, when bounded to a type with a contravariant generic bounded to `NoReturn`[/-][+]dict value type fails to narrow when it can't be inferred for all its values, when bounded to a type with a contravariant generic bounded to `NoReturn`[/+]on Nov 10, 2021
You pointed out that "the error goes away if you change this to [foo]". That looks like a bug. It should be an error in this case too. Keep in mind that no type is assignable to NoReturn, and NoReturn is not assignable to any type.
Also, PEP 484 specifically says:
The NoReturn type is only valid as a return annotation of functions, and considered an error if it appears in other positions.
So mypy should generate an error for the type annotation Sequence[Foo[NoReturn]]].
changed the title [-]dict value type fails to narrow when it can't be inferred for all its values, when bounded to a type with a contravariant generic bounded to `NoReturn`[/-][+]generic fails to narrow, something to do with using `NoReturn` on contravariant generic[/+]on Nov 10, 2021
I'm not sure what you mean by "remove a lot of functionality it has implemented around NoReturn". I'm just pointing out that the semantics of NoReturn are well documented in PEP 484. Has mypy implemented functionality that is in conflict with the specification?
Yes, you're right that NoReturn does act like a Never type. This is useful for explicit validation of exhaustive completion checks, like the following:
You can find more details about this use case if you search the issue tracker for assert_never.
I guess this is a use case that PEP 484 didn't anticipate and technically doesn't allow (since NoReturn is being used in this context as a parameter type annotation, not a return type annotation). To my knowledge, this exception to PEP 484 is support by all Python type checkers.
This works because of the property I mentioned above. The only type that can be assigned to NoReturn is NoReturn (or Never, which is another name for the same type).
Activity
[-]dict value type fails to narrow when it can't be inferred for all values in the dict, when bounded to a type with a contravariant generic bounded to `NoReturn`[/-][+]dict value type fails to narrow when it can't be inferred for all its values, when bounded to a type with a contravariant generic bounded to `NoReturn`[/+]KotlinIsland commentedon Nov 10, 2021
🤔
erictraut commentedon Nov 10, 2021
You pointed out that "the error goes away if you change this to [foo]". That looks like a bug. It should be an error in this case too. Keep in mind that no type is assignable to
NoReturn, andNoReturnis not assignable to any type.Also, PEP 484 specifically says:
So mypy should generate an error for the type annotation
Sequence[Foo[NoReturn]]].[-]dict value type fails to narrow when it can't be inferred for all its values, when bounded to a type with a contravariant generic bounded to `NoReturn`[/-][+]generic fails to narrow, something to do with using `NoReturn` on contravariant generic[/+]KotlinIsland commentedon Nov 10, 2021
So mypy should remove a lot of functionality it has implemented around
NoReturnto act like an uninhabited type?erictraut commentedon Nov 10, 2021
I'm not sure what you mean by "remove a lot of functionality it has implemented around
NoReturn". I'm just pointing out that the semantics ofNoReturnare well documented in PEP 484. Has mypy implemented functionality that is in conflict with the specification?DetachHead commentedon Nov 10, 2021
mypy lets you use it like a
Nevertype(relevant discussion from a previous issue i raised: #11291 (comment))
erictraut commentedon Nov 10, 2021
Yes, you're right that
NoReturndoes act like aNevertype. This is useful for explicit validation of exhaustive completion checks, like the following:You can find more details about this use case if you search the issue tracker for
assert_never.I guess this is a use case that PEP 484 didn't anticipate and technically doesn't allow (since
NoReturnis being used in this context as a parameter type annotation, not a return type annotation). To my knowledge, this exception to PEP 484 is support by all Python type checkers.This works because of the property I mentioned above. The only type that can be assigned to
NoReturnisNoReturn(orNever, which is another name for the same type).Use union of current context and left side for right side narrowing o…