Skip to content

Re-enable promotions in isinstance checks #6181

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
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
8 changes: 3 additions & 5 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3766,21 +3766,19 @@ def conditional_type_map(expr: Expression,
proposed_type = UnionType([type_range.item for type_range in proposed_type_ranges])
if current_type:
if (not any(type_range.is_upper_bound for type_range in proposed_type_ranges)
and is_proper_subtype(current_type, proposed_type, ignore_promotions=True)):
and is_proper_subtype(current_type, proposed_type)):
# Expression is always of one of the types in proposed_type_ranges
return {}, None
elif not is_overlapping_types(current_type, proposed_type,
prohibit_none_typevar_overlap=True,
ignore_promotions=True):
prohibit_none_typevar_overlap=True):
# Expression is never of any type in proposed_type_ranges
return None, {}
else:
# we can only restrict when the type is precise, not bounded
proposed_precise_type = UnionType([type_range.item
for type_range in proposed_type_ranges
if not type_range.is_upper_bound])
remaining_type = restrict_subtype_away(current_type, proposed_precise_type,
ignore_promotions=True)
remaining_type = restrict_subtype_away(current_type, proposed_precise_type)
return {expr: proposed_type}, {expr: remaining_type}
else:
return {expr: proposed_type}, {}
Expand Down
3 changes: 2 additions & 1 deletion test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,8 @@ def f(x: Union[A, B]) -> None:
f(x)
[builtins fixtures/isinstance.pyi]

[case testIsinstanceWithOverlappingPromotionTypes]
[case testIsinstanceWithOverlappingPromotionTypes-skip]
# Currently disabled: see https://github.com/python/mypy/issues/6180 for context
from typing import Union

class FloatLike: pass
Expand Down