Skip to content
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

Type detection doesn't work with TypedDict #7344

Closed
priyank-p opened this issue Aug 14, 2019 · 4 comments · Fixed by #7917
Closed

Type detection doesn't work with TypedDict #7344

priyank-p opened this issue Aug 14, 2019 · 4 comments · Fixed by #7917

Comments

@priyank-p
Copy link

  • Are you reporting a bug, or opening a feature request? Bug
  • Code that reproduces the issue:
from typing import Union, List, Iterable
from typing_extensions import Literal, TypedDict

# Goal here is to add mypy type so that
# if the dict's key is 'a' or 'b' mypy knows
# that the value should be int or str.
ABDictT = TypedDict('ABDictT', {
    'key': Literal['a', 'b'],
    'value': Union[str, int]
})

# If key is 'c', or 'd' then mypy should know
# value should be str, or int list.
CDDictT = TypedDict('CDDictT', {
    'key': Literal['c', 'd'],
    'value': Union[str, Iterable[int]],
})

ValueDictElementT = Union[ABDictT, CDDictT]
ValuesDictT = Iterable[ValueDictElementT]

def some_func(values: ValuesDictT) -> None:
    for element in values:
        if element['key'] == 'a':
            # The reveal_type here show the type to be
            # Union[str, int, Iterable[int]] since we
            # did a check that element['key'] is 'a' mypy
            # should be able to tell that the type is
            # ABDictT here?
            reveal_type(element['value'])
  • What is the actual behavior/output?
$ mypy --strict repro.py
repro.py:30: note: Revealed type is 'Union[builtins.str, builtins.int, typing.Iterable[builtins.int]]'
  • What is the behavior/output you expect?
    mypy should be able to tell that the type is ABDictT after checking that the key is a and therefore the output on the reveal_type should be Union[str, int].
$ mypy --strict repro.py
repro.py:30: note: Revealed type is 'Union[builtins.str, builtins.int]'
  • What are the versions of mypy and Python you are using? Python: 3.6.8, mypy: 0.720
    Do you see the same issue after installing mypy from Git master? Yes
  • What are the mypy flags you are using? (For example --strict-optional) I did use --strict but it can also be reproduced with no flags.
@priyank-p
Copy link
Author

I also figured out a simpler code the reproduces this:

from typing import Union, Iterable
from typing_extensions import TypedDict

Dict1T = TypedDict('Dict1T', {
    'key': str,
    'value': int
})

Dict2T = TypedDict('Dict2T', {
    'key': int,
    'value': Iterable[int]
})

ValuesDictElement = Union[Dict1T, Dict2T]
ValuesDictT = Iterable[ValuesDictElement]

def some_func(values: ValuesDictT) -> None:
    for element in values:
        if isinstance(element['key'], str):
            reveal_type(element['value'])

@priyank-p priyank-p changed the title mypy doesn't recognize type based TypedDict type Type detection doesn't work with TypedDict Aug 14, 2019
@JelleZijlstra
Copy link
Member

There was a recent bug report asking for something like this in a simpler form (e.g. Union[Tuple[A, B], Tuple[C, D]], where you isinstance the first element of the tuple and expect mypy to know the type of the second element). It's unlikely that mypy will support this.

@ilevkivskyi
Copy link
Member

ilevkivskyi commented Aug 15, 2019

Actually, I think we might want to special-case typed dicts, because one can't use isinstance() to select an item from union as one would do with other classes.

Also there is somewhat similar pattern in TypeScript where one can use a string literal to select an item from union.

@ilevkivskyi
Copy link
Member

cc @Michael0x2a

Michael0x2a added a commit to Michael0x2a/mypy that referenced this issue Nov 9, 2019
This diff adds support for the following pattern:

```python
from typing import Enum, List
from enum import Enum

class Key(Enum):
    A = 1
    B = 2

class Foo:
    key: Literal[Key.A]
    blah: List[int]

class Bar:
    key: Literal[Key.B]
    something: List[str]

x: Union[Foo, Bar]
if x.key is Key.A:
    reveal_type(x)  # Revealed type is 'Foo'
else:
    reveal_type(x)  # Revealed type is 'Bar'
```

In short, when we do `x.key is Key.A`, we "propagate" the information
we discovered about `x.key` up one level to refine the type of `x`.

We perform this propagation only when `x` is a Union and only when we
are doing member or index lookups into instances, typeddicts,
namedtuples, and tuples. For indexing operations, we have one additional
limitation: we *must* use a literal expression in order for narrowing
to work at all. Using Literal types or Final instances won't work;
See python#7905 for more details.

To put it another way, this adds support for tagged unions, I guess.

This more or less resolves python#7344.
We currently don't have support for narrowing based on string or int
literals, but that's a separate issue and should be resolved by
python#7169 (which I resumed work
on earlier this week).
Michael0x2a added a commit to Michael0x2a/mypy that referenced this issue Nov 9, 2019
This diff adds support for the following pattern:

```python
from typing import Enum, List
from enum import Enum

class Key(Enum):
    A = 1
    B = 2

class Foo:
    key: Literal[Key.A]
    blah: List[int]

class Bar:
    key: Literal[Key.B]
    something: List[str]

x: Union[Foo, Bar]
if x.key is Key.A:
    reveal_type(x)  # Revealed type is 'Foo'
else:
    reveal_type(x)  # Revealed type is 'Bar'
```

In short, when we do `x.key is Key.A`, we "propagate" the information
we discovered about `x.key` up one level to refine the type of `x`.

We perform this propagation only when `x` is a Union and only when we
are doing member or index lookups into instances, typeddicts,
namedtuples, and tuples. For indexing operations, we have one additional
limitation: we *must* use a literal expression in order for narrowing
to work at all. Using Literal types or Final instances won't work;
See python#7905 for more details.

To put it another way, this adds support for tagged unions, I guess.

This more or less resolves python#7344.
We currently don't have support for narrowing based on string or int
literals, but that's a separate issue and should be resolved by
python#7169 (which I resumed work
on earlier this week).
Michael0x2a added a commit to Michael0x2a/mypy that referenced this issue Nov 9, 2019
This diff adds support for the following pattern:

```python
from typing import Enum, List
from enum import Enum

class Key(Enum):
    A = 1
    B = 2

class Foo:
    key: Literal[Key.A]
    blah: List[int]

class Bar:
    key: Literal[Key.B]
    something: List[str]

x: Union[Foo, Bar]
if x.key is Key.A:
    reveal_type(x)  # Revealed type is 'Foo'
else:
    reveal_type(x)  # Revealed type is 'Bar'
```

In short, when we do `x.key is Key.A`, we "propagate" the information
we discovered about `x.key` up one level to refine the type of `x`.

We perform this propagation only when `x` is a Union and only when we
are doing member or index lookups into instances, typeddicts,
namedtuples, and tuples. For indexing operations, we have one additional
limitation: we *must* use a literal expression in order for narrowing
to work at all. Using Literal types or Final instances won't work;
See python#7905 for more details.

To put it another way, this adds support for tagged unions, I guess.

This more or less resolves python#7344.
We currently don't have support for narrowing based on string or int
literals, but that's a separate issue and should be resolved by
python#7169 (which I resumed work
on earlier this week).
Michael0x2a added a commit that referenced this issue Nov 13, 2019
This diff adds support for the following pattern:

```python
from typing import Enum, List
from enum import Enum

class Key(Enum):
    A = 1
    B = 2

class Foo:
    key: Literal[Key.A]
    blah: List[int]

class Bar:
    key: Literal[Key.B]
    something: List[str]

x: Union[Foo, Bar]
if x.key is Key.A:
    reveal_type(x)  # Revealed type is 'Foo'
else:
    reveal_type(x)  # Revealed type is 'Bar'
```

In short, when we do `x.key is Key.A`, we "propagate" the information
we discovered about `x.key` up to refine the type of `x`.

We perform this propagation only when `x` is a Union and only when we
are doing member or index lookups into instances, typeddicts,
namedtuples, and tuples. For indexing operations, we have one additional
limitation: we *must* use a literal expression in order for narrowing
to work at all. Using Literal types or Final instances won't work;
See #7905 for more details.

To put it another way, this adds support for tagged unions, I guess.

This more or less resolves #7344.
We currently don't have support for narrowing based on string or int
literals, but that's a separate issue and should be resolved by
#7169 (which I resumed work
on earlier this week).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants