Skip to content
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

Fix single item enum match type exhaustion #16966

Conversation

superosku
Copy link
Contributor

@superosku superosku commented Feb 29, 2024

Fixes #14109

This comment has been minimized.

@superosku superosku force-pushed the hotfix/fix-single-item-enums-match-type-exhaustion branch 3 times, most recently from 5cc30c9 to 60f6c4a Compare February 29, 2024 16:41
@@ -1369,6 +1369,35 @@ match m3:
reveal_type(m3) # N: Revealed type is "Tuple[Union[builtins.int, builtins.str]]"
[builtins fixtures/tuple.pyi]

[case testMatchEnumMultipleChoices]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This case is not strictly needed for this change. It just demonstrates how this should work normally. But I would think this is already covered. What do you think?

@superosku superosku marked this pull request as ready for review February 29, 2024 16:43

This comment has been minimized.

@superosku superosku force-pushed the hotfix/fix-single-item-enums-match-type-exhaustion branch from 60f6c4a to 14699cb Compare February 29, 2024 17:53

This comment has been minimized.

@superosku superosku force-pushed the hotfix/fix-single-item-enums-match-type-exhaustion branch from 14699cb to 885a84e Compare March 1, 2024 08:58
@@ -202,7 +202,7 @@ def visit_value_pattern(self, o: ValuePattern) -> PatternType:
typ = self.chk.expr_checker.accept(o.expr)
typ = coerce_to_literal(typ)
narrowed_type, rest_type = self.chk.conditional_types_with_intersection(
current_type, [get_type_range(typ)], o, default=current_type
current_type, [get_type_range(typ)], o, default=get_proper_type(typ)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue was that on mypy/checker.py on conditional_types function, which is called by this self.chk.conditional_types_with_intersection , the conditional_types returns from here the default value:

return default, UninhabitedType()

When on regular enum that has multiple choices the conditional_types returns here instead:

return proposed_type, remaining_type

But the first one returns wrong default value, so I am changing the default value here to be what the get_proper_type returns instead.

This comment has been minimized.

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Thanks, this looks good to me (just made a minor tweak to the tests)

Copy link
Contributor

github-actions bot commented Mar 7, 2024

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@hauntsaninja hauntsaninja merged commit 0b8fed5 into python:master Mar 7, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exhaustiveness Checks Fail on Singleton Enums
2 participants