Skip to content

Commit 59d52cf

Browse files
authored
Merge pull request #36 from 15r10nk/3.14
3.14
2 parents 4588ab8 + 60891b6 commit 59d52cf

8 files changed

+81
-8
lines changed

src/pysource_minimize/_minimize_base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def wrap(value):
115115
("Nonlocal", "names"),
116116
("Global", "names"),
117117
("MatchClass", "kwd_attrs"),
118+
("Dict", "keys"),
118119
]:
119120
setattr(node, name, wrap(value))
120121

src/pysource_minimize/_minimize_structure.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ def minimize_expr(self, node):
7474
)
7575

7676
elif isinstance(node, ast.Subscript):
77+
if isinstance(node.ctx, ast.Store):
78+
if self.try_node(node, ast.Name(id="new_name", ctx=ast.Store())):
79+
return
80+
7781
self.try_only_minimize(node, node.value, node.slice)
7882

7983
elif isinstance(node, ast.FormattedValue):
@@ -227,6 +231,41 @@ def minimize_expr(self, node):
227231

228232
elif isinstance(node, ast.NamedExpr):
229233
self.try_only_minimize(node, node.target, node.value)
234+
235+
elif isinstance(node, ast.Interpolation):
236+
if isinstance(node.format_spec, ast.JoinedStr):
237+
# work around for https://github.com/python/cpython/issues/110309
238+
spec = [
239+
v
240+
for v in node.format_spec.values
241+
if not (isinstance(v, ast.Constant) and v.value == "")
242+
]
243+
if len(spec) == 1 and self.try_only(node, spec[0]):
244+
coverage_required()
245+
self.minimize(spec[0])
246+
return
247+
248+
if not self.try_none(node.format_spec):
249+
self.minimize(node.format_spec)
250+
251+
self.minimize_expr(node.value)
252+
253+
elif isinstance(node, ast.TemplateStr):
254+
for v in node.values:
255+
if isinstance(v, ast.Interpolation) and self.try_only(node, v.value):
256+
self.minimize(v.value)
257+
return
258+
if (
259+
isinstance(v, ast.Interpolation)
260+
and v.format_spec
261+
and self.try_only(node, v.format_spec)
262+
):
263+
self.minimize(v.format_spec)
264+
return
265+
266+
self.minimize(node.values)
267+
# todo minimize values
268+
230269
else:
231270
assert False, "expression is not handled %s" % (node)
232271

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
for 0[0] in 0:
2+
pass
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0[0]: 0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
t'{name_4!s}'

tests/test_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def minimize_files(
5353
print("\n".join(format_tb(result.exc_info[2])))
5454
print(result.exception)
5555

56-
output = re.sub("'/.*python", "'python", result.output)
56+
output = re.sub("'/.*python3?", "'python", result.output)
5757

5858
assert output == expected_output
5959

@@ -192,7 +192,7 @@ def test_no_track():
192192
expected_output=snapshot(
193193
"""\
194194
I don't know what you want to minimize for.
195-
'bbbb' is not a string which in the stdout/stderr of 'python3 bug.py'
195+
'bbbb' is not a string which in the stdout/stderr of 'python bug.py'
196196
"""
197197
),
198198
expected_files=snapshot("<unchanged>"),

tests/test_remove_children.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,30 @@
2222
sample_dir.mkdir(exist_ok=True)
2323

2424

25+
def is_trivial_node(node):
26+
"""
27+
trivial nodes are:
28+
* nodes without ast-nodes as children or
29+
* none nodes at all
30+
"""
31+
if not isinstance(node, (ast.expr, ast.stmt)):
32+
return True
33+
return (
34+
sum(isinstance(n, (ast.expr, ast.stmt)) for n in ast.iter_child_nodes(node))
35+
== 0
36+
)
37+
38+
39+
# EXCEPTIONS = {ast.AnnAssign: {"target": ast.Subscript}}
40+
EXCEPTIONS = {}
41+
42+
2543
def is_simple_node(node: Optional[ast.AST]):
2644
if node is None:
2745
return True
2846

47+
assert not isinstance(node, list)
48+
2949
if sys.version_info >= (3, 10):
3050
if (
3151
isinstance(node, ast.Match)
@@ -41,12 +61,14 @@ def is_simple_node(node: Optional[ast.AST]):
4161
return True
4262

4363
for name, field in ast.iter_fields(node):
44-
if (
45-
isinstance(field, (ast.expr, ast.stmt))
46-
and len([n for n in ast.walk(field) if isinstance(n, (ast.expr, ast.stmt))])
47-
> 1
48-
):
49-
return False
64+
exceptions = EXCEPTIONS.get(type(node), [])
65+
if name in exceptions and isinstance(field, exceptions[name]):
66+
if not is_simple_node(field):
67+
return False
68+
else:
69+
if not is_trivial_node(field):
70+
return False
71+
5072
return True
5173

5274

tests/test_remove_one.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ def weight(node):
8888
# work around for https://github.com/python/cpython/issues/110309
8989
result = -(sum(isinstance(n, ast.Constant) for n in node.values))
9090

91+
if sys.version_info >= (3, 14):
92+
if isinstance(node, ast.Interpolation):
93+
result = 0
94+
if isinstance(node, ast.TemplateStr):
95+
# work around for https://github.com/python/cpython/issues/110309
96+
result = -(sum(isinstance(n, ast.Constant) for n in node.values))
97+
9198
if isinstance(node, ast.IfExp):
9299
result = -1
93100
if isinstance(node, ast.Subscript):

0 commit comments

Comments
 (0)