Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4dfa817

Browse files
authoredApr 13, 2024
feat: improve Python import docs (#189)
1 parent be6d9b0 commit 4dfa817

File tree

4 files changed

+259
-132
lines changed

4 files changed

+259
-132
lines changed
 
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
This file contains some additional tests for Python imports.
2+
3+
```grit
4+
engine marzano(0.1)
5+
language python
6+
7+
contains bubble or {
8+
import_from(source="pydantic") => .,
9+
`$testlib.TestCase` where {
10+
$newtest = `newtest`,
11+
$testlib <: `unittest` => `$newtest`,
12+
$newtest <: ensure_import_from(source=`testing`),
13+
},
14+
`othermodule` as $other where {
15+
$other <: ensure_bare_import()
16+
},
17+
`$bob.caller` where {
18+
$newbob = `newbob`,
19+
$bob <: `thingbob` => `$newbob`,
20+
$newbob <: ensure_import_from(source=`ourlib.goodlib`),
21+
},
22+
`$badimport.remove_parent()` where {
23+
$badimport <: remove_import()
24+
}
25+
}
26+
```
27+
28+
29+
## ensure_import_from
30+
31+
```python
32+
import somewhere
33+
34+
unittest.TestCase()
35+
```
36+
37+
```python
38+
from testing import newtest
39+
40+
import somewhere
41+
42+
newtest.TestCase()
43+
```
44+
45+
## Ensure no duplicate imports
46+
47+
```python
48+
from testing import newtest, unittest
49+
50+
unittest.TestCase()
51+
```
52+
53+
```python
54+
from testing import newtest, unittest
55+
56+
newtest.TestCase()
57+
```
58+
59+
## Ensure we don't append to the same import
60+
61+
```python
62+
from testing import unittest, newtest
63+
64+
unittest.TestCase()
65+
```
66+
67+
```python
68+
from testing import unittest, newtest
69+
70+
newtest.TestCase()
71+
```
72+
73+
## Ensure we handle nested modules correctly
74+
75+
```python
76+
from ourlib.goodlib import thingbob, newbob
77+
78+
newbob.caller()
79+
80+
thingbob.caller()
81+
```
82+
83+
```python
84+
from ourlib.goodlib import thingbob, newbob
85+
86+
newbob.caller()
87+
88+
newbob.caller()
89+
```
90+
91+
## Add a bare import
92+
93+
```python
94+
othermodule.TestCase()
95+
```
96+
97+
```python
98+
import othermodule
99+
100+
othermodule.TestCase()
101+
```
102+
103+
## Do not add duplicate bare imports
104+
105+
```python
106+
import othermodule
107+
108+
othermodule.TestCase()
109+
```
110+
111+
```python
112+
import othermodule
113+
114+
othermodule.TestCase()
115+
```
116+
117+
## Remove imports - base case
118+
119+
Grit can handle removing single imports from packages, and the entire package if no imports are left.
120+
121+
```python
122+
from somewhere import somelib
123+
from elsewhere import foolib, keeplib
124+
from otherlib import keepthis
125+
126+
somelib.remove_parent()
127+
foolib.remove_parent()
128+
129+
```
130+
131+
```python
132+
from elsewhere import keeplib
133+
from otherlib import keepthis
134+
135+
somelib.remove_parent()
136+
foolib.remove_parent()
137+
```
138+
139+
## Remove imports - complex cases
140+
141+
```python
142+
import entirelib
143+
import elselib as hiddenlib
144+
import secretlib as aliasedlib
145+
from complicated_alias import coolstuff as badlib
146+
# Keep this, even though it *looks* like it could be related
147+
from confusing_lib import somelib as otherlib
148+
149+
entirelib.remove_parent()
150+
aliasedlib.remove_parent()
151+
badlib.remove_parent()
152+
hiddenlib.keep_parent()
153+
154+
```
155+
156+
```python
157+
import elselib as hiddenlib
158+
# Keep this, even though it *looks* like it could be related
159+
from confusing_lib import somelib as otherlib
160+
161+
entirelib.remove_parent()
162+
aliasedlib.remove_parent()
163+
badlib.remove_parent()
164+
hiddenlib.keep_parent()
165+
166+
```
167+
168+
## Remove multiple imports from the same package
169+
170+
```python
171+
from elsewhere import foolib, badlib
172+
from otherlib import keepthis
173+
174+
keepthis.keep_parent()
175+
foolib.remove_parent()
176+
badlib.remove_parent()
177+
178+
```
179+
180+
```python
181+
from otherlib import keepthis
182+
183+
keepthis.keep_parent()
184+
foolib.remove_parent()
185+
badlib.remove_parent()
186+
187+
```
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Please sign in to comment.