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

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

Closed
wants to merge 3 commits into from
Closed
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
30 changes: 25 additions & 5 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,33 @@ 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:]):
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
22 changes: 21 additions & 1 deletion mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,27 @@ def visit_instance(self, left: Instance) -> bool:
not self.ignore_declared_variance):
# Map left type to corresponding right instances.
t = map_instance_to_supertype(left, right.type)
nominal = all(self.check_type_parameter(lefta, righta, tvar.variance)

uninhabited_as_subtype = any(t.type.has_base(_t) for _t in (
'builtins.dict',
'builtins.list',
'builtins.set',
'builtins.tuple',
))

def map_variance(lefta, righta, variance: int) -> int:
if variance == COVARIANT or variance == CONTRAVARIANT:
return variance
if isinstance(lefta, UninhabitedType):
return COVARIANT
elif isinstance(righta, UninhabitedType):
return CONTRAVARIANT
return variance

nominal = all(self.check_type_parameter(
lefta,
righta,
map_variance(lefta, righta, tvar.variance) if uninhabited_as_subtype else tvar.variance)
for lefta, righta, tvar in
zip(t.args, right.args, right.type.defn.type_vars))
if nominal:
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]

[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]