Skip to content

Commit

Permalink
fix: minimize except handler name
Browse files Browse the repository at this point in the history
  • Loading branch information
15r10nk committed Oct 5, 2023
1 parent b8a8af4 commit 91e14e5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
4 changes: 4 additions & 0 deletions pysource_minimize/_minimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,10 @@ def minimize_except_handler(handler):
if not handler.name and not try_star:
self.minimize_optional(handler.type)

if handler.name:
self.try_attr(handler,"name",None)


self.minimize_list(
node.handlers, minimize_except_handler, 1 # 0 if node.finalbody else 1
)
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def pytest_sessionfinish(session, exitstatus):
from .test_remove_one import generate_remove_one
from .test_needle import generate_needle

for i in range(10):
for i in range(2):
generate_remove_one()
generate_needle()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
try:
pass
except name_2 if name_4 else name_2 as name_0:
pass
except {name_3 for name_3 in name_0 if name_4} as name_1:
pass
45 changes: 25 additions & 20 deletions tests/test_remove_one.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def node_weights(source):

def weight(node):

result = 1
if isinstance(
node,
(
Expand All @@ -45,65 +46,69 @@ def weight(node):
ast.ImportFrom,
),
):
return 0
result = 0

if isinstance(node, (ast.GeneratorExp, ast.ListComp, ast.SetComp)):
return 0
result = 0
if isinstance(node, (ast.DictComp)):
return -2
result = -2
if isinstance(node, ast.comprehension):
# removing comrehension removes variable and iterable
return -1
result = -1

if isinstance(node, (ast.Dict)):
return -len(node.keys) + 1
result = -len(node.keys) + 1
if sys.version_info >= (3, 8) and isinstance(node, ast.NamedExpr):
return 0
result = 0

if isinstance(node, ast.FormattedValue):
return 0
result = 0
if isinstance(node, ast.JoinedStr):
# work around for https://github.com/python/cpython/issues/110309
return -(
result = -(
sum(isinstance(n, ast.Constant) and n.value == "" for n in node.values)
)

if isinstance(node, ast.IfExp):
return -1
result = -1
if isinstance(node, ast.Subscript):
return 0
result = 0
if isinstance(node, ast.Index):
return 0
result = 0

# match
if sys.version_info >= (3, 10):
if isinstance(node, ast.MatchValue):
return -1
result = -1
if isinstance(node, (ast.MatchOr, ast.match_case, ast.MatchClass)):
return 0
result = 0
if isinstance(node, ast.Match):
return -1 # for the subject
result = -1 # for the subject
if isinstance(node, ast.MatchMapping):
# key-value pairs can only be removed together
return -len(node.patterns) + 1
result = -len(node.patterns) + 1

# try
if sys.version_info >= (3, 11) and isinstance(node, ast.TryStar):
# execpt*: is invalid syntax
return -len(node.handlers) + 1
result = -len(node.handlers) + 1

if isinstance(node, ast.excepthandler):
return 0
result = 0
if node.name:
result+=1

if isinstance(node, ast.arguments):
# kw_defaults and kwonlyargs can only be removed together
return -len(node.kw_defaults)
result = -len(node.kw_defaults)

if sys.version_info >= (3, 12):
if isinstance(node, ast.TypeAlias):
return 0
result = 0

return 1


return result

return [(n, weight(n)) for n in ast.walk(tree)]

Expand Down

0 comments on commit 91e14e5

Please sign in to comment.