Skip to content

Fix nondeterministic type checking caused by nonassociative of None joins #19158

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

Merged
merged 2 commits into from
May 28, 2025
Merged
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
2 changes: 2 additions & 0 deletions mypy/solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ def solve_iteratively(
def _join_sorted_key(t: Type) -> int:
t = get_proper_type(t)
if isinstance(t, UnionType):
return -2
if isinstance(t, NoneType):
return -1
return 0

Expand Down
13 changes: 13 additions & 0 deletions test-data/unit/check-generics.test
Original file line number Diff line number Diff line change
Expand Up @@ -3602,4 +3602,17 @@ def draw(
reveal_type(c1) # N: Revealed type is "Union[builtins.int, builtins.str]"
reveal_type(c2) # N: Revealed type is "Union[builtins.int, builtins.str]"
reveal_type(c3) # N: Revealed type is "Union[builtins.int, builtins.str]"

def takes_int_str_none(x: int | str | None) -> None: ...

def draw_none(
colors1: A[str] | B[str] | C[int] | D[None],
colors2: A[str] | B[str] | C[int] | D[None],
colors3: A[str] | B[str] | C[int] | D[None],
) -> None:
for c1, c2, c3 in zip2(colors1, colors2, colors3):
# TODO: can't do reveal type because the union order is not deterministic
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there is a way to avoid non-deterministic union order altogether, e.g. by not putting types into sets?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nvm, you mention this in the issue description.

takes_int_str_none(c1)
takes_int_str_none(c2)
takes_int_str_none(c3)
[builtins fixtures/tuple.pyi]
2 changes: 1 addition & 1 deletion test-data/unit/check-varargs.test
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ T = TypeVar('T')
def f(*args: T) -> T: ...
reveal_type(f(*(1, None))) # N: Revealed type is "Union[Literal[1]?, None]"
reveal_type(f(1, *(None, 1))) # N: Revealed type is "Union[Literal[1]?, None]"
reveal_type(f(1, *(1, None))) # N: Revealed type is "Union[builtins.int, None]"
reveal_type(f(1, *(1, None))) # N: Revealed type is "Union[Literal[1]?, None]"
[builtins fixtures/tuple.pyi]


Expand Down