8000 Fix nondeterministic type checking caused by nonassociative of None j… · python/mypy@29e125f · GitHub
[go: up one dir, main page]

Skip to content

Commit 29e125f

Browse files
authored
Fix nondeterministic type checking caused by nonassociative of None joins (#19158)
Fixes #19121 (xarray case) See #19147 for context The ordering of the union is still nondeterministic. We could solve this by change the solver to use `dict[Type, None` instead of `set[Type]` since dicts are ordered. But doing so could paper over further bad solving from nonassociativity or noncommutativity
1 parent 3801b7f commit 29e125f

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

mypy/solve.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ def solve_iteratively(
250250
def _join_sorted_key(t: Type) -> int:
251251
t = get_proper_type(t)
252252
if isinstance(t, UnionType):
253+
return -2
254+
if isinstance(t, NoneType):
253255
return -1
254256
return 0
255257

test-data/unit/check-generics.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3599,4 +3599,17 @@ def draw(
35993599
reveal_type(c1) # N: Revealed type is "Union[builtins.int, builtins.str]"
36003600
reveal_type(c2) # N: Revealed type is "Union[builtins.int, builtins.str]"
36013601
reveal_type(c3) # N: Revealed type is "Union[builtins.int, builtins.str]"
3602+
3603+
def takes_int_str_none(x: int | str | None) -> None: ...
3604+
3605+
def draw_none(
3606+
colors1: A[str] | B[str] | C[int] | D[None],
3607+
colors2: A[str] | B[str] | C[int] | D[None],
3608+
colors3: A[str] | B[str] | C[int] | D[None],
3609+
) -> None:
3610+
for c1, c2, c3 in zip2(colors1, colors2, colors3):
3611+
# TODO: can't do reveal type because the union order is not deterministic
3612+
takes_int_str_none(c1)
3613+
takes_int_str_none(c2)
3614+
takes_int_str_none(c3)
36023615
[builtins fixtures/tuple.pyi]

test-data/unit/check-varargs.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ T = TypeVar('T')
631631
def f(*args: T) -> T: ...
632632
reveal_type(f(*(1, None))) # N: Revealed type is "Union[Literal[1]?, None]"
633633
reveal_type(f(1, *(None, 1))) # N: Revealed type is "Union[Literal[1]?, None]"
634-
reveal_type(f(1, *(1, None))) # N: Revealed type is "Union[builtins.int, None]"
634+
reveal_type(f(1, *(1, None))) # N: Revealed type is "Union[Literal[1]?, None]"
635635
[builtins fixtures/tuple.pyi]
636636

637637

0 commit comments

Comments
 (0)
0