Skip to content

Commit 6bc5c1e

Browse files
committed
Remove builtin exceptions brackets
1 parent 374f003 commit 6bc5c1e

File tree

6 files changed

+123
-12
lines changed

6 files changed

+123
-12
lines changed

docs/source/transforms/remove_exception_brackets.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
Remove Exception Brackets
2-
=========================
1+
Remove Builtin Exception Brackets
2+
=================================
33

44
This transform removes parentheses when raising builtin exceptions with no arguments.
55

66
The raise statement automatically instantiates exceptions with no arguments, so the parentheses are unnecessary.
77

8-
This transform is enabled by default. Disable by passing the ``remove_exception_brackets=False`` argument to the :func:`python_minifier.minify` function,
9-
or passing ``--no-remove-exception-brackets`` to the pyminify command.
8+
This transform is enabled by default. Disable by passing the ``remove_builtin_exception_brackets=False`` argument to the :func:`python_minifier.minify` function,
9+
or passing ``--no-remove-builtin-exception-brackets`` to the pyminify command.
1010

1111
Example
1212
-------

src/python_minifier/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def minify(
6969
remove_asserts=False,
7070
remove_debug=False,
7171
remove_explicit_return_none=True,
72-
remove_exception_brackets=True,
72+
remove_builtin_exception_brackets=True
7373
):
7474
"""
7575
Minify a python module
@@ -101,7 +101,7 @@ def minify(
101101
:param bool remove_asserts: If assert statements should be removed
102102
:param bool remove_debug: If conditional statements that test '__debug__ is True' should be removed
103103
:param bool remove_explicit_return_none: If explicit return None statements should be replaced with a bare return
104-
:param bool remove_exception_brackets: If brackets should be removed when raising exceptions with no arguments
104+
:param bool remove_builtin_exception_brackets: If brackets should be removed when raising exceptions with no arguments
105105
106106
:rtype: str
107107
@@ -153,7 +153,7 @@ def minify(
153153
bind_names(module)
154154
resolve_names(module)
155155

156-
if remove_exception_brackets:
156+
if remove_builtin_exception_brackets:
157157
remove_no_arg_exception_call(module)
158158

159159
if module.tainted:

src/python_minifier/__init__.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ def minify(
2323
preserve_shebang: bool = ...,
2424
remove_asserts: bool = ...,
2525
remove_debug: bool = ...,
26-
remove_explicit_return_none: bool = ...
27-
remove_exception_brackets: bool = ...,
26+
remove_explicit_return_none: bool = ...,
27+
remove_builtin_exception_brackets: bool = ...
2828
) -> Text: ...
2929

3030
def unparse(module: ast.Module) -> Text: ...

src/python_minifier/__main__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ def parse_args():
185185
dest='remove_explicit_return_none',
186186
)
187187
minification_options.add_argument(
188-
'--no-remove-exception-brackets',
188+
'--no-remove-builtin-exception-brackets',
189189
action='store_false',
190-
help='Disable removing brackets when raising exceptions with no arguments',
190+
help='Disable removing brackets when raising builtin exceptions with no arguments',
191191
dest='remove_exception_brackets',
192192
)
193193

@@ -308,7 +308,7 @@ def do_minify(source, filename, minification_args):
308308
remove_asserts=minification_args.remove_asserts,
309309
remove_debug=minification_args.remove_debug,
310310
remove_explicit_return_none=minification_args.remove_explicit_return_none,
311-
remove_exception_brackets=minification_args.remove_exception_brackets
311+
remove_builtin_exception_brackets=minification_args.remove_exception_brackets
312312
)
313313

314314

src/python_minifier/transforms/remove_exception_brackets.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,5 @@ def remove_no_arg_exception_call(module):
120120
if isinstance(binding, BuiltinBinding) and binding.name in builtin_exceptions:
121121
# We can remove any calls to builtin exceptions
122122
_remove_empty_call(binding)
123+
124+
return module
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import ast
2+
import sys
3+
4+
import pytest
5+
6+
from python_minifier.rename import add_namespace, bind_names, resolve_names
7+
from python_minifier.ast_compare import compare_ast
8+
from python_minifier.transforms.remove_exception_brackets import remove_no_arg_exception_call
9+
10+
11+
def remove_brackets(source):
12+
module = ast.parse(source, 'remove_brackets')
13+
14+
add_namespace(module)
15+
bind_names(module)
16+
resolve_names(module)
17+
return remove_no_arg_exception_call(module)
18+
19+
20+
def test_exception_brackets():
21+
"""This is a buitin so remove the brackets"""
22+
source = 'def a(): raise Exception()'
23+
expected = 'def a(): raise Exception'
24+
25+
expected_ast = ast.parse(expected)
26+
actual_ast = remove_brackets(source)
27+
compare_ast(expected_ast, actual_ast)
28+
29+
def test_zero_division_error_brackets():
30+
"""This is a buitin so remove the brackets"""
31+
source = 'def a(): raise ZeroDivisionError()'
32+
expected = 'def a(): raise ZeroDivisionError'
33+
34+
expected_ast = ast.parse(expected)
35+
actual_ast = remove_brackets(source)
36+
compare_ast(expected_ast, actual_ast)
37+
38+
def test_builtin_with_arg():
39+
"""This has an arg so dont' remove the brackets"""
40+
source = 'def a(): raise Exception(1)'
41+
expected = 'def a(): raise Exception(1)'
42+
43+
expected_ast = ast.parse(expected)
44+
actual_ast = remove_brackets(source)
45+
compare_ast(expected_ast, actual_ast)
46+
47+
def test_one_division_error_brackets():
48+
"""This is not a builtin so don't remove the brackets even though it's not defined in the module"""
49+
source = 'def a(): raise OneDivisionError()'
50+
expected = source
51+
52+
expected_ast = ast.parse(expected)
53+
actual_ast = remove_brackets(source)
54+
compare_ast(expected_ast, actual_ast)
55+
56+
def test_redefined():
57+
"""This is usually a builtin, but don't remove brackets if it's been redefined"""
58+
source = '''
59+
def a():
60+
raise ZeroDivisionError()
61+
def b():
62+
ZeroDivisionError = blag
63+
raise ZeroDivisionError()
64+
'''
65+
expected = '''
66+
def a():
67+
raise ZeroDivisionError
68+
def b():
69+
ZeroDivisionError = blag
70+
raise ZeroDivisionError()
71+
'''
72+
expected_ast = ast.parse(expected)
73+
actual_ast = remove_brackets(source)
74+
compare_ast(expected_ast, actual_ast)
75+
76+
def test_raise_from():
77+
"""This is a builtin so remove the brackets"""
78+
if sys.version_info < (3, 0):
79+
pytest.skip('raise from not supported in this version of python')
80+
source = 'def a(): raise Exception() from Exception()'
81+
expected = 'def a(): raise Exception from Exception'
82+
83+
expected_ast = ast.parse(expected)
84+
actual_ast = remove_brackets(source)
85+
compare_ast(expected_ast, actual_ast)
86+
87+
def test_raise_from_only():
88+
"""This is a builtin so remove the brackets"""
89+
if sys.version_info < (3, 0):
90+
pytest.skip('raise from not supported in this version of python')
91+
92+
source = 'def a(): raise Hello() from Exception()'
93+
expected = 'def a(): raise Hello() from Exception'
94+
95+
expected_ast = ast.parse(expected)
96+
actual_ast = remove_brackets(source)
97+
compare_ast(expected_ast, actual_ast)
98+
99+
def test_raise_from_arg():
100+
"""This is a builtin so remove the brackets"""
101+
if sys.version_info < (3, 0):
102+
pytest.skip('raise from not supported in this version of python')
103+
104+
source = 'def a(): raise Hello() from Exception(1)'
105+
expected = 'def a(): raise Hello() from Exception(1)'
106+
107+
expected_ast = ast.parse(expected)
108+
actual_ast = remove_brackets(source)
109+
compare_ast(expected_ast, actual_ast)

0 commit comments

Comments
 (0)