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 conditional_type_map to not broaden the type (#10739) #10912

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 5 additions & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@
from mypy.join import join_types
from mypy.treetransform import TransformVisitor
from mypy.binder import ConditionalTypeBinder, get_declaration
from mypy.meet import is_overlapping_erased_types, is_overlapping_types
from mypy.meet import (
is_overlapping_erased_types, is_overlapping_types, narrow_declared_type
)
from mypy.options import Options
from mypy.plugin import Plugin, CheckerPluginInterface
from mypy.sharedparse import BINARY_MAGIC_METHODS
Expand Down Expand Up @@ -5130,7 +5132,8 @@ def conditional_type_map(expr: Expression,
for type_range in proposed_type_ranges
if not type_range.is_upper_bound])
remaining_type = restrict_subtype_away(current_type, proposed_precise_type)
return {expr: proposed_type}, {expr: remaining_type}
narrowed_proposed_type = narrow_declared_type(current_type, proposed_type)
return {expr: narrowed_proposed_type}, {expr: remaining_type}
else:
return {expr: proposed_type}, {}
else:
Expand Down
55 changes: 55 additions & 0 deletions test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -2552,6 +2552,61 @@ if isinstance(foo, Ambiguous):
reveal_type(foo.x) # N: Revealed type is "builtins.int"
[builtins fixtures/isinstance.pyi]

[case testIsInstanceWithNonSubsetOverlap]
from typing import Union
class A: pass
class B: pass
class C: pass

x: Union[A, B, C]
if isinstance(x, (A, B)):
reveal_type(x) # N: Revealed type is "Union[__main__.A, __main__.B]"
if isinstance(x, (B, C)):
# This is the key clause of the test.
# Avoid a regression to "Union[__main__.B, __main__.C]" because
# we know that x is not C.
reveal_type(x) # N: Revealed type is "__main__.B"
else:
reveal_type(x) # N: Revealed type is "__main__.A"
else:
reveal_type(x) # N: Revealed type is "__main__.C"
[builtins fixtures/isinstancelist.pyi]

[case testIsInstanceWithTypeVar]
from typing import Any, TypeVar, Union

T = TypeVar("T", bound=Any)
U = TypeVar("U", bound=Union[float, int])
V = TypeVar("V")

def f(x: T) -> None:
if isinstance(x, int):
x + 1
def g(y: U) -> None:
if isinstance(y, int):
y + 1
def h(z: V) -> None:
if isinstance(z, int):
z + 1
[builtins fixtures/isinstance.pyi]

[case testIsInstanceWithTypeVarAndSubclass]
from typing import Any, TypeVar, Union

class A: pass
class B(A): pass

T = TypeVar("T", bound=A)
U = TypeVar("U", bound=B)

def f(x: T) -> None:
if isinstance(x, B):
reveal_type(x) # N: Revealed type is "__main__.B"
def g(y: U) -> None:
if isinstance(y, A):
reveal_type(y) # N: Revealed type is "U`-1"
[builtins fixtures/isinstance.pyi]

[case testIsSubclassAdHocIntersection]
# flags: --warn-unreachable
from typing import Type
Expand Down