Skip to content

Commit e30b8fe

Browse files
committed
Re-add ~bool folding
1 parent 1b46b8b commit e30b8fe

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

‎src/python_minifier/transforms/constant_folding.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ def fold(self, node):
4242
# Evaluate the expression
4343
try:
4444
original_expression = unparse_expression(node)
45-
original_value = safe_eval(original_expression)
45+
46+
if isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.Invert) and is_constant_node(node.operand, ast.NameConstant) and isinstance(node.operand.value, bool):
47+
# ~ on a bool is deprecated from python 3.12, to be removed in 3.16.
48+
if sys.version_info >= (3, 16):
49+
return node
50+
original_value = ~int(node.operand.value)
51+
else:
52+
original_value = safe_eval(original_expression)
4653
except Exception:
4754
return node
4855

@@ -155,13 +162,6 @@ def visit_UnaryOp(self, node):
155162
if not isinstance(node.op, (ast.USub, ast.UAdd, ast.Invert, ast.Not)):
156163
return node
157164

158-
# ~ on a bool is deprecated from python 3.12 (removed in 3.16), so evaluating
159-
# it would emit a DeprecationWarning. Folding ~True / ~False saves almost
160-
# nothing, so skip it rather than suppress warnings globally. (~None is a
161-
# TypeError that never folds anyway.)
162-
if isinstance(node.op, ast.Invert) and is_constant_node(node.operand, ast.NameConstant):
163-
return node
164-
165165
return self.fold(node)
166166

167167

‎test/test_folding.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,13 +544,18 @@ def test_double_invert_folded(source, expected):
544544
('-5 + True', '-4'),
545545
('10 * False', '0'),
546546
('True + True', '2'),
547+
('~True', '-2'),
548+
('~False', '-1')
547549
]
548550
)
549551
def test_mixed_numeric_bool_folded(source, expected):
550552
"""
551553
Test folding of expressions mixing numeric and boolean operands.
552554
553555
Python treats True as 1 and False as 0 in numeric contexts.
556+
557+
~ on a bool is deprecated from python 3.12, so it is folded by computing the
558+
value directly rather than evaluating it (which would emit a DeprecationWarning).
554559
"""
555560
if sys.version_info < (3, 4):
556561
pytest.skip('NameConstant not in python < 3.4')
@@ -562,16 +567,18 @@ def test_mixed_numeric_bool_folded(source, expected):
562567
('source', 'expected'), [
563568
('~True', '~True'),
564569
('~False', '~False'),
570+
('~~False', '~~False'),
565571
]
566572
)
567-
def test_invert_bool_not_folded(source, expected):
573+
def test_invert_bool_not_folded_from_316(source, expected, monkeypatch):
568574
"""
569-
~ on a bool is deprecated from python 3.12, so it is not folded (evaluating
570-
it would emit a DeprecationWarning, and the saving is negligible).
575+
~ on a bool is removed in python 3.16, where it raises.
576+
The minified code should fail the same way the original would.
571577
"""
572578
if sys.version_info < (3, 4):
573579
pytest.skip('NameConstant not in python < 3.4')
574580

581+
monkeypatch.setattr(sys, 'version_info', (3, 16, 0))
575582
run_test(source, expected)
576583

577584

0 commit comments

Comments
 (0)