-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4be232d
Fixes how `**` affects inference in `{}` expressions, refs #11691
sobolevn 540f00b
Fixes typechecking
sobolevn edf20f5
Adds tests
sobolevn a233afc
Removes prints
sobolevn 164a28c
Fixes tests
sobolevn 167df5f
Update checkexpr.py
sobolevn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that we also need to handle: |
||
return ( | ||
AnyType(TypeOfAny.implementation_artifact), | ||
AnyType(TypeOfAny.implementation_artifact), | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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: