Skip to content

Improve type inference by supporting partial OrderedDict types #8039

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

Merged
merged 1 commit into from
Nov 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2809,7 +2809,8 @@ def infer_partial_type(self, name: Var, lvalue: Lvalue, init_type: Type) -> bool
if (isinstance(lvalue, (NameExpr, MemberExpr)) and
(fullname == 'builtins.list' or
fullname == 'builtins.set' or
fullname == 'builtins.dict') and
fullname == 'builtins.dict' or
fullname == 'collections.OrderedDict') and
all(isinstance(t, (NoneType, UninhabitedType))
for t in get_proper_types(init_type.args))):
partial_type = PartialType(init_type.type, name, init_type.args)
Expand Down Expand Up @@ -3002,7 +3003,7 @@ def try_infer_partial_type_from_indexed_assignment(
if partial_types is None:
return
typename = type_type.fullname
if typename == 'builtins.dict':
if typename == 'builtins.dict' or typename == 'collections.OrderedDict':
# TODO: Don't infer things twice.
key_type = self.expr_checker.accept(lvalue.index)
value_type = self.expr_checker.accept(rvalue)
Expand All @@ -3013,7 +3014,7 @@ def try_infer_partial_type_from_indexed_assignment(
if (is_valid_inferred_type(full_key_type) and
is_valid_inferred_type(full_value_type)):
if not self.current_node_deferred:
var.type = self.named_generic_type('builtins.dict',
var.type = self.named_generic_type(typename,
[full_key_type, full_value_type])
del partial_types[var]

Expand Down
1 change: 1 addition & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ def check_typeddict_call_with_kwargs(self, callee: TypedDictType,
} # type: ClassVar[Dict[str, List[str]]]
container_args = {'builtins.list': {'extend': ['builtins.list']},
'builtins.dict': {'update': ['builtins.dict']},
'collections.OrderedDict': {'update': ['builtins.dict']},
'builtins.set': {'update': ['builtins.set', 'builtins.list']},
} # type: ClassVar[Dict[str, Dict[str, List[str]]]]

Expand Down
13 changes: 13 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,19 @@ if bool():
reveal_type(dd) # N: Revealed type is 'builtins.dict[Any, Any]'
[builtins fixtures/dict.pyi]

[case testInferOrderedDictInitializedToEmpty]
from collections import OrderedDict

o = OrderedDict()
o[1] = 'x'
reveal_type(o) # N: Revealed type is 'collections.OrderedDict[builtins.int, builtins.str]'

d = {1: 'x'}
oo = OrderedDict()
oo.update(d)
reveal_type(oo) # N: Revealed type is 'collections.OrderedDict[builtins.int*, builtins.str*]'
[builtins fixtures/dict.pyi]


-- Inferring types of variables first initialized to None (partial types)
-- ----------------------------------------------------------------------
Expand Down
8 changes: 7 additions & 1 deletion test-data/unit/lib-stub/collections.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Iterable, Union, Optional
from typing import Any, Iterable, Union, Optional, Dict, TypeVar

def namedtuple(
typename: str,
Expand All @@ -9,3 +9,9 @@ def namedtuple(
module: Optional[str] = ...,
defaults: Optional[Iterable[Any]] = ...
) -> Any: ...

K = TypeVar('K')
V = TypeVar('V')

class OrderedDict(Dict[K, V]):
def __setitem__(self, k: K, v: V) -> None: ...