-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve Python import docs (#189)
- Loading branch information
Showing
4 changed files
with
259 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
This file contains some additional tests for Python imports. | ||
|
||
```grit | ||
engine marzano(0.1) | ||
language python | ||
contains bubble or { | ||
import_from(source="pydantic") => ., | ||
`$testlib.TestCase` where { | ||
$newtest = `newtest`, | ||
$testlib <: `unittest` => `$newtest`, | ||
$newtest <: ensure_import_from(source=`testing`), | ||
}, | ||
`othermodule` as $other where { | ||
$other <: ensure_bare_import() | ||
}, | ||
`$bob.caller` where { | ||
$newbob = `newbob`, | ||
$bob <: `thingbob` => `$newbob`, | ||
$newbob <: ensure_import_from(source=`ourlib.goodlib`), | ||
}, | ||
`$badimport.remove_parent()` where { | ||
$badimport <: remove_import() | ||
} | ||
} | ||
``` | ||
|
||
|
||
## ensure_import_from | ||
|
||
```python | ||
import somewhere | ||
|
||
unittest.TestCase() | ||
``` | ||
|
||
```python | ||
from testing import newtest | ||
|
||
import somewhere | ||
|
||
newtest.TestCase() | ||
``` | ||
|
||
## Ensure no duplicate imports | ||
|
||
```python | ||
from testing import newtest, unittest | ||
|
||
unittest.TestCase() | ||
``` | ||
|
||
```python | ||
from testing import newtest, unittest | ||
|
||
newtest.TestCase() | ||
``` | ||
|
||
## Ensure we don't append to the same import | ||
|
||
```python | ||
from testing import unittest, newtest | ||
|
||
unittest.TestCase() | ||
``` | ||
|
||
```python | ||
from testing import unittest, newtest | ||
|
||
newtest.TestCase() | ||
``` | ||
|
||
## Ensure we handle nested modules correctly | ||
|
||
```python | ||
from ourlib.goodlib import thingbob, newbob | ||
|
||
newbob.caller() | ||
|
||
thingbob.caller() | ||
``` | ||
|
||
```python | ||
from ourlib.goodlib import thingbob, newbob | ||
|
||
newbob.caller() | ||
|
||
newbob.caller() | ||
``` | ||
|
||
## Add a bare import | ||
|
||
```python | ||
othermodule.TestCase() | ||
``` | ||
|
||
```python | ||
import othermodule | ||
|
||
othermodule.TestCase() | ||
``` | ||
|
||
## Do not add duplicate bare imports | ||
|
||
```python | ||
import othermodule | ||
|
||
othermodule.TestCase() | ||
``` | ||
|
||
```python | ||
import othermodule | ||
|
||
othermodule.TestCase() | ||
``` | ||
|
||
## Remove imports - base case | ||
|
||
Grit can handle removing single imports from packages, and the entire package if no imports are left. | ||
|
||
```python | ||
from somewhere import somelib | ||
from elsewhere import foolib, keeplib | ||
from otherlib import keepthis | ||
|
||
somelib.remove_parent() | ||
foolib.remove_parent() | ||
|
||
``` | ||
|
||
```python | ||
from elsewhere import keeplib | ||
from otherlib import keepthis | ||
|
||
somelib.remove_parent() | ||
foolib.remove_parent() | ||
``` | ||
|
||
## Remove imports - complex cases | ||
|
||
```python | ||
import entirelib | ||
import elselib as hiddenlib | ||
import secretlib as aliasedlib | ||
from complicated_alias import coolstuff as badlib | ||
# Keep this, even though it *looks* like it could be related | ||
from confusing_lib import somelib as otherlib | ||
|
||
entirelib.remove_parent() | ||
aliasedlib.remove_parent() | ||
badlib.remove_parent() | ||
hiddenlib.keep_parent() | ||
|
||
``` | ||
|
||
```python | ||
import elselib as hiddenlib | ||
# Keep this, even though it *looks* like it could be related | ||
from confusing_lib import somelib as otherlib | ||
|
||
entirelib.remove_parent() | ||
aliasedlib.remove_parent() | ||
badlib.remove_parent() | ||
hiddenlib.keep_parent() | ||
|
||
``` | ||
|
||
## Remove multiple imports from the same package | ||
|
||
```python | ||
from elsewhere import foolib, badlib | ||
from otherlib import keepthis | ||
|
||
keepthis.keep_parent() | ||
foolib.remove_parent() | ||
badlib.remove_parent() | ||
|
||
``` | ||
|
||
```python | ||
from otherlib import keepthis | ||
|
||
keepthis.keep_parent() | ||
foolib.remove_parent() | ||
badlib.remove_parent() | ||
|
||
``` |
Oops, something went wrong.