Skip to content

Commit d313da1

Browse files
Add star import dedup behavior to supported-behaviors docs (#320)
1 parent d1670b2 commit d313da1

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

docs/tutorial/supported-behaviors.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,81 @@ __all__.extend(["walk"])
113113

114114
---
115115

116+
## Star Import
117+
118+
When used with `--include-star-import`, unimport can refactor star imports into explicit
119+
imports by detecting which names are actually used in the code.
120+
121+
**input**
122+
123+
```python
124+
from os import *
125+
from json import *
126+
127+
print(getcwd())
128+
print(JSONEncoder)
129+
```
130+
131+
**output**
132+
133+
```python
134+
from os import getcwd
135+
from json import JSONEncoder
136+
137+
print(getcwd())
138+
print(JSONEncoder)
139+
```
140+
141+
#### Duplicate Name Resolution
142+
143+
When multiple star imports export the same name, unimport deduplicates suggestions so
144+
that only the last import provides each name (matching Python's shadowing semantics).
145+
This produces correct output in a single pass.
146+
147+
**input**
148+
149+
```python
150+
from _collections import *
151+
from collections import *
152+
153+
print(defaultdict)
154+
```
155+
156+
**output**
157+
158+
```python
159+
from collections import defaultdict
160+
161+
print(defaultdict)
162+
```
163+
164+
If the star imports partially overlap, each import keeps only its unique names:
165+
166+
**input**
167+
168+
```python
169+
from collections import *
170+
from _collections import *
171+
172+
print(Counter)
173+
print(defaultdict)
174+
```
175+
176+
**output**
177+
178+
```python
179+
from collections import Counter
180+
from _collections import defaultdict
181+
182+
print(Counter)
183+
print(defaultdict)
184+
```
185+
186+
Star import suggestions also respect explicit imports — if a name already has an
187+
explicit import, star imports won't produce a duplicate for that name.
188+
189+
---
190+
116191
## Scope
117192

118193
Unimport tries to better understand whether the import is unused by performing scope

0 commit comments

Comments
 (0)