Skip to content

Commit f4aca38

Browse files
committed
Fix parsing conditional expression in set comprehension
Fixes #584.
1 parent 026061b commit f4aca38

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

mypy/parse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,7 @@ def parse_dict_or_set_expr(self) -> Node:
12071207
items = List[Tuple[Node, Node]]()
12081208
self.expect('{')
12091209
while self.current_str() != '}' and not self.eol():
1210-
key = self.parse_expression(precedence['<if>'])
1210+
key = self.parse_expression(precedence['<for>'])
12111211
if self.current_str() in [',', '}'] and items == []:
12121212
return self.parse_set_expr(key)
12131213
elif self.current_str() == 'for' and items == []:

mypy/test/data/parse.test

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3086,3 +3086,34 @@ MypyFile:1(
30863086
x
30873087
IntExpr(1))
30883088
VarArg)))
3089+
3090+
[case testConditionalExpressionInSetComprehension]
3091+
{ 1 if x else 2 for x in y }
3092+
[out]
3093+
MypyFile:1(
3094+
ExpressionStmt:1(
3095+
SetComprehension:1(
3096+
GeneratorExpr:1(
3097+
ConditionalExpr:1(
3098+
Condition(
3099+
NameExpr(x))
3100+
IntExpr(1)
3101+
IntExpr(2))
3102+
NameExpr(x)
3103+
NameExpr(y)))))
3104+
3105+
[case testConditionalExpressionInListComprehension]
3106+
a = [ 1 if x else 2 for x in y ]
3107+
[out]
3108+
MypyFile:1(
3109+
AssignmentStmt:1(
3110+
NameExpr(a)
3111+
ListComprehension:1(
3112+
GeneratorExpr:1(
3113+
ConditionalExpr:1(
3114+
Condition(
3115+
NameExpr(x))
3116+
IntExpr(1)
3117+
IntExpr(2))
3118+
NameExpr(x)
3119+
NameExpr(y)))))

0 commit comments

Comments
 (0)