Skip to content

Commit

Permalink
TYP001
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jan 25, 2023
1 parent 0758049 commit 942602a
Show file tree
Hide file tree
Showing 15 changed files with 551 additions and 31 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,9 @@ For more, see [flake8-type-checking](https://pypi.org/project/flake8-type-checki

| Code | Name | Message | Fix |
| ---- | ---- | ------- | --- |
| TYP001 | typing-only-first-party-import | Move application import `{}` into a type-checking block | |
| TYP002 | typing-only-third-party-import | Move third-party import `{}` into a type-checking block | |
| TYP003 | typing-only-standard-library-import | Move standard library import `{}` into a type-checking block | |
| TYP004 | runtime-import-in-type-checking-block | Move import `{}` out of type-checking block. Import is used for more than type hinting. | |
| TYP005 | empty-type-checking-block | Found empty type-checking block | |

Expand Down
28 changes: 28 additions & 0 deletions resources/test/fixtures/flake8_type_checking/TYP001.py
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)
148 changes: 148 additions & 0 deletions resources/test/fixtures/flake8_type_checking/TYP002.py
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]
16 changes: 16 additions & 0 deletions resources/test/fixtures/flake8_type_checking/TYP003.py
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)
3 changes: 3 additions & 0 deletions ruff.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,9 @@
"TYP",
"TYP0",
"TYP00",
"TYP001",
"TYP002",
"TYP003",
"TYP004",
"TYP005",
"UP",
Expand Down

0 comments on commit 942602a

Please sign in to comment.