Skip to content

Fix reachability inference with 'isinstance(Any, Any)' #7048

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
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
13 changes: 8 additions & 5 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3895,12 +3895,15 @@ def conditional_type_map(expr: Expression,
second element is a map from the expression to the type it would hold
if it was not the proposed type, if any. None means bot, {} means top"""
if proposed_type_ranges:
if len(proposed_type_ranges) == 1:
proposed_type = proposed_type_ranges[0].item # Union with a single type breaks tests
else:
proposed_type = UnionType([type_range.item for type_range in proposed_type_ranges])
proposed_items = [type_range.item for type_range in proposed_type_ranges]
proposed_type = UnionType.make_simplified_union(proposed_items)
if current_type:
if (not any(type_range.is_upper_bound for type_range in proposed_type_ranges)
if isinstance(proposed_type, AnyType):
# We don't really know much about the proposed type, so we shouldn't
# attempt to narrow anything. Instead, we broaden the expr to Any to
# avoid false positives
return {expr: proposed_type}, {}
elif (not any(type_range.is_upper_bound for type_range in proposed_type_ranges)
and is_proper_subtype(current_type, proposed_type)):
# Expression is always of one of the types in proposed_type_ranges
return {}, None
Expand Down
28 changes: 28 additions & 0 deletions test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -2101,6 +2101,34 @@ def f(x: Union[A, str]) -> None:
x.method_only_in_a()
[builtins fixtures/isinstance.pyi]

[case testIsinstanceIgnoredImportDualAny]
from typing import Any
from foo import Bad, OtherBad # type: ignore
x: Any
if isinstance(x, Bad):
reveal_type(x) # N: Revealed type is 'Any'
else:
reveal_type(x) # N: Revealed type is 'Any'

if isinstance(x, (Bad, OtherBad)):
reveal_type(x) # N: Revealed type is 'Any'
else:
reveal_type(x) # N: Revealed type is 'Any'

y: object
if isinstance(y, Bad):
reveal_type(y) # N: Revealed type is 'Any'
else:
reveal_type(y) # N: Revealed type is 'builtins.object'

class Ok: pass
z: Any
if isinstance(z, Ok):
reveal_type(z) # N: Revealed type is '__main__.Ok'
else:
reveal_type(z) # N: Revealed type is 'Any'
[builtins fixtures/isinstance.pyi]

[case testIsInstanceInitialNoneCheckSkipsImpossibleCasesNoStrictOptional]
# flags: --strict-optional
from typing import Optional, Union
Expand Down