8000 join constraints with Any when constraints are in same shape by oraluben · Pull Request #6487 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

join constraints with Any when constraints are in same shape #6487

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

Closed
wants to merge 3 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
Next Next commit
join constraints with Any when constraints are in same shape
  • Loading branch information
oraluben committed Feb 27, 2019
commit 360a137209fe8324371bf8c4129aa2ba0ccf0e11
29 changes: 24 additions & 5 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,32 @@ def any_constraints(options: List[Optional[List[Constraint]]], eager: bool) -> L
valid_options = [option for option in options if option is not None]
if len(valid_options) == 1:
return valid_options[0]
elif (len(valid_options) > 1 and
all(is_same_constraints(valid_options[0], c)
for c in valid_options[1:])):
# Multiple sets of constraints that are all the same. Just pick any one of them.
elif len(valid_options) > 1:
if all(is_same_constraints(valid_options[0], c)
for c in valid_options[1:]):
# Multiple sets of constraints that are all the same. Just pick any one of them.
return valid_options[0]
# TODO: More generally, if a given (variable, direction) pair appears in
# every option, combine the bounds with meet/join.
return valid_options[0]
elif all(len(option) == len(valid_options[0]) for option in valid_options[1:]):
# same length, more shape check below
to_join_constraint_group = [list(c_t) for c_t in zip(*valid_options)] # type: List[List[Constraint]]
joined_constraints = [] # type: List[Constraint]
for to_join_constraint_list in to_join_constraint_group:
if any(
(c.op != to_join_constraint_list[0].op or
c.type_var != to_join_constraint_list[0].type_var)
for c in to_join_constraint_list[1:]
):
break
joined_constraints.append(to_join_constraint_list[0])
if not all(is_same_constraint(to_join_constraint_list[0], c)
for c in to_join_constraint_list[1:]):
# Multiple sets of constraints that are all the same. Just pick any one of them.
joined_constraints[-1].target = AnyType(TypeOfAny.explicit)
else:
# not break from loop
return joined_constraints

# Otherwise, there are either no valid options or multiple, inconsistent valid
# options. Give up and deduce nothing.
Expand Down
20 changes: 20 additions & 0 deletions test-data/unit/check-unions.test
Original file line number Diff line number Diff line change
Expand Up @@ -990,3 +990,23 @@ def union_test3():
return x + 1

[builtins fixtures/isinstancelist.pyi]
< 6216 span class='blob-code-inner blob-code-marker ' data-code-marker="+">
[case testUninhabitedUnionDict]
from typing import Any, Union, List

def tl(t: Union[List[int], List[str]]):
reveal_type(t) # E: Revealed type is 'Union[builtins.list[builtins.int], builtins.list[builtins.str]]'

tl([])

[builtins fixtures/list.pyi]

[case testUninhabitedUnionList]
from typing import Any, Dict, Union

def td(t: Union[Dict[int, Any], Dict[str, Any]]):
reveal_type(t) # E: Revealed type is 'Union[builtins.dict[builtins.int, Any], builtins.dict[builtins.str, Any]]'

td({})

[builtins fixtures/dict.pyi]
0