Skip to content
Merged
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
6 changes: 6 additions & 0 deletions src/codegen/sdk/python/assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,6 +21,8 @@
from codegen.sdk.core.node_id_factory import NodeId
from codegen.sdk.python.statements.assignment_statement import PyAssignmentStatement

logger = get_logger(__name__)


@py_apidoc
class PyAssignment(Assignment["PyAssignmentStatement"], PySymbol):
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/codegen/sdk/python/expressions/test_unpacking.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from typing import TYPE_CHECKING
from unittest.mock import patch

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
Expand Down Expand Up @@ -155,3 +161,27 @@ 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:
# 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()
assert len(file1.symbols) != 0
assert mock_logger.warning.call_count == 1
Loading