8000 [mypyc] Initial optimization for f-string through a str.join() specializer by 97littleleaf11 · Pull Request #10776 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

[mypyc] Initial optimization for f-string through a str.join() specializer #10776

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

Merged
merged 8 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add some tests
  • Loading branch information
97littleleaf11 committed Jul 8, 2021
commit a9908ede25a718c12170a769a8449100dd644dbb
5 changes: 3 additions & 2 deletions mypyc/irbuild/specialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,8 @@ def translate_fstring(
# Special case for f-string, which is translated into str.join() in mypy AST.
# This specializer optimizes simplest f-strings which don't contain any
# format operation.
if expr.arg_kinds == [ARG_POS] and isinstance(expr.args[0], ListExpr):
if (isinstance(callee.expr, StrExpr) and callee.expr.value == ''
and expr.arg_kinds == [ARG_POS] and isinstance(expr.args[0], ListExpr)):
for item in expr.args[0].items:
if isinstance(item, StrExpr):
continue
Expand All @@ -478,7 +479,7 @@ def translate_fstring(

result_list: List[Value] = [Integer(0, c_pyssize_t_rprimitive)]
for item in expr.args[0].items:
if isinstance(item, StrExpr):
if isinstance(item, StrExpr) and item.value != '':
result_list.append(builder.accept(item))
elif isinstance(item, CallExpr):
result_list.append(builder.call_c(str_op,
Expand Down
15 changes: 15 additions & 0 deletions mypyc/test-data/run-strings.test
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ def test_str_to_bool() -> None:
assert is_true(x)
assert not is_false(x)

def test_str_join() -> None:
var = 'mypyc'
num = 10
assert ''.join(['a', 'b', '{}'.format(var), 'c']) == 'abmypycc'
assert ''.join(['a', 'b', '{:{}}'.format(var, ''), 'c']) == 'abmypycc'
assert ''.join(['a', 'b', '{:{}}'.format(var, '>10'), 'c']) == 'ab mypycc'
assert ''.join(['a', 'b', '{:{}}'.format(var, '>{}'.format(num)), 'c']) == 'ab mypycc'

[case testFStrings]
import decimal
from datetime import datetime
Expand Down Expand Up @@ -356,6 +364,13 @@ def test_format_method_different_kind() -> None:
assert "Test: {}{}".format(s3, s1) == "Test: 测试:Literal['😀']"
assert "Test: {}{}".format(s3, s2) == "Test: 测试:Revealed type is"

def test_format_method_nested() -> None:
var = 'mypyc'
num = 10
assert '{:{}}'.format(var, '') == 'mypyc'
assert '{:{}}'.format(var, '>10') == ' mypyc'
assert '{:{}}'.format(var, '>{}'.format(num)) == ' mypyc'

class Point:
def __init__(self, x, y):
self.x, self.y = x, y
Expand Down
0