Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next #13

Merged
merged 8 commits into from
Mar 25, 2024
Merged

Next #13

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ on:
jobs:
mypy:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: ${{matrix.python-version}}
architecture: x64
- run: pip install nox==2019.11.9
- run: pip install nox==2022.11.21
- run: pip install poetry==1.2.2
- run: nox --session mypy
- run: nox --session mypy-${{matrix.python-version}}

test:
runs-on: ubuntu-latest
Expand Down
27 changes: 10 additions & 17 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repos:
- id: check-json
- id: end-of-file-fixer
repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
- hooks:
- args:
- --in-place
Expand All @@ -22,15 +22,10 @@ repos:
- --ignore-init-module-imports
id: autoflake
repo: https://github.com/myint/autoflake
rev: v2.0.0

- repo: https://github.com/asottile/setup-cfg-fmt
rev: v2.2.0
hooks:
- id: setup-cfg-fmt
rev: v2.3.1

- repo: https://github.com/asottile/reorder_python_imports
rev: v3.9.0
rev: v3.12.0
hooks:
- args:
- --py37-plus
Expand All @@ -40,31 +35,29 @@ repos:
- --py37-plus
id: pyupgrade
repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
rev: v3.15.2
- hooks:
- id: black
repo: https://github.com/psf/black
rev: 22.12.0
rev: 24.3.0
- hooks:
- additional_dependencies:
- black==20.8b1
id: blacken-docs
- id: blacken-docs
repo: https://github.com/asottile/blacken-docs
rev: v1.12.1
rev: 1.16.0

- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: v2.5.0
rev: v2.12.0
hooks:
- id: pretty-format-yaml
args: [--autofix, --indent, '2']

- hooks:
- id: blackdoc
repo: https://github.com/keewis/blackdoc
rev: v0.3.8
rev: v0.3.9
- hooks:
- id: commitizen
stages:
- commit-msg
repo: https://github.com/commitizen-tools/commitizen
rev: v2.38.0
rev: v3.20.0
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## v0.6.2 (2024-03-25)

### Fix

- minimize (yield) to None
- minimization of MatchClass
- minimization of default arguments
- minimize slice correctly

## v0.6.1 (2023-11-29)

### Fix
Expand Down
308 changes: 184 additions & 124 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pysource-minimize"
version = "0.6.1"
version = "0.6.2"
description = "minimize python source code"
authors = ["Frank Hoffmann"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion pysource_minimize/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
__all__ = ("minimize", "StopMinimization")


version = "0.6.1"
version = "0.6.2"
17 changes: 12 additions & 5 deletions pysource_minimize/_minimize_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def wrap(value):
("arguments", "kw_defaults"),
("Nonlocal", "names"),
("Global", "names"),
("MatchClass", "kwd_attrs"),
]:
setattr(node, name, wrap(value))

Expand Down Expand Up @@ -306,11 +307,17 @@ def try_none(self, node):
return True
return self.try_with({node.__index: None})

def try_only(self, node, child) -> bool:
if isinstance(child, list):
return self.try_with({node.__index: [c.__index for c in child]})
else:
return self.try_with({node.__index: child.__index})
def try_only(self, node, *childs) -> bool:
for child in childs:
if isinstance(child, list):
if self.try_with({node.__index: [c.__index for c in child]}):
return True
elif child is None:
continue
else:
if self.try_with({node.__index: child.__index}):
return True
return False

def try_only_minimize(self, node, *childs):
childs = [child for child in childs if child is not None]
Expand Down
50 changes: 28 additions & 22 deletions pysource_minimize/_minimize_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def minimize(self, o):
return self.minimize_list(o, self.minimize)
elif isinstance(o, ast.arg):
return self.minimize_arg(o)
elif isinstance(o, ast.pattern):
pass
elif isinstance(o, ValueWrapper):
pass
else:
Expand Down Expand Up @@ -106,7 +108,12 @@ def minimize_expr(self, node):
# todo minimize values

elif isinstance(node, ast.Slice):
self.try_only_minimize(node, node.lower, node.upper, node.step)
if self.try_only(node, node.lower, node.upper, node.step):
return

for child in (node.lower, node.upper, node.step):
if not self.try_none(child):
self.minimize(child)

elif isinstance(node, ast.ExtSlice):
self.minimize_list(node.dims, minimal=1)
Expand All @@ -133,6 +140,9 @@ def minimize_expr(self, node):
elif isinstance(node, ast.Await):
self.try_only_minimize(node, node.value)
elif isinstance(node, ast.Yield):
if node.value is None:
if self.try_node(node, ast.Constant(value=None, kind="")):
return
self.try_only_minimize(node, node.value)
elif isinstance(node, ast.YieldFrom):
self.try_only_minimize(node, node.value)
Expand Down Expand Up @@ -242,18 +252,7 @@ def minimize_pattern(pattern):
elif isinstance(pattern, ast.MatchClass):
self.minimize(pattern.cls)
self.minimize_list(pattern.patterns, minimize_pattern)

new_attrs = list(pattern.kwd_attrs)

for i in reversed(range(len(pattern.kwd_patterns))):
try_attrs = [v for j, v in enumerate(new_attrs) if j != i]
if self.try_with(
{
self.index_of(pattern.kwd_patterns[i]): [],
(self.index_of(pattern), "kwd_attrs"): try_attrs,
}
):
new_attrs = try_attrs
self.minimize_lists((pattern.kwd_attrs, pattern.kwd_patterns))

self.minimize(c.body)

Expand Down Expand Up @@ -282,17 +281,25 @@ def minimize_args_of(self, func):

split = len(all_args) - len(args.defaults)
self.minimize_list(all_args[:split])
remaining = self.minimize_lists((all_args[split:], args.defaults))
remaining = self.minimize_lists(
(all_args[split:], args.defaults), (self.minimize, lambda a: None)
)

remove_defaults = True
for _, default in remaining:
if default is not None:
if not self.try_without([default]):
break
if remove_defaults:
if default is not None:
if not self.try_without([default]):
self.minimize(default)
remove_defaults = False
else:
self.minimize(default)

remaining = self.minimize_lists((args.kwonlyargs, args.kw_defaults))
remaining = self.minimize_lists(
(args.kwonlyargs, args.kw_defaults), (self.minimize, lambda a: None)
)
for _, default in remaining:
if default is not None:
self.try_none(default)
self.minimize_optional(default)

self.minimize_optional(args.vararg)
self.minimize_optional(args.kwarg)
Expand Down Expand Up @@ -417,7 +424,7 @@ def minimize_stmt(self, node):
),
):
self.minimize(node.target)
self.minimize(node.value)
self.minimize_optional(node.value)
self.minimize(node.annotation)

elif isinstance(node, (ast.For, ast.AsyncFor)):
Expand Down Expand Up @@ -501,7 +508,6 @@ def minimize_item(item: ast.withitem):
# cause requires exc
# `raise from cause` is not valid
if self.get_ast(node).cause:
coverage_required()
self.minimize(node.exc)
else:
coverage_required()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name_2[needle_17597:]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
raise b'' from 't'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lambda: (yield)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def name_1(*, name_3={name_2: name_0 for name_2 in name_1}):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name_0[name_2:name_1:name_2]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def name_2(name_5=f'{name_1!s}'):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
match name_0:
case name_0(-0.1, name_5=name_5.name_3, name_0=b''):
pass
Loading