From e95970cc7b37e33eb981eb745a55731ef0a8974c Mon Sep 17 00:00:00 2001 From: tkucar Date: Tue, 4 Mar 2025 01:59:58 +0100 Subject: [PATCH 1/5] fix unpacking --- src/codegen/sdk/python/assignment.py | 6 +++++ .../sdk/python/expressions/test_unpacking.py | 26 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/codegen/sdk/python/assignment.py b/src/codegen/sdk/python/assignment.py index a85f57a82..221c9a87f 100644 --- a/src/codegen/sdk/python/assignment.py +++ b/src/codegen/sdk/python/assignment.py @@ -12,6 +12,7 @@ from codegen.sdk.python.symbol import PySymbol from codegen.sdk.python.symbol_groups.comment_group import PyCommentGroup from codegen.shared.decorators.docs import noapidoc, py_apidoc +from codegen.shared.logging.get_logger import get_logger if TYPE_CHECKING: from tree_sitter import Node as TSNode @@ -19,6 +20,8 @@ from codegen.sdk.codebase.codebase_context import CodebaseContext from codegen.sdk.core.node_id_factory import NodeId from codegen.sdk.python.statements.assignment_statement import PyAssignmentStatement + +logger = get_logger(__name__) @py_apidoc @@ -152,6 +155,9 @@ def remove(self, delete_formatting: bool = True, priority: int = 0, dedupe: bool else: self.parent._values_scheduled_for_removal = [] else: + if name.source=="_": + logger.warning("Attempting to remove '_' in unpacking, command will be ignored if you wish to remove the statement, remove the other remaining variable(s)!") + return transaction_count = self._active_transactions_on_assignment_names(TransactionPriority.Edit) throwaway = [asgnmt.name == "_" for asgnmt in self.parent.assignments].count(True) # Only edit if we didn't already omit all the other assignments, otherwise just remove the whole thing diff --git a/tests/unit/codegen/sdk/python/expressions/test_unpacking.py b/tests/unit/codegen/sdk/python/expressions/test_unpacking.py index cdf853e37..fb67d56fd 100644 --- a/tests/unit/codegen/sdk/python/expressions/test_unpacking.py +++ b/tests/unit/codegen/sdk/python/expressions/test_unpacking.py @@ -155,3 +155,29 @@ def test_remove_unpacking_assignment_num(tmpdir) -> None: assert len(file2.symbols) == 0 assert file2.source == """""" + + +def test_unpacking_function_with_underscore_removal(tmpdir:str) -> None: + + # language=python + content1 = """ + args, _ = parser.parse_known_args() ##args gets deleted + with open(args.template_path) as f: + print('test') + """ + with get_codebase_session( + tmpdir=tmpdir, + files={ + "file1.py": content1, + }, + ) as codebase: + + file1: SourceFile = codebase.get_file("file1.py") + + + for symbol in codebase.symbols: + if not symbol.usages: + symbol.remove() + codebase.commit() + #The first TEST_BOOL Assigment gets removed when it should stay due to conditionality + assert len(file1.symbols) !=0 \ No newline at end of file From 40cf1c540a91e488f27bb21334f71398f3f72459 Mon Sep 17 00:00:00 2001 From: tkucar Date: Tue, 4 Mar 2025 02:00:22 +0100 Subject: [PATCH 2/5] lint --- src/codegen/sdk/python/assignment.py | 2 +- .../codegen/sdk/python/expressions/test_unpacking.py | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/codegen/sdk/python/assignment.py b/src/codegen/sdk/python/assignment.py index 221c9a87f..fadc08ab4 100644 --- a/src/codegen/sdk/python/assignment.py +++ b/src/codegen/sdk/python/assignment.py @@ -20,7 +20,7 @@ from codegen.sdk.codebase.codebase_context import CodebaseContext from codegen.sdk.core.node_id_factory import NodeId from codegen.sdk.python.statements.assignment_statement import PyAssignmentStatement - + logger = get_logger(__name__) diff --git a/tests/unit/codegen/sdk/python/expressions/test_unpacking.py b/tests/unit/codegen/sdk/python/expressions/test_unpacking.py index fb67d56fd..875bfcf99 100644 --- a/tests/unit/codegen/sdk/python/expressions/test_unpacking.py +++ b/tests/unit/codegen/sdk/python/expressions/test_unpacking.py @@ -1,5 +1,10 @@ +from typing import TYPE_CHECKING + from codegen.sdk.codebase.factory.get_session import get_codebase_session +if TYPE_CHECKING: + from codegen.sdk.core.file import SourceFile + def test_remove_unpacking_assignment(tmpdir) -> None: # language=python @@ -161,7 +166,7 @@ def test_unpacking_function_with_underscore_removal(tmpdir:str) -> None: # language=python content1 = """ - args, _ = parser.parse_known_args() ##args gets deleted + args, _ = parser.parse_known_args() ##args gets deleted with open(args.template_path) as f: print('test') """ @@ -171,7 +176,7 @@ def test_unpacking_function_with_underscore_removal(tmpdir:str) -> None: "file1.py": content1, }, ) as codebase: - + file1: SourceFile = codebase.get_file("file1.py") @@ -180,4 +185,4 @@ def test_unpacking_function_with_underscore_removal(tmpdir:str) -> None: symbol.remove() codebase.commit() #The first TEST_BOOL Assigment gets removed when it should stay due to conditionality - assert len(file1.symbols) !=0 \ No newline at end of file + assert len(file1.symbols) != 0 From d17d9e3a0274cbcc1369f7ba92ae21b81c3896d2 Mon Sep 17 00:00:00 2001 From: tomcodgen <191515280+tomcodgen@users.noreply.github.com> Date: Tue, 4 Mar 2025 01:01:52 +0000 Subject: [PATCH 3/5] Automated pre-commit update --- src/codegen/sdk/python/assignment.py | 2 +- .../unit/codegen/sdk/python/expressions/test_unpacking.py | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/codegen/sdk/python/assignment.py b/src/codegen/sdk/python/assignment.py index fadc08ab4..41d2498e0 100644 --- a/src/codegen/sdk/python/assignment.py +++ b/src/codegen/sdk/python/assignment.py @@ -155,7 +155,7 @@ def remove(self, delete_formatting: bool = True, priority: int = 0, dedupe: bool else: self.parent._values_scheduled_for_removal = [] else: - if name.source=="_": + if name.source == "_": logger.warning("Attempting to remove '_' in unpacking, command will be ignored if you wish to remove the statement, remove the other remaining variable(s)!") return transaction_count = self._active_transactions_on_assignment_names(TransactionPriority.Edit) diff --git a/tests/unit/codegen/sdk/python/expressions/test_unpacking.py b/tests/unit/codegen/sdk/python/expressions/test_unpacking.py index 875bfcf99..bda0c5302 100644 --- a/tests/unit/codegen/sdk/python/expressions/test_unpacking.py +++ b/tests/unit/codegen/sdk/python/expressions/test_unpacking.py @@ -162,8 +162,7 @@ def test_remove_unpacking_assignment_num(tmpdir) -> None: assert file2.source == """""" -def test_unpacking_function_with_underscore_removal(tmpdir:str) -> None: - +def test_unpacking_function_with_underscore_removal(tmpdir: str) -> None: # language=python content1 = """ args, _ = parser.parse_known_args() ##args gets deleted @@ -176,13 +175,11 @@ def test_unpacking_function_with_underscore_removal(tmpdir:str) -> None: "file1.py": content1, }, ) as codebase: - file1: SourceFile = codebase.get_file("file1.py") - for symbol in codebase.symbols: if not symbol.usages: symbol.remove() codebase.commit() - #The first TEST_BOOL Assigment gets removed when it should stay due to conditionality + # The first TEST_BOOL Assigment gets removed when it should stay due to conditionality assert len(file1.symbols) != 0 From f02fef0b3a5122b0553103b9b82ffbd730fcf4c1 Mon Sep 17 00:00:00 2001 From: tkucar Date: Tue, 4 Mar 2025 20:54:30 +0100 Subject: [PATCH 4/5] patch --- src/codegen/sdk/python/assignment.py | 2 +- .../unit/codegen/sdk/python/expressions/test_unpacking.py | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/codegen/sdk/python/assignment.py b/src/codegen/sdk/python/assignment.py index 41d2498e0..2614b6d43 100644 --- a/src/codegen/sdk/python/assignment.py +++ b/src/codegen/sdk/python/assignment.py @@ -156,7 +156,7 @@ def remove(self, delete_formatting: bool = True, priority: int = 0, dedupe: bool self.parent._values_scheduled_for_removal = [] else: if name.source == "_": - logger.warning("Attempting to remove '_' in unpacking, command will be ignored if you wish to remove the statement, remove the other remaining variable(s)!") + logger.warning("Attempting to remove '_' in unpacking, command will be ignored. If you wish to remove the statement, remove the other remaining variable(s)!") return transaction_count = self._active_transactions_on_assignment_names(TransactionPriority.Edit) throwaway = [asgnmt.name == "_" for asgnmt in self.parent.assignments].count(True) diff --git a/tests/unit/codegen/sdk/python/expressions/test_unpacking.py b/tests/unit/codegen/sdk/python/expressions/test_unpacking.py index bda0c5302..925da7fc2 100644 --- a/tests/unit/codegen/sdk/python/expressions/test_unpacking.py +++ b/tests/unit/codegen/sdk/python/expressions/test_unpacking.py @@ -1,4 +1,5 @@ from typing import TYPE_CHECKING +from unittest.mock import patch from codegen.sdk.codebase.factory.get_session import get_codebase_session @@ -161,8 +162,8 @@ def test_remove_unpacking_assignment_num(tmpdir) -> None: assert len(file2.symbols) == 0 assert file2.source == """""" - -def test_unpacking_function_with_underscore_removal(tmpdir: str) -> None: +@patch("codegen.sdk.python.assignment.logger") +def test_unpacking_function_with_underscore_removal(mock_logger,tmpdir: str) -> None: # language=python content1 = """ args, _ = parser.parse_known_args() ##args gets deleted @@ -181,5 +182,5 @@ def test_unpacking_function_with_underscore_removal(tmpdir: str) -> None: if not symbol.usages: symbol.remove() codebase.commit() - # The first TEST_BOOL Assigment gets removed when it should stay due to conditionality assert len(file1.symbols) != 0 + assert mock_logger.warning.call_count==1 From f4996a6b866b3daf3fcdfdb01514782b58bc9496 Mon Sep 17 00:00:00 2001 From: tomcodgen <191515280+tomcodgen@users.noreply.github.com> Date: Tue, 4 Mar 2025 19:55:54 +0000 Subject: [PATCH 5/5] Automated pre-commit update --- tests/unit/codegen/sdk/python/expressions/test_unpacking.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/unit/codegen/sdk/python/expressions/test_unpacking.py b/tests/unit/codegen/sdk/python/expressions/test_unpacking.py index 925da7fc2..274329bea 100644 --- a/tests/unit/codegen/sdk/python/expressions/test_unpacking.py +++ b/tests/unit/codegen/sdk/python/expressions/test_unpacking.py @@ -162,8 +162,9 @@ def test_remove_unpacking_assignment_num(tmpdir) -> None: assert len(file2.symbols) == 0 assert file2.source == """""" + @patch("codegen.sdk.python.assignment.logger") -def test_unpacking_function_with_underscore_removal(mock_logger,tmpdir: str) -> None: +def test_unpacking_function_with_underscore_removal(mock_logger, tmpdir: str) -> None: # language=python content1 = """ args, _ = parser.parse_known_args() ##args gets deleted @@ -183,4 +184,4 @@ def test_unpacking_function_with_underscore_removal(mock_logger,tmpdir: str) -> symbol.remove() codebase.commit() assert len(file1.symbols) != 0 - assert mock_logger.warning.call_count==1 + assert mock_logger.warning.call_count == 1