Skip to content

Commit 1a88fcb

Browse files
committed
Ignore str/bytes and pow
1 parent 4abe91c commit 1a88fcb

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/python_minifier/transforms/constant_folding.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,22 @@ def visit_BinOp(self, node):
2020
node.right = self.visit(node.right)
2121

2222
# Check this is a constant expression that could be folded
23-
if not is_ast_node(node.left, (ast.Num, ast.Str, 'Bytes', 'NameConstant')):
23+
# We don't try to fold strings or bytes, since they have probably been arranged this way to make the source shorter and we are unlikely to beat that
24+
if not is_ast_node(node.left, (ast.Num, 'NameConstant')):
2425
return node
25-
if not is_ast_node(node.right, (ast.Num, ast.Str, 'Bytes', 'NameConstant')):
26+
if not is_ast_node(node.right, (ast.Num, 'NameConstant')):
2627
return node
2728

2829
if isinstance(node.op, ast.Div):
2930
# Folding div is subtle, since it can have different results in Python 2 and Python 3
3031
# Do this once target version options have been implemented
3132
return node
3233

34+
if isinstance(node.op, ast.Pow):
35+
# This can be folded, but it is unlikely to reduce the size of the source
36+
# It can also be slow to evaluate
37+
return node
38+
3339
expression_printer = ExpressionPrinter()
3440

3541
try:
@@ -41,10 +47,6 @@ def visit_BinOp(self, node):
4147
if isinstance(value, float) and math.isnan(value):
4248
# There is no nan literal.
4349
new_node = ast.Call(func=ast.Name(id='float', ctx=ast.Load()), args=[ast.Str(s='nan')], keywords=[])
44-
elif isinstance(value, str):
45-
new_node = ast.Str(s=value)
46-
elif isinstance(value, bytes):
47-
new_node = ast.Bytes(s=value)
4850
elif isinstance(value, bool):
4951
new_node = ast.NameConstant(value=value)
5052
elif isinstance(value, (int, float, complex)):

0 commit comments

Comments
 (0)