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

Fixes how ** affects inference in {} expressions, refs #11691 #11693

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 10 additions & 8 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3334,13 +3334,14 @@ def check_lst_expr(self, items: List[Expression], fullname: str,
self.chk.named_generic_type(fullname, [tv]),
self.named_type('builtins.function'),
name=tag,
variables=[tv])
out = self.check_call(constructor,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a readability change only. Before and after:
Снимок экрана 2021-12-09 в 18 41 40
Снимок экрана 2021-12-09 в 18 41 52

[(i.expr if isinstance(i, StarExpr) else i)
for i in items],
[(nodes.ARG_STAR if isinstance(i, StarExpr) else nodes.ARG_POS)
for i in items],
context)[0]
variables=[tv],
)
out = self.check_call(
constructor,
[(i.expr if isinstance(i, StarExpr) else i) for i in items],
[(nodes.ARG_STAR if isinstance(i, StarExpr) else nodes.ARG_POS) for i in items],
context,
)[0]
return remove_instance_last_known_values(out)

def visit_tuple_expr(self, e: TupleExpr) -> Type:
Expand Down Expand Up @@ -3518,7 +3519,8 @@ def visit_dict_expr(self, e: DictExpr) -> Type:
variables=[kt, vt])
rv = self.check_call(constructor, [arg], [nodes.ARG_POS], arg)[0]
else:
self.check_method_call_by_name('update', rv, [arg], [nodes.ARG_POS], arg)
sobolevn marked this conversation as resolved.
Show resolved Hide resolved
arg_type = arg.accept(self)
rv = join.dict_unpack(rv, arg_type, self.named_type('typing.Mapping'))
assert rv is not None
return rv

Expand Down
42 changes: 42 additions & 0 deletions mypy/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,45 @@ def unpack_callback_protocol(t: Instance) -> Optional[Type]:
if t.type.protocol_members == ['__call__']:
return find_member('__call__', t, t, is_operator=True)
return None


def dict_unpack(dict_obj: Type, other: Type, mapping_type: Instance) -> ProperType:
"""Joins two dict-like objects.

For example, `dict[str, int]` and `dict[int, int]`
will become `dict[object, int]`.
"""
dict_obj = get_proper_type(dict_obj)
if (not isinstance(dict_obj, Instance)
or dict_obj.type.fullname != 'builtins.dict'
or len(dict_obj.args) != 2):
# Since this function is only used when two dicts are joined like:
# `{**other, 'a': 1}`, we require `dict_obj` to be an `Instance`
# of `builtins.dict``.
return AnyType(TypeOfAny.from_error)

key_type, value_type = dict_obj.args
new_key_type, new_value_type = extract_key_value_types(other, mapping_type)
return dict_obj.copy_modified(args=[
join_types(key_type, new_key_type),
join_types(value_type, new_value_type),
])


def extract_key_value_types(typ: Type, mapping_type: Instance) -> Tuple[Type, Type]:
typ = get_proper_type(typ)
if isinstance(typ, Instance) and len(typ.args) >= 2:
mapping = map_instance_to_supertype(typ, mapping_type.type)
return mapping.args[0], mapping.args[1]
elif isinstance(typ, UnionType):
keys = []
values = []
for item in typ.relevant_items:
key, value = extract_key_value_types(item, mapping_type)
keys.append(key)
values.append(value)
return join_type_list(keys), join_type_list(values)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we also need to handle: NoReturn and TypedDict types here. Any others?

return (
AnyType(TypeOfAny.implementation_artifact),
AnyType(TypeOfAny.implementation_artifact),
)