From 1e96d8c8302975f3245c376dbe8427cf7e93d0c0 Mon Sep 17 00:00:00 2001 From: jbjd Date: Sat, 17 May 2025 11:05:46 -0500 Subject: [PATCH 01/11] See if tests are version dependent --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index d77c82b..685f73c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ authors = [ name = "personal-python-ast-optimizer" version = "1.1.0" readme = "README.md" -requires-python = ">=3.10" +requires-python = ">=3.11" dependencies = [ "autoflake==2.3.1" ] From c810f24d080d2b2f8f275b12a99bba5ee65de555 Mon Sep 17 00:00:00 2001 From: jbjd Date: Sat, 17 May 2025 11:10:30 -0500 Subject: [PATCH 02/11] Different asserts for version 3.10 --- pyproject.toml | 2 +- tests/parser/test_constant_folding.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 685f73c..d77c82b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ authors = [ name = "personal-python-ast-optimizer" version = "1.1.0" readme = "README.md" -requires-python = ">=3.11" +requires-python = ">=3.10" dependencies = [ "autoflake==2.3.1" ] diff --git a/tests/parser/test_constant_folding.py b/tests/parser/test_constant_folding.py index b6be143..3bec726 100644 --- a/tests/parser/test_constant_folding.py +++ b/tests/parser/test_constant_folding.py @@ -1,4 +1,5 @@ import pytest +import sys from tests.utils import BeforeAndAfter, run_minifiyer_and_assert_correct @@ -57,7 +58,7 @@ """ FAVORITE_NUMBER,a,*_=4,5,6,7,8 """, - "a,*_=(5,6,7,8)", + "a,*_=(5,6,7,8)" if sys.version_info > (3, 10) else "(a,*_)=(5,6,7,8)", ) ), ( @@ -65,7 +66,7 @@ """ *_,FAVORITE_NUMBER,a=4,5,6,7,8 """, - "*_,a=(4,6,7,8)", + "*_,a=(4,6,7,8)" if sys.version_info > (3, 10) else "(*_,a)=(4,6,7,8)", ) ), ( From 8d0d1b426dfeb1f721d1e75fc95404de15719a6b Mon Sep 17 00:00:00 2001 From: jbjd Date: Sat, 17 May 2025 11:12:24 -0500 Subject: [PATCH 03/11] isort --- tests/parser/test_constant_folding.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/parser/test_constant_folding.py b/tests/parser/test_constant_folding.py index 3bec726..68d44d7 100644 --- a/tests/parser/test_constant_folding.py +++ b/tests/parser/test_constant_folding.py @@ -1,6 +1,7 @@ -import pytest import sys +import pytest + from tests.utils import BeforeAndAfter, run_minifiyer_and_assert_correct From fc5120646f6eca737c4452b28c7e3d63a698afa2 Mon Sep 17 00:00:00 2001 From: jbjd Date: Sat, 17 May 2025 11:53:51 -0500 Subject: [PATCH 04/11] Fix tuple compare --- tests/parser/test_constant_folding.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/parser/test_constant_folding.py b/tests/parser/test_constant_folding.py index 68d44d7..d3c6a62 100644 --- a/tests/parser/test_constant_folding.py +++ b/tests/parser/test_constant_folding.py @@ -59,7 +59,11 @@ """ FAVORITE_NUMBER,a,*_=4,5,6,7,8 """, - "a,*_=(5,6,7,8)" if sys.version_info > (3, 10) else "(a,*_)=(5,6,7,8)", + ( + "a,*_=(5,6,7,8)" + if sys.version_info[:2] > (3, 11) + else "(a,*_)=(5,6,7,8)" + ), ) ), ( @@ -67,7 +71,11 @@ """ *_,FAVORITE_NUMBER,a=4,5,6,7,8 """, - "*_,a=(4,6,7,8)" if sys.version_info > (3, 10) else "(*_,a)=(4,6,7,8)", + ( + "*_,a=(4,6,7,8)" + if sys.version_info[:2] > (3, 11) + else "(*_,a)=(4,6,7,8)" + ), ) ), ( From 9bd32b22e61fdb48bdb125ea35a31aa69317ec2a Mon Sep 17 00:00:00 2001 From: jbjd Date: Sat, 17 May 2025 11:54:51 -0500 Subject: [PATCH 05/11] uncomment on pull_request --- .github/workflows/validate-code.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/validate-code.yml b/.github/workflows/validate-code.yml index 5f75b17..e8c2277 100644 --- a/.github/workflows/validate-code.yml +++ b/.github/workflows/validate-code.yml @@ -1,9 +1,9 @@ name: Validate Code on: - # pull_request: - # branches: - # - main + pull_request: + branches: + - main workflow_dispatch: jobs: From 93ef849a4ee1459775fdd3ccf315690754686bbf Mon Sep 17 00:00:00 2001 From: jbjd Date: Sat, 17 May 2025 11:55:43 -0500 Subject: [PATCH 06/11] Correct python version --- tests/parser/test_constant_folding.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/parser/test_constant_folding.py b/tests/parser/test_constant_folding.py index d3c6a62..8e0e5de 100644 --- a/tests/parser/test_constant_folding.py +++ b/tests/parser/test_constant_folding.py @@ -61,7 +61,7 @@ """, ( "a,*_=(5,6,7,8)" - if sys.version_info[:2] > (3, 11) + if sys.version_info[:2] > (3, 10) else "(a,*_)=(5,6,7,8)" ), ) @@ -73,7 +73,7 @@ """, ( "*_,a=(4,6,7,8)" - if sys.version_info[:2] > (3, 11) + if sys.version_info[:2] > (3, 10) else "(*_,a)=(4,6,7,8)" ), ) From e5de04136ebf99d1a60518fcc862a31b9d9c553f Mon Sep 17 00:00:00 2001 From: jbjd Date: Sat, 17 May 2025 12:49:07 -0500 Subject: [PATCH 07/11] Allow assign on same line --- .../parser/minifier.py | 46 ++++++++++++++++--- pyproject.toml | 2 +- tests/parser/test_assign.py | 31 +++++++++++++ 3 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 tests/parser/test_assign.py diff --git a/personal_python_ast_optimizer/parser/minifier.py b/personal_python_ast_optimizer/parser/minifier.py index b54f6c9..157af2b 100644 --- a/personal_python_ast_optimizer/parser/minifier.py +++ b/personal_python_ast_optimizer/parser/minifier.py @@ -21,6 +21,7 @@ class MinifyUnparser(_Unparser): __slots__ = ( "constant_vars_to_fold", + "is_last_node_in_body", "module_name", "target_python_version", "within_class", @@ -41,6 +42,7 @@ def __init__( constant_vars_to_fold if constant_vars_to_fold is not None else {} ) + self.is_last_node_in_body: bool = False self.within_class: bool = False self.within_function: bool = False @@ -67,12 +69,35 @@ def _update_text_to_write(self, text: str) -> str: return text + def visit_node(self, node, is_last_node_in_body: bool = False): + method = "visit_" + node.__class__.__name__ + visitor = getattr(self, method, self.generic_visit) + + previous_state: bool = self.is_last_node_in_body + + self.is_last_node_in_body = is_last_node_in_body + try: + return visitor(node) + finally: + self.is_last_node_in_body = previous_state + + def traverse( + self, node: list[ast.expr] | ast.AST, is_last_node_in_body: bool = False + ) -> None: + if isinstance(node, list): + last_index = len(node) - 1 + for index, item in enumerate(node): + is_last_node_in_body: bool = index == last_index + self.visit_node(item, is_last_node_in_body) + else: + self.visit_node(node) + def visit_Pass(self, _: ast.Pass | None = None) -> None: - same_line: bool = self._last_token_was_colon() + same_line: bool = self._can_write_same_line() self.fill("pass", same_line=same_line) def visit_Return(self, node: ast.Return) -> None: - same_line: bool = self._last_token_was_colon() + same_line: bool = self._can_write_same_line() self.fill("return", same_line=same_line) if node.value and not is_return_none(node): self.write(" ") @@ -157,7 +182,12 @@ def visit_Assign(self, node: ast.Assign) -> None: if len(node.value.elts) == 1: node.value = node.value.elts[0] - super().visit_Assign(node) + self.fill(same_line=self._can_write_same_line()) + for target in node.targets: + self.set_precedence(ast._Precedence.TUPLE, target) + self.traverse(target) + self.write("=") + self.traverse(node.value) def visit_AugAssign(self, node: ast.AugAssign) -> None: self.fill() @@ -267,8 +297,6 @@ def _function_helper( remove_empty_annotations(node) add_pass_if_body_empty(node) - # if len(node.body) == 1: - # self._set_can_write_same_line(node.body[0]) self._write_decorators(node) @@ -301,5 +329,9 @@ def _use_version_optimization(self, python_version: tuple[int, int]) -> bool: return self.target_python_version >= python_version - def _last_token_was_colon(self): - return len(self._source) > 0 and self._source[-1] == ":" + def _can_write_same_line(self): + return ( + len(self._source) > 0 + and self._source[-1] == ":" + and self.is_last_node_in_body + ) diff --git a/pyproject.toml b/pyproject.toml index d77c82b..98a1fda 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ authors = [ {name = "James Demetris"}, ] name = "personal-python-ast-optimizer" -version = "1.1.0" +version = "1.1.1" readme = "README.md" requires-python = ">=3.10" dependencies = [ diff --git a/tests/parser/test_assign.py b/tests/parser/test_assign.py new file mode 100644 index 0000000..9e140a3 --- /dev/null +++ b/tests/parser/test_assign.py @@ -0,0 +1,31 @@ +import pytest + +from tests.utils import BeforeAndAfter, run_minifiyer_and_assert_correct + + +@pytest.mark.parametrize( + "before_and_after", + [ + ( + BeforeAndAfter( + """ +if a > 6: + b = 3 + c = 4 +""", + "if a>6:\n\tb=3\n\tc=4", + ) + ), + ( + BeforeAndAfter( + """ +if a > 6: + b = 3 +""", + "if a>6:b=3", + ) + ), + ], +) +def test_exclude_name_equals_main(before_and_after: BeforeAndAfter): + run_minifiyer_and_assert_correct(before_and_after) From d781d5c64c2f98da16ce51a46f47ce282d69df62 Mon Sep 17 00:00:00 2001 From: jbjd Date: Sat, 17 May 2025 12:52:50 -0500 Subject: [PATCH 08/11] Fix for mypy --- personal_python_ast_optimizer/parser/minifier.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/personal_python_ast_optimizer/parser/minifier.py b/personal_python_ast_optimizer/parser/minifier.py index 157af2b..bde7bb2 100644 --- a/personal_python_ast_optimizer/parser/minifier.py +++ b/personal_python_ast_optimizer/parser/minifier.py @@ -69,7 +69,7 @@ def _update_text_to_write(self, text: str) -> str: return text - def visit_node(self, node, is_last_node_in_body: bool = False): + def visit_node(self, node: ast.AST, is_last_node_in_body: bool = False): method = "visit_" + node.__class__.__name__ visitor = getattr(self, method, self.generic_visit) @@ -77,13 +77,11 @@ def visit_node(self, node, is_last_node_in_body: bool = False): self.is_last_node_in_body = is_last_node_in_body try: - return visitor(node) + return visitor(node) # type: ignore finally: self.is_last_node_in_body = previous_state - def traverse( - self, node: list[ast.expr] | ast.AST, is_last_node_in_body: bool = False - ) -> None: + def traverse(self, node: list[ast.stmt] | ast.AST) -> None: if isinstance(node, list): last_index = len(node) - 1 for index, item in enumerate(node): @@ -184,7 +182,7 @@ def visit_Assign(self, node: ast.Assign) -> None: self.fill(same_line=self._can_write_same_line()) for target in node.targets: - self.set_precedence(ast._Precedence.TUPLE, target) + self.set_precedence(ast._Precedence.TUPLE, target) # type: ignore self.traverse(target) self.write("=") self.traverse(node.value) From 938e2620819b74034b30f3933863c19c539c5891 Mon Sep 17 00:00:00 2001 From: jbjd Date: Fri, 23 May 2025 17:51:38 -0500 Subject: [PATCH 09/11] Write continue and raise on same line --- .../parser/minifier.py | 24 +++- personal_python_ast_optimizer/parser/utils.py | 7 + pyproject.toml | 2 +- tests/parser/conftest.py | 27 +--- tests/parser/test_assign.py | 36 +++--- tests/parser/test_constant_folding.py | 122 +++++++++--------- tests/parser/test_continue.py | 13 ++ tests/parser/test_exception.py | 13 ++ tests/parser/test_imports.py | 19 ++- tests/parser/test_versions.py | 12 +- 10 files changed, 161 insertions(+), 114 deletions(-) create mode 100644 tests/parser/test_continue.py create mode 100644 tests/parser/test_exception.py diff --git a/personal_python_ast_optimizer/parser/minifier.py b/personal_python_ast_optimizer/parser/minifier.py index bde7bb2..39a7b79 100644 --- a/personal_python_ast_optimizer/parser/minifier.py +++ b/personal_python_ast_optimizer/parser/minifier.py @@ -69,7 +69,7 @@ def _update_text_to_write(self, text: str) -> str: return text - def visit_node(self, node: ast.AST, is_last_node_in_body: bool = False): + def visit_node(self, node: ast.AST, is_last_node_in_body: bool = False) -> None: method = "visit_" + node.__class__.__name__ visitor = getattr(self, method, self.generic_visit) @@ -94,6 +94,10 @@ def visit_Pass(self, _: ast.Pass | None = None) -> None: same_line: bool = self._can_write_same_line() self.fill("pass", same_line=same_line) + def visit_Continue(self, _: ast.Continue | None = None) -> None: + same_line: bool = self._can_write_same_line() + self.fill("continue", same_line=same_line) + def visit_Return(self, node: ast.Return) -> None: same_line: bool = self._can_write_same_line() self.fill("return", same_line=same_line) @@ -101,6 +105,22 @@ def visit_Return(self, node: ast.Return) -> None: self.write(" ") self.traverse(node.value) + def visit_Raise(self, node: ast.Raise) -> None: + same_line: bool = self._can_write_same_line() + self.fill("raise", same_line=same_line) + + if not node.exc: + if node.cause: + raise ValueError("Node can't use cause without an exception.") + return + + self.write(" ") + self.traverse(node.exc) + + if node.cause: + self.write(" from ") + self.traverse(node.cause) + def visit_ImportFrom(self, node: ast.ImportFrom) -> None: """Skip unnecessary futures imports""" if node.module == "__future__" and self.target_python_version is not None: @@ -327,7 +347,7 @@ def _use_version_optimization(self, python_version: tuple[int, int]) -> bool: return self.target_python_version >= python_version - def _can_write_same_line(self): + def _can_write_same_line(self) -> bool: return ( len(self._source) > 0 and self._source[-1] == ":" diff --git a/personal_python_ast_optimizer/parser/utils.py b/personal_python_ast_optimizer/parser/utils.py index 693dbde..74983b6 100644 --- a/personal_python_ast_optimizer/parser/utils.py +++ b/personal_python_ast_optimizer/parser/utils.py @@ -65,6 +65,13 @@ def is_return_none(node: ast.Return) -> bool: return isinstance(node.value, ast.Constant) and node.value.value is None +# def can_skip_paranthesis_in_raise(node: ast.Raise) -> bool: +# """Returns True when exception raised with empty paranthesis like: +# raise Exception() +# so caller knowns node can be written without them""" +# return isinstance(node.exc, ast.Call) and len(node.exc.args) == 0 + + def remove_dangling_expressions(node: ast.ClassDef | ast.FunctionDef) -> None: """Removes constant daggling expression like doc strings""" node.body = [ diff --git a/pyproject.toml b/pyproject.toml index 98a1fda..f4676ee 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ authors = [ {name = "James Demetris"}, ] name = "personal-python-ast-optimizer" -version = "1.1.1" +version = "1.1.2" readme = "README.md" requires-python = ">=3.10" dependencies = [ diff --git a/tests/parser/conftest.py b/tests/parser/conftest.py index 096efac..3e6ea18 100644 --- a/tests/parser/conftest.py +++ b/tests/parser/conftest.py @@ -1,6 +1,6 @@ import pytest -from tests.utils import BeforeAndAfter, BeforeAndAfterBasedOnVersion +from tests.utils import BeforeAndAfter @pytest.fixture @@ -102,17 +102,6 @@ class SomeTuple: ) -@pytest.fixture -def ignorable_bases_class() -> BeforeAndAfterBasedOnVersion: - return BeforeAndAfterBasedOnVersion( - """ -class Foo(object): - pass -""", - {"3.0": "class Foo:pass", None: "class Foo(object):pass"}, - ) - - @pytest.fixture def annotations_script() -> BeforeAndAfter: return BeforeAndAfter("a: int;b: int = 2;b -= 1", "b=2\nb-=1") @@ -132,20 +121,6 @@ def one_line_if_script() -> BeforeAndAfter: ) -@pytest.fixture -def futures_imports() -> BeforeAndAfterBasedOnVersion: - many_futures_imports: str = """ -from __future__ import annotations -from __future__ import generator_stop -from __future__ import unicode_literals -from __future__ import with_statement -""" - return BeforeAndAfterBasedOnVersion( - many_futures_imports, - {"3.7": "", None: many_futures_imports.strip()}, - ) - - @pytest.fixture def name_equals_main_excludes() -> BeforeAndAfter: return BeforeAndAfter( diff --git a/tests/parser/test_assign.py b/tests/parser/test_assign.py index 9e140a3..db06205 100644 --- a/tests/parser/test_assign.py +++ b/tests/parser/test_assign.py @@ -3,29 +3,29 @@ from tests.utils import BeforeAndAfter, run_minifiyer_and_assert_correct -@pytest.mark.parametrize( - "before_and_after", - [ - ( - BeforeAndAfter( - """ +_assign_cases = [ + ( + BeforeAndAfter( + """ if a > 6: b = 3 c = 4 """, - "if a>6:\n\tb=3\n\tc=4", - ) - ), - ( - BeforeAndAfter( - """ + "if a>6:\n\tb=3\n\tc=4", + ) + ), + ( + BeforeAndAfter( + """ if a > 6: b = 3 """, - "if a>6:b=3", - ) - ), - ], -) -def test_exclude_name_equals_main(before_and_after: BeforeAndAfter): + "if a>6:b=3", + ) + ), +] + + +@pytest.mark.parametrize("before_and_after", _assign_cases) +def test_assign(before_and_after: BeforeAndAfter): run_minifiyer_and_assert_correct(before_and_after) diff --git a/tests/parser/test_constant_folding.py b/tests/parser/test_constant_folding.py index 8e0e5de..3ef8f97 100644 --- a/tests/parser/test_constant_folding.py +++ b/tests/parser/test_constant_folding.py @@ -5,89 +5,89 @@ from tests.utils import BeforeAndAfter, run_minifiyer_and_assert_correct -@pytest.mark.parametrize( - "before_and_after", - [ - ( - BeforeAndAfter( - """ +_exclude_name_equals_main_cases = [ + ( + BeforeAndAfter( + """ from foo import FAVORITE_NUMBER a = FAVORITE_NUMBER """, - "a=6", - ) - ), - ( - BeforeAndAfter( - """ + "a=6", + ) + ), + ( + BeforeAndAfter( + """ FAVORITE_NUMBER = 6 a = FAVORITE_NUMBER """, - "a=6", - ) - ), - ( - BeforeAndAfter( - """ + "a=6", + ) + ), + ( + BeforeAndAfter( + """ FAVORITE_NUMBER: int = 6 a = FAVORITE_NUMBER """, - "a=6", - ) - ), - ( - BeforeAndAfter( - """ + "a=6", + ) + ), + ( + BeforeAndAfter( + """ FAVORITE_NUMBER=a=6 """, - "a=6", - ) - ), - ( - BeforeAndAfter( - """ + "a=6", + ) + ), + ( + BeforeAndAfter( + """ FAVORITE_NUMBER,a=4,5 """, - "a=5", - ) - ), - ( - BeforeAndAfter( - """ + "a=5", + ) + ), + ( + BeforeAndAfter( + """ FAVORITE_NUMBER,a,*_=4,5,6,7,8 """, - ( - "a,*_=(5,6,7,8)" - if sys.version_info[:2] > (3, 10) - else "(a,*_)=(5,6,7,8)" - ), - ) - ), - ( - BeforeAndAfter( - """ + ( + "a,*_=(5,6,7,8)" + if sys.version_info[:2] > (3, 10) + else "(a,*_)=(5,6,7,8)" + ), + ) + ), + ( + BeforeAndAfter( + """ *_,FAVORITE_NUMBER,a=4,5,6,7,8 """, - ( - "*_,a=(4,6,7,8)" - if sys.version_info[:2] > (3, 10) - else "(*_,a)=(4,6,7,8)" - ), - ) - ), - ( - BeforeAndAfter( - """ + ( + "*_,a=(4,6,7,8)" + if sys.version_info[:2] > (3, 10) + else "(*_,a)=(4,6,7,8)" + ), + ) + ), + ( + BeforeAndAfter( + """ FAVORITE_NUMBER,TEST=4,5 """, - "", - ) - ), - ], -) + "", + ) + ), +] + + +@pytest.mark.parametrize("before_and_after", _exclude_name_equals_main_cases) def test_exclude_name_equals_main(before_and_after: BeforeAndAfter): run_minifiyer_and_assert_correct( before_and_after, diff --git a/tests/parser/test_continue.py b/tests/parser/test_continue.py new file mode 100644 index 0000000..ea1abdc --- /dev/null +++ b/tests/parser/test_continue.py @@ -0,0 +1,13 @@ +from tests.utils import BeforeAndAfter, run_minifiyer_and_assert_correct + + +def test_continue_same_line(): + before_and_after = BeforeAndAfter( + """ +if a > 6: + continue +""", + "if a>6:continue", + ) + + run_minifiyer_and_assert_correct(before_and_after) diff --git a/tests/parser/test_exception.py b/tests/parser/test_exception.py new file mode 100644 index 0000000..97f6d91 --- /dev/null +++ b/tests/parser/test_exception.py @@ -0,0 +1,13 @@ +from tests.utils import BeforeAndAfter, run_minifiyer_and_assert_correct + + +def test_continue_same_line(): + before_and_after = BeforeAndAfter( + """ +if a > 6: + raise Exception() +""", + "if a>6:raise Exception()", + ) + + run_minifiyer_and_assert_correct(before_and_after) diff --git a/tests/parser/test_imports.py b/tests/parser/test_imports.py index 067c2f4..8735291 100644 --- a/tests/parser/test_imports.py +++ b/tests/parser/test_imports.py @@ -4,7 +4,18 @@ ) -def test_futures_imports( - futures_imports: BeforeAndAfterBasedOnVersion, -): - run_minifiyer_and_assert_correct_multiple_versions(futures_imports) +def test_futures_imports(): + + many_futures_imports: str = """ +from __future__ import annotations +from __future__ import generator_stop +from __future__ import unicode_literals +from __future__ import with_statement +""" + + before_and_after = BeforeAndAfterBasedOnVersion( + many_futures_imports, + {"3.7": "", None: many_futures_imports.strip()}, + ) + + run_minifiyer_and_assert_correct_multiple_versions(before_and_after) diff --git a/tests/parser/test_versions.py b/tests/parser/test_versions.py index a67ed75..2b89530 100644 --- a/tests/parser/test_versions.py +++ b/tests/parser/test_versions.py @@ -6,5 +6,13 @@ ) -def test_class_ignorable_bases(ignorable_bases_class: BeforeAndAfterBasedOnVersion): - run_minifiyer_and_assert_correct_multiple_versions(ignorable_bases_class) +def test_class_ignorable_bases(): + before_and_after = BeforeAndAfterBasedOnVersion( + """ +class Foo(object): + pass +""", + {"3.0": "class Foo:pass", None: "class Foo(object):pass"}, + ) + + run_minifiyer_and_assert_correct_multiple_versions(before_and_after) From 3597881520a02b8ec2e9e8f306fe3a1a8e24b849 Mon Sep 17 00:00:00 2001 From: jbjd Date: Fri, 23 May 2025 17:54:26 -0500 Subject: [PATCH 10/11] Remove commented code --- personal_python_ast_optimizer/parser/utils.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/personal_python_ast_optimizer/parser/utils.py b/personal_python_ast_optimizer/parser/utils.py index 74983b6..693dbde 100644 --- a/personal_python_ast_optimizer/parser/utils.py +++ b/personal_python_ast_optimizer/parser/utils.py @@ -65,13 +65,6 @@ def is_return_none(node: ast.Return) -> bool: return isinstance(node.value, ast.Constant) and node.value.value is None -# def can_skip_paranthesis_in_raise(node: ast.Raise) -> bool: -# """Returns True when exception raised with empty paranthesis like: -# raise Exception() -# so caller knowns node can be written without them""" -# return isinstance(node.exc, ast.Call) and len(node.exc.args) == 0 - - def remove_dangling_expressions(node: ast.ClassDef | ast.FunctionDef) -> None: """Removes constant daggling expression like doc strings""" node.body = [ From 77e8f3be7dbc068bf2b4ed5388cb3ae7c00f7115 Mon Sep 17 00:00:00 2001 From: jbjd Date: Fri, 23 May 2025 17:55:33 -0500 Subject: [PATCH 11/11] Isort --- tests/parser/test_assign.py | 1 - tests/parser/test_constant_folding.py | 1 - 2 files changed, 2 deletions(-) diff --git a/tests/parser/test_assign.py b/tests/parser/test_assign.py index db06205..977f8d6 100644 --- a/tests/parser/test_assign.py +++ b/tests/parser/test_assign.py @@ -2,7 +2,6 @@ from tests.utils import BeforeAndAfter, run_minifiyer_and_assert_correct - _assign_cases = [ ( BeforeAndAfter( diff --git a/tests/parser/test_constant_folding.py b/tests/parser/test_constant_folding.py index 3ef8f97..65d3e3f 100644 --- a/tests/parser/test_constant_folding.py +++ b/tests/parser/test_constant_folding.py @@ -4,7 +4,6 @@ from tests.utils import BeforeAndAfter, run_minifiyer_and_assert_correct - _exclude_name_equals_main_cases = [ ( BeforeAndAfter(