Skip to content

Commit e8cb282

Browse files
author
Adrien Chauve
committed
Split f-string tests and fix ast visit of JoinedStr
1 parent 025c948 commit e8cb282

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

mypy/fastparse.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -774,11 +774,14 @@ def visit_Str(self, n: ast35.Str) -> Union[UnicodeExpr, StrExpr]:
774774
# JoinedStr(expr* values)
775775
@with_line
776776
def visit_JoinedStr(self, n: ast35.JoinedStr) -> StrExpr:
777-
return StrExpr(n.values[0])
777+
result_string_expression = StrExpr('')
778+
for value in n.values:
779+
result_string_expression = OpExpr('+', result_string_expression, self.visit(value))
780+
return result_string_expression
778781

779782
# FormattedValue(expr value)
780783
@with_line
781-
def visit_FormattedValue(self, n: ast35.FormattedValue) -> StrExpr:
784+
def visit_FormattedValue(self, n: ast35.FormattedValue) -> Expression:
782785
return self.visit(n.value)
783786

784787
# Bytes(bytes s)

test-data/unit/check-expressions.test

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,21 +1162,23 @@ u'%s' % (u'abc',)
11621162
-- --------
11631163

11641164

1165-
[case testFStringParseOk]
1166-
# flags: --fast-parser
1165+
[case testFStringBasics]
1166+
# flags: --fast-parser --python-version 3.6
11671167
f'foobar'
1168+
f'{"foobar"}'
1169+
f'foo{"bar"}'
1170+
a: str
1171+
a = f'foobar'
1172+
a = f'{"foobar"}'
11681173

1169-
[case testFStringTypecheckExpression]
1174+
[case testFStringExpressions]
11701175
# flags: --fast-parser --python-version 3.6
1171-
a: str
1172-
a = 'foo'
1173-
b: str
1174-
b = f'{a}bar'
1175-
b == 'foobar'
1176-
f'{1 + "a"}'
1177-
f'{1 + 1}'
1176+
f'{1 + ""}'
1177+
f' {1 + ""}'
11781178
[out]
1179-
main:7: error: Unsupported operand types for + ("int" and "str") (diff)
1179+
main:2: error: Unsupported operand types for + ("int" and "str")
1180+
main:3: error: Unsupported operand types for + ("int" and "str")
1181+
main:3: error: Unsupported operand types for + ("str" and "int")
11801182

11811183

11821184
-- Lambdas

0 commit comments

Comments
 (0)