-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement typing-only import detection (TYP001, TYP002, TYP003) (#2147)
- Loading branch information
1 parent
9eb13bc
commit 63b4f60
Showing
15 changed files
with
551 additions
and
31 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
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,28 @@ | ||
"""Tests to determine first-party import classification. | ||
For typing-only import detection tests, see `TYP002.py`. | ||
""" | ||
|
||
|
||
def f(): | ||
import TYP001 | ||
|
||
x: TYP001 | ||
|
||
|
||
def f(): | ||
import TYP001 | ||
|
||
print(TYP001) | ||
|
||
|
||
def f(): | ||
from . import TYP001 | ||
|
||
x: TYP001 | ||
|
||
|
||
def f(): | ||
from . import TYP001 | ||
|
||
print(TYP001) |
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,148 @@ | ||
"""Tests to determine accurate detection of typing-only imports.""" | ||
|
||
|
||
def f(): | ||
import pandas as pd # TYP002 | ||
|
||
x: pd.DataFrame | ||
|
||
|
||
def f(): | ||
from pandas import DataFrame # TYP002 | ||
|
||
x: DataFrame | ||
|
||
|
||
def f(): | ||
from pandas import DataFrame as df # TYP002 | ||
|
||
x: df | ||
|
||
|
||
def f(): | ||
import pandas as pd # TYP002 | ||
|
||
x: pd.DataFrame = 1 | ||
|
||
|
||
def f(): | ||
from pandas import DataFrame # TYP002 | ||
|
||
x: DataFrame = 2 | ||
|
||
|
||
def f(): | ||
from pandas import DataFrame as df # TYP002 | ||
|
||
x: df = 3 | ||
|
||
|
||
def f(): | ||
import pandas as pd # TYP002 | ||
|
||
x: "pd.DataFrame" = 1 | ||
|
||
|
||
def f(): | ||
import pandas as pd | ||
|
||
x = dict["pd.DataFrame", "pd.DataFrame"] # TYP002 | ||
|
||
|
||
def f(): | ||
import pandas as pd | ||
|
||
print(pd) | ||
|
||
|
||
def f(): | ||
from pandas import DataFrame | ||
|
||
print(DataFrame) | ||
|
||
|
||
def f(): | ||
from pandas import DataFrame | ||
|
||
def f(): | ||
print(DataFrame) | ||
|
||
|
||
def f(): | ||
from typing import Dict, Any | ||
|
||
def example() -> Any: | ||
return 1 | ||
|
||
x: Dict[int] = 20 | ||
|
||
|
||
def f(): | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from typing import Dict | ||
x: Dict[int] = 20 | ||
|
||
|
||
def f(): | ||
from pathlib import Path | ||
|
||
class ImportVisitor(ast.NodeTransformer): | ||
def __init__(self, cwd: Path) -> None: | ||
self.cwd = cwd | ||
origin = Path(spec.origin) | ||
|
||
class ExampleClass: | ||
def __init__(self): | ||
self.cwd = Path(pandas.getcwd()) | ||
|
||
|
||
def f(): | ||
import pandas | ||
|
||
class Migration: | ||
enum = pandas | ||
|
||
|
||
def f(): | ||
import pandas | ||
|
||
class Migration: | ||
enum = pandas.EnumClass | ||
|
||
|
||
def f(): | ||
from typing import TYPE_CHECKING | ||
|
||
from pandas import y | ||
|
||
if TYPE_CHECKING: | ||
_type = x | ||
else: | ||
_type = y | ||
|
||
|
||
def f(): | ||
from typing import TYPE_CHECKING | ||
|
||
from pandas import y | ||
|
||
if TYPE_CHECKING: | ||
_type = x | ||
elif True: | ||
_type = y | ||
|
||
|
||
def f(): | ||
from typing import cast | ||
|
||
import pandas as pd | ||
|
||
x = cast(pd.DataFrame, 2) | ||
|
||
|
||
def f(): | ||
import pandas as pd | ||
|
||
x = dict[pd.DataFrame, pd.DataFrame] |
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,16 @@ | ||
"""Tests to determine standard library import classification. | ||
For typing-only import detection tests, see `TYP002.py`. | ||
""" | ||
|
||
|
||
def f(): | ||
import os | ||
|
||
x: os | ||
|
||
|
||
def f(): | ||
import os | ||
|
||
print(os) |
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 |
---|---|---|
|
@@ -1784,6 +1784,9 @@ | |
"TYP", | ||
"TYP0", | ||
"TYP00", | ||
"TYP001", | ||
"TYP002", | ||
"TYP003", | ||
"TYP004", | ||
"TYP005", | ||
"UP", | ||
|
Oops, something went wrong.