Skip to content

Recognize isinstance(a[b], ...)? #2458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JukkaL opened this issue Nov 15, 2016 · 1 comment
Closed

Recognize isinstance(a[b], ...)? #2458

JukkaL opened this issue Nov 15, 2016 · 1 comment

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 15, 2016

Consider this fragment:

from typing import Union, Dict

x = {}  # type: Dict[str, Union[int, str]]
n = ''
if isinstance(x[n], int):
    reveal_type(x[n])  # E: Revealed type is 'Union[builtins.int, builtins.str]'

Perhaps mypy should infer type int for x[n] (i.e. put x[n] into the conditional type binder).

The example can be refactored to type check pretty easily:

...
t = x[n]
if isinstance(t, int):
    ...

A slightly more interesting example that is a bit harder to get to type check:

...
if n in x and isinstance(x[n], int):
    ...

This was originally reported by @davire on gitter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants