KEMBAR78
Fix single item enum match type exhaustion by superosku · Pull Request #16966 · python/mypy · GitHub
Skip to content

Conversation

superosku
Copy link
Contributor

@superosku superosku commented Feb 29, 2024

Fixes #14109

@github-actions

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
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
@github-actions

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
@github-actions

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
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.

@github-actions

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)

@github-actions
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. ✅

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