Skip to content

Commit 8194e08

Browse files
hakancelikdevclaude
andcommitted
Fix subpackage import incorrectly removed when sibling subpackage is used (#180)
`import a.b` binds `a` in the namespace, so any `a.x.y` reference relies on that binding. `is_match_sub_packages` was comparing the import's root package against the full name string instead of just its first component, causing attribute usages like `urllib.parse.urlparse` to not match `import urllib.request`. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent f6cf9c5 commit 8194e08

13 files changed

+89
-1
lines changed

src/unimport/statement.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __len__(self) -> int:
2323
return len(self.name.split("."))
2424

2525
def is_match_sub_packages(self, name_name: str) -> bool:
26-
return self.name.split(".")[0] == name_name
26+
return self.name.split(".")[0] == name_name.split(".")[0]
2727

2828
@property
2929
def scope(self):
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import Union
2+
3+
from unimport.statement import Import, ImportFrom, Name
4+
5+
__all__ = ["NAMES", "IMPORTS", "UNUSED_IMPORTS"]
6+
7+
8+
NAMES: list[Name] = [
9+
Name(lineno=4, name="urllib.parse.urlparse", is_all=False),
10+
Name(lineno=4, name="urllib.parse", is_all=False),
11+
Name(lineno=4, name="url", is_all=False),
12+
]
13+
IMPORTS: list[Union[Import, ImportFrom]] = [
14+
Import(lineno=1, column=1, name="urllib.request", package="urllib.request"),
15+
]
16+
UNUSED_IMPORTS: list[Union[Import, ImportFrom]] = []
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import Union
2+
3+
from unimport.statement import Import, ImportFrom, Name
4+
5+
__all__ = ["NAMES", "IMPORTS", "UNUSED_IMPORTS"]
6+
7+
8+
NAMES: list[Name] = [
9+
Name(lineno=3, name="print", is_all=False),
10+
Name(lineno=3, name="os.path.join", is_all=False),
11+
Name(lineno=3, name="os.path", is_all=False),
12+
]
13+
IMPORTS: list[Union[Import, ImportFrom]] = [
14+
Import(lineno=1, column=1, name="os.path", package="os.path"),
15+
]
16+
UNUSED_IMPORTS: list[Union[Import, ImportFrom]] = []
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import Union
2+
3+
from unimport.statement import Import, ImportFrom, Name
4+
5+
__all__ = ["NAMES", "IMPORTS", "UNUSED_IMPORTS"]
6+
7+
8+
NAMES: list[Name] = [
9+
Name(lineno=3, name="print", is_all=False),
10+
Name(lineno=3, name="os.getcwd", is_all=False),
11+
]
12+
IMPORTS: list[Union[Import, ImportFrom]] = [
13+
Import(lineno=1, column=1, name="os.path", package="os.path"),
14+
]
15+
UNUSED_IMPORTS: list[Union[Import, ImportFrom]] = []
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import Union
2+
3+
from unimport.statement import Import, ImportFrom, Name
4+
5+
__all__ = ["NAMES", "IMPORTS", "UNUSED_IMPORTS"]
6+
7+
8+
NAMES: list[Name] = [
9+
Name(lineno=3, name="print", is_all=False),
10+
]
11+
IMPORTS: list[Union[Import, ImportFrom]] = [
12+
Import(lineno=1, column=1, name="urllib.request", package="urllib.request"),
13+
]
14+
UNUSED_IMPORTS: list[Union[Import, ImportFrom]] = [
15+
Import(lineno=1, column=1, name="urllib.request", package="urllib.request"),
16+
]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import urllib.request
2+
3+
def parse_url(url):
4+
return urllib.parse.urlparse(url)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import os.path
2+
3+
print(os.path.join("a", "b"))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import os.path
2+
3+
print(os.getcwd())
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
print("hello")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import urllib.request
2+
3+
def parse_url(url):
4+
return urllib.parse.urlparse(url)

0 commit comments

Comments
 (0)