Closed
Description
See the following example:
from typing import Any, Optional, TypeVar
T = TypeVar("T")
def assert_not_none(value: Optional[T]) -> T:
assert value is not None
return value
def foo1(a: Optional[Any]) -> int:
return assert_not_none(a)[3]
def foo2(a: Optional[Any]) -> int:
assert a is not None
return a[3]
I would expect that foo1
and foo2
are equivalent. However, running mypy
on this file, I get:
test.py:12: error: Value of type <nothing> is not indexable
For some reason, assert_not_none
doesn't map Optional[Any]
to Any
, as it is supposed to (and which would accept the indexing), but instead maps it to <nothing>
which throws an error. The second version with asserting that it is not None seems to work fine, though.
Activity
msullivan commentedon May 20, 2020
Definitely a bug and a somewhat surprising one too. Probably an issue in constraints.py?
ilevkivskyi commentedon Feb 8, 2025
The root cause is:
(and same with
Optional[Any]
) because of loose handling of unions inconstraints.py
. This is pretty bad IMO, so raising priority to high.[-]Value of type <nothing> is not indexable[/-][+]Inference of Any against optional type infers Never[/+]JukkaL commentedon Apr 30, 2025
I encountered this when preparing an internal codebase for the 1.16 release. The issue wasn't directly related to any change in 1.16, though -- some additional
Any | None
types are inferred after recent changes.[-]Inference of Any against optional type infers Never[/-][+][1.16 makes more common] Inference of Any against optional type infers Never[/+]Infer constraints eagerly if actual is Any (#19190)
2 remaining items