This code should be accepted by mypy:
def f(x: Union[int, str]) -> None: assert isinstance(x, int) x + 1 # type of x is int, so this should be okay
The type type checker should know about type assertions.
This could sometimes be used as an arguably more Pythonic replacement for casts.
I found a bunch of occurrences of this in our server code; bumping priority to 0.3.2.
Get type information from isinstance assertions
Get type information from isinstance assertions (#1346)
Activity
JukkaL commented on Oct 12, 2014
This could sometimes be used as an arguably more Pythonic replacement for casts.
gvanrossum commented on Mar 23, 2016
I found a bunch of occurrences of this in our server code; bumping priority to 0.3.2.
Get type information from isinstance assertions
Get type information from isinstance assertions (#1346)