-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
mypy infers conditional expression of Dict[str, object] to be object instead of union #7835
Comments
Yeah, it would be better if the inferred type for |
Since I worked on #7780 before, I believe the problem is located in the code snippet below Lines 3567 to 3571 in 3b5a62e
There's no explicit type annotation to declare registry as a Union, so mypy takes the else branch here and simply join the two types from cond expr into an object typeA simple workaround for @scascketta 's case would be: from typing import Dict
from dataclasses import dataclass
@dataclass
class Foo:
bar: bool
@dataclass
class Bar:
baz: bool
foo_registry: Dict[str, Foo] = {}
bar_registry: Dict[str, Bar] = {}
use_foo = True
# annotate registry
registry: Union[Dict[str, Foo], Dict[str, Bar]] = foo_registry if use_foo else bar_registry
registry.get('asdfsdf') Interesting enough that the |
I believe I stumbled on this while creating a list of heterogeneous dicts: a = {1: {1: 2}}
b = {1: {1: 3}}
c = {2: {1: "x"}}
d = [a, b]
e = [a, c]
reveal_type(a) # builtins.dict[builtins.int*, builtins.dict*[builtins.int*, builtins.int*]]
reveal_type(b) # builtins.dict[builtins.int*, builtins.dict*[builtins.int*, builtins.int*]]
reveal_type(c) # builtins.dict[builtins.int*, builtins.dict*[builtins.int*, builtins.str*]]
reveal_type(d) # builtins.list[builtins.dict*[builtins.int, builtins.dict[builtins.int, builtins.int]]]
reveal_type(e) # builtins.list[builtins.object*] Could you confirm it's the same issue? e.g. is the type of It's also interesting to note that |
I think I've stumbled upon this issue as well with the following code: res = await maybe_awaitable(callback(event)) Where def maybe_awaitable(value: Union[Awaitable[R], R]) -> Awaitable[R]: ( mypy incorrectly identifies the type of r1 = callback(event)
reveal_type(r1)
a = maybe_awaitable(r1)
reveal_type(a)
res = await a
reveal_type(res) mypy outputs the following:
So it looks like this bug is causing mypy to assume the wrong return type for |
Fixed by #17427 |
Bug
or a mock-up repro if the source is private.
On the last line, mypy infers that
registry
has the typeobject
mypy infers that the type of
registry
to beUnion[Dict[str, Foo], Dict[str, Bar]]
but it looks like this behavior may have been undone by #5095 ?mypy 0.740 and Python 3.7.2
Do you see the same issue after installing mypy from Git master?
I ran into other issues doing this, but I am on the latest release.
I'm using
--strict --warn-unreachable
though this occurs with no flags at all.Thanks for reading.
The text was updated successfully, but these errors were encountered: