Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3.1k
Closed
Description
See the following example:
from typing import Any, Optional, TypeVarT = TypeVar("T")def assert_not_none(value: Optional[T]) -> T: assert value is not None return valuedef 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 thatfoo1 andfoo2 are equivalent. However, runningmypy on this file, I get:
test.py:12: error: Value of type <nothing> is not indexable
For some reason,assert_not_none doesn't mapOptional[Any] toAny, 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.