Skip to content

Commit 6a31479

Browse files
author
tkucar
committed
simplify py code
1 parent 7123cf9 commit 6a31479

File tree

3 files changed

+4
-67
lines changed

3 files changed

+4
-67
lines changed

src/codegen/sdk/python/file.py

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -190,43 +190,13 @@ def remove_unused_imports(self) -> None:
190190
- Future imports even if unused
191191
- Type hints and annotations
192192
"""
193-
# Track processed imports to avoid duplicates
194-
processed_imports = set()
195-
196-
# Group imports by module for more efficient processing
197-
module_imports = {}
198-
199-
# First pass - group imports by module
193+
# Process each import statement
200194
for import_stmt in self.imports:
201-
if import_stmt in processed_imports:
202-
continue
203-
204195
# Always preserve __future__ and star imports since we can't track their usage
205-
if import_stmt.is_future_import or import_stmt.is_star_import:
206-
continue
207-
208-
module = import_stmt.module_name
209-
if module not in module_imports:
210-
module_imports[module] = []
211-
module_imports[module].append(import_stmt)
212-
213-
# Second pass - process each module's imports
214-
for module, imports in module_imports.items():
215-
# Skip if any import from this module is used
216-
if any(imp.usages for imp in imports):
217-
# Remove individual unused imports if it's a from-style import
218-
if len(imports) > 1 and imports[0].is_from_import():
219-
for imp in imports:
220-
if not imp.usages and imp not in processed_imports:
221-
processed_imports.add(imp)
222-
imp.remove()
196+
if import_stmt.is_future_import or import_stmt.is_wildcard_import():
223197
continue
224198

225-
# If no imports from module are used, remove them all
226-
for imp in imports:
227-
if imp not in processed_imports:
228-
processed_imports.add(imp)
229-
imp.remove()
199+
import_stmt.remove_if_unused()
230200

231201
self.G.commit_transactions()
232202

src/codegen/sdk/python/import_resolution.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -319,21 +319,7 @@ def is_from_import(self) -> bool:
319319
Returns:
320320
bool: True if this is a from-style import, False otherwise.
321321
"""
322-
return self.ts_node.type == "import_from_statement"
323-
324-
@property
325-
def is_star_import(self) -> bool:
326-
"""Determines if this is a star import (from x import *).
327-
328-
Returns:
329-
bool: True if this is a star import, False otherwise
330-
"""
331-
if self.ts_node.type != "import_from_statement":
332-
return False
333-
334-
# Look for wildcard_import node among children
335-
wildcard_import = next((node for node in self.ts_node.children if node.type == "wildcard_import"), None)
336-
return wildcard_import is not None
322+
return self.import_type in [ImportType.NAMED_EXPORT, ImportType.WILDCARD]
337323

338324
@property
339325
def is_future_import(self) -> bool:

tests/unit/codegen/sdk/python/import_resolution/test_import_properties.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,6 @@ def test_is_from_import(tmpdir) -> None:
4848
assert imports[5].is_from_import()
4949

5050

51-
def test_is_star_import(tmpdir) -> None:
52-
# language=python
53-
content = """
54-
import module1
55-
from module2 import symbol
56-
from module3 import *
57-
from module4 import (a, b, c)
58-
"""
59-
with get_codebase_session(tmpdir=tmpdir, files={"test.py": content}) as codebase:
60-
file = codebase.get_file("test.py")
61-
imports = file.imports
62-
63-
# Only star import should return True
64-
assert not imports[0].is_star_import
65-
assert not imports[1].is_star_import
66-
assert imports[2].is_star_import
67-
assert not imports[3].is_star_import
68-
69-
7051
def test_is_future_import(tmpdir) -> None:
7152
# language=python
7253
content = """

0 commit comments

Comments
 (0)