Skip to content

Commit 09498fb

Browse files
committed
Merge all or-ed constraints if they have the same structure
1 parent 657bdd8 commit 09498fb

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

mypy/constraints.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,13 +541,14 @@ def any_constraints(options: list[list[Constraint] | None], *, eager: bool) -> l
541541
# TODO: More generally, if a given (variable, direction) pair appears in
542542
# every option, combine the bounds with meet/join always, not just for Any.
543543
trivial_options = select_trivial(valid_options)
544-
if trivial_options and len(trivial_options) < len(valid_options):
544+
if 0 < len(trivial_options) < len(valid_options):
545545
merged_options = []
546546
for option in valid_options:
547547
if option in trivial_options:
548548
continue
549549
merged_options.append([merge_with_any(c) for c in option])
550550
return any_constraints(list(merged_options), eager=eager)
551+
return sum(valid_options, [])
551552

552553
# If normal logic didn't work, try excluding trivially unsatisfiable constraint (due to
553554
# upper bounds) from each option, and comparing them again.

test-data/unit/check-inference-context.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,3 +1530,15 @@ def check3(use: bool, val: str) -> "str | Literal[False]":
15301530
def check4(use: bool, val: str) -> "str | bool":
15311531
return use and identity(val)
15321532
[builtins fixtures/tuple.pyi]
1533+
1534+
[case testDictOrLiteralInContext]
1535+
from typing import Union, Optional, Any
1536+
1537+
P = dict[str, Union[Optional[str], dict[str, Optional[str]]]]
1538+
1539+
def f(x: P) -> None:
1540+
pass
1541+
1542+
def g(x: Union[dict[str, Any], None], s: Union[str, None]) -> None:
1543+
f(x or {'x': s})
1544+
[builtins fixtures/dict.pyi]

0 commit comments

Comments
 (0)