mypy icon indicating copy to clipboard operation
mypy copied to clipboard

`isinstance` doesn't narrow when using union

Open DetachHead opened this issue 3 years ago • 3 comments

a: int | str
assert isinstance(a, int | None)
reveal_type(a)

playground

DetachHead avatar Aug 17 '22 05:08 DetachHead

This seems specific to | None. If you do assert isinstance(a, int | list) instead, it correctly narrows to int.

JelleZijlstra avatar Aug 19 '22 03:08 JelleZijlstra

@SakshiSingh25 Start here: https://github.com/python/mypy/blob/master/CONTRIBUTING.md

This issue is likely due to strict_optional in some way, so search for that.

KotlinIsland avatar Sep 09 '22 02:09 KotlinIsland