Skip to content

Commit e20d469

Browse files
author
Guido van Rossum
committed
Factor out repetitive code from visit_conditional_expr().
1 parent 3734a95 commit e20d469

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

mypy/checkexpr.py

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Expression type checker. This file is conceptually part of TypeChecker."""
22

3-
from typing import cast, List, Tuple, Callable, Union, Optional
3+
from typing import cast, Dict, List, Tuple, Callable, Union, Optional
44

55
from mypy.types import (
66
Type, AnyType, CallableType, Overloaded, NoneTyp, Void, TypeVarDef,
@@ -1415,45 +1415,37 @@ def visit_conditional_expr(self, e: ConditionalExpr) -> Type:
14151415
self.chk.type_map,
14161416
self.chk.typing_mode_weak())
14171417

1418-
with self.chk.binder:
1419-
if if_map:
1420-
for var, type in if_map.items():
1421-
self.chk.binder.push(var, type)
1422-
if_type = self.accept(e.if_expr, context=ctx)
1418+
if_type = self.analyze_cond_branch(if_map, e.if_expr, context=ctx)
14231419

14241420
if not mypy.checker.is_valid_inferred_type(if_type):
14251421
# Analyze the right branch disregarding the left branch.
1426-
with self.chk.binder:
1427-
if else_map:
1428-
for var, type in else_map.items():
1429-
self.chk.binder.push(var, type)
1430-
else_type = self.accept(e.else_expr, context=ctx)
1422+
else_type = self.analyze_cond_branch(else_map, e.else_expr, context=ctx)
14311423

14321424
# If it would make a difference, re-analyze the left
14331425
# branch using the right branch's type as context.
14341426
if ctx is None or not is_equivalent(else_type, ctx):
14351427
# TODO: If it's possible that the previous analysis of
14361428
# the left branch produced errors that are avoided
14371429
# using this context, suppress those errors.
1438-
with self.chk.binder:
1439-
if if_map:
1440-
for var, type in if_map.items():
1441-
self.chk.binder.push(var, type)
1442-
if_type = self.accept(e.if_expr, context=else_type)
1430+
if_type = self.analyze_cond_branch(if_map, e.if_expr, context=else_type)
14431431

14441432
else:
14451433
# Analyze the right branch in the context of the left
14461434
# branch's type.
1447-
with self.chk.binder:
1448-
if else_map:
1449-
for var, type in else_map.items():
1450-
self.chk.binder.push(var, type)
1451-
else_type = self.accept(e.else_expr, context=if_type)
1435+
else_type = self.analyze_cond_branch(else_map, e.else_expr, context=if_type)
14521436

14531437
res = join.join_types(if_type, else_type)
14541438

14551439
return res
14561440

1441+
def analyze_cond_branch(self, map: Optional[Dict[Node, Type]],
1442+
node: Node, context: Optional[Type]) -> Type:
1443+
with self.chk.binder:
1444+
if map:
1445+
for var, type in map.items():
1446+
self.chk.binder.push(var, type)
1447+
return self.accept(node, context=context)
1448+
14571449
def visit_backquote_expr(self, e: BackquoteExpr) -> Type:
14581450
self.accept(e.expr)
14591451
return self.named_type('builtins.str')

0 commit comments

Comments
 (0)