-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Handle python 3.6 f-strings without error #2622
Changes from 7 commits
fc03182
31e0252
6c4aa78
2112398
025c948
e8cb282
d6ba5fd
25043d4
8f17a4c
4847e27
5534c1a
5567215
929c5ef
2c73695
47d592a
0867026
8a17249
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -769,6 +769,22 @@ def visit_Str(self, n: ast35.Str) -> Union[UnicodeExpr, StrExpr]: | |
else: | ||
return UnicodeExpr(n.s) | ||
|
||
# Only available with typed_ast >= 0.6.2 | ||
if hasattr(ast35, 'JoinedStr'): | ||
# JoinedStr(expr* values) | ||
@with_line | ||
def visit_JoinedStr(self, n: ast35.JoinedStr) -> StrExpr: | ||
result_string_expression = StrExpr('') | ||
for value in n.values: | ||
value_as_string_expr = cast(StrExpr, self.visit(value)) | ||
result_string_expression = cast(StrExpr, OpExpr('+', result_string_expression, value_as_string_expr)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if you could use asserts instead of casts, e.g. value_as_string_expr = self.visit(value)
assert isinstance(value_as_string_expr, StrExpr)
r = OpExpr('+', result_string_expression, value_as_string_expr)
assert isinstance(r, StrExpr)
result_string_expression = r That way if the type is ever not what you expect it'll fail right there rather than mysteriously failing at some later point. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used cast because otherwise I thought it would systematically fail without a call to I thought another typesafe solution would be to replace casts by something like (untested) |
||
return result_string_expression | ||
|
||
# FormattedValue(expr value) | ||
@with_line | ||
def visit_FormattedValue(self, n: ast35.FormattedValue) -> Expression: | ||
return self.visit(n.value) | ||
|
||
# Bytes(bytes s) | ||
@with_line | ||
def visit_Bytes(self, n: ast35.Bytes) -> Union[BytesExpr, StrExpr]: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1157,6 +1157,30 @@ b'%c' % (123) | |
[case testUnicodeInterpolation_python2] | ||
u'%s' % (u'abc',) | ||
|
||
|
||
-- F-String | ||
-- -------- | ||
|
||
|
||
[case testFStringBasics] | ||
# flags: --fast-parser --python-version 3.6 | ||
f'foobar' | ||
f'{"foobar"}' | ||
f'foo{"bar"}' | ||
a: str | ||
a = f'foobar' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need Can you also add tests showing that the expressions in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PS. To quickly run just this test, try: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the tip, better than I've just added another test to check type checking of expressions but I can't get it working. However running the test with
It doesn't use python 3.6 syntax. How can I specify it in the tests? |
||
a = f'{"foobar"}' | ||
|
||
[case testFStringExpressions] | ||
# flags: --fast-parser --python-version 3.6 | ||
f'{1 + ""}' | ||
f' {1 + ""}' | ||
[out] | ||
main:2: error: Unsupported operand types for + ("int" and "str") | ||
main:3: error: Unsupported operand types for + ("int" and "str") | ||
main:3: error: Unsupported operand types for + ("str" and "int") | ||
|
||
|
||
-- Lambdas | ||
-- ------- | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're also going to have to add
JoinedStr
andFormattedValue
to the stubs fortyped_ast.ast35
in typeshed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes thanks for the tip
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done python/typeshed#803