FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix nondeterministic type checking caused by nonassociative of None joins by hauntsaninja · Pull Request #19158 · python/mypy · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .test  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 2 additions & 0 deletions mypy/solve.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL