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

Attempt to fix bool re-narrowing #11845

Closed
wants to merge 3 commits 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/binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,17 @@ def _get(self, key: Key, index: int = -1) -> Optional[Type]:
return self.frames[i].types[key]
return None

def put(self, expr: Expression, typ: Type) -> None:
def put(self, expr: Expression, typ: Type, *, redeclare: bool = False) -> None:
if not isinstance(expr, (IndexExpr, MemberExpr, AssignmentExpr, NameExpr)):
return
if not literal(expr):
return
key = literal_hash(expr)
assert key is not None, 'Internal error: binder tried to put non-literal'
if key not in self.declarations:
proper = get_proper_type(typ)
if redeclare and proper:
self.declarations[key] = proper
elif key not in self.declarations:
self.declarations[key] = get_declaration(expr)
self._add_dependencies(key)
self._put(key, typ)
Expand Down
8 changes: 5 additions & 3 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3479,7 +3479,9 @@ def visit_assert_stmt(self, s: AssertStmt) -> None:
true_map, else_map = self.find_isinstance_check(s.expr)
if s.msg is not None:
self.expr_checker.analyze_cond_branch(else_map, s.msg, None)
self.push_type_map(true_map)
# We want to redeclare the type in type_map, because it was refined
# for the rest of the frame.
self.push_type_map(true_map, redeclare=True)

def visit_raise_stmt(self, s: RaiseStmt) -> None:
"""Type check a raise statement."""
Expand Down Expand Up @@ -5244,12 +5246,12 @@ def iterable_item_type(self, instance: Instance) -> Type:
def function_type(self, func: FuncBase) -> FunctionLike:
return function_type(func, self.named_type('builtins.function'))

def push_type_map(self, type_map: 'TypeMap') -> None:
def push_type_map(self, type_map: 'TypeMap', *, redeclare: bool = False) -> None:
if type_map is None:
self.binder.unreachable()
else:
for expr, type in type_map.items():
self.binder.put(expr, type)
self.binder.put(expr, type, redeclare=redeclare)

def infer_issubclass_maps(self, node: CallExpr,
expr: Expression,
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-narrowing.test
Original file line number Diff line number Diff line change
Expand Up @@ -1245,3 +1245,17 @@ def two_type_vars(x: Union[str, Dict[str, int], Dict[bool, object], int]) -> Non
else:
reveal_type(x) # N: Revealed type is "builtins.int"
[builtins fixtures/dict.pyi]

[case testBoolNarrowingAfterLiterallFallback]
# https://github.com/python/mypy/issues/11839
from typing import Any

var: Any
reveal_type(var) # N: Revealed type is "Any"
assert isinstance(var, bool)
reveal_type(var) # N: Revealed type is "builtins.bool"

if var:
reveal_type(var) # N: Revealed type is "Literal[True]"
reveal_type(var) # N: Revealed type is "builtins.bool"
[builtins fixtures/isinstance.pyi]