Skip to content

Commit 37d2ee4

Browse files
committed
Fix regression involving dict or on recursive dict
Fixes #21141
1 parent ef7e8a6 commit 37d2ee4

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

mypy/constraints.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,9 +506,11 @@ def handle_recursive_union(template: UnionType, actual: Type, direction: int) ->
506506
# the union in two parts, and try inferring sequentially.
507507
non_type_var_items = [t for t in template.items if not isinstance(t, TypeVarType)]
508508
type_var_items = [t for t in template.items if isinstance(t, TypeVarType)]
509-
return infer_constraints(
510-
UnionType.make_union(non_type_var_items), actual, direction
511-
) or infer_constraints(UnionType.make_union(type_var_items), actual, direction)
509+
ret = infer_constraints(UnionType.make_union(non_type_var_items), actual, direction)
510+
if ret or any(mypy.subtypes.is_subtype(t, actual) for t in non_type_var_items):
511+
return ret
512+
ret = infer_constraints(UnionType.make_union(type_var_items), actual, direction)
513+
return ret
512514

513515

514516
def any_constraints(options: list[list[Constraint] | None], *, eager: bool) -> list[Constraint]:

test-data/unit/check-recursive-types.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,3 +1040,12 @@ def f(obj: OneClass) -> None:
10401040
else:
10411041
reveal_type(obj) # N: Revealed type is "builtins.list[...]"
10421042
[builtins fixtures/isinstancelist.pyi]
1043+
1044+
[case testRecursiveDictOr]
1045+
from typing_extensions import TypeAlias
1046+
A: TypeAlias = "dict[str, A | int]"
1047+
1048+
def f(d1: A, d2: A) -> None:
1049+
d3: A = {**d1, **d2}
1050+
d4: A = d1 | d2
1051+
[builtins fixtures/dict-full.pyi]

0 commit comments

Comments
 (0)