Repository: asottile/flake8-typing-imports

Issue:
False positive with some.package.typing.SomeType
Types imported from a package path that ends with `.typing` (not just the top-level `typing`) result in false positives.

```
$ find
.
./x
./x/typing.py
./x/__init__.py

$ cat x/typing.py 
ExampleType = str

$ cat x/__init__.py 
from .typing import ExampleType


def foo(ex: ExampleType):
    pass

$ flake8 x
x/__init__.py:1:1: TYP001 guard import by `if False:  # TYPE_CHECKING`: ExampleType (not in 3.5.0, 3.5.1, 3.5.2, 3.5.3, 3.5.4, 3.5.5, 3.5.6, 3.5.7, 3.6.0, 3.6.1, 3.6.2, 3.6.3, 3.6.4, 3.6.5, 3.6.6, 3.6.7, 3.6.8, 3.6.9, 3.7.0, 3.7.1, 3.7.2, 3.7.3, 3.7.4)
```

If `ExampleType` here is imported from somewhere else, e.g. `x.blah`, no such problem.

Discussion:

Patch:
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index d9f7ae5..6c8a1a3 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -1,6 +1,6 @@
 repos:
 -   repo: https://github.com/pre-commit/pre-commit-hooks
-    rev: v2.2.3
+    rev: v2.4.0
     hooks:
     -   id: trailing-whitespace
     -   id: end-of-file-fixer
@@ -11,30 +11,30 @@ repos:
     -   id: double-quote-string-fixer
     -   id: name-tests-test
 -   repo: https://gitlab.com/pycqa/flake8
-    rev: 3.7.7
+    rev: 3.7.9
     hooks:
     -   id: flake8
         additional_dependencies: [flake8-typing-imports==1.5.0]
 -   repo: https://github.com/pre-commit/mirrors-autopep8
-    rev: v1.4.4
+    rev: v1.5
     hooks:
     -   id: autopep8
 -   repo: https://github.com/asottile/reorder_python_imports
-    rev: v1.4.0
+    rev: v1.9.0
     hooks:
     -   id: reorder-python-imports
         args: [--py3-plus]
 -   repo: https://github.com/asottile/add-trailing-comma
-    rev: v1.0.0
+    rev: v1.5.0
     hooks:
     -   id: add-trailing-comma
         args: [--py36-plus]
 -   repo: https://github.com/asottile/pyupgrade
-    rev: v1.17.1
+    rev: v1.26.2
     hooks:
     -   id: pyupgrade
         args: [--py36-plus]
 -   repo: https://github.com/pre-commit/mirrors-mypy
-    rev: v0.701
+    rev: v0.761
     hooks:
     -   id: mypy
diff --git a/flake8_typing_imports.py b/flake8_typing_imports.py
index 6271ffb..3d00f8c 100644
--- a/flake8_typing_imports.py
+++ b/flake8_typing_imports.py
@@ -464,7 +464,7 @@ class Visitor(ast.NodeVisitor):
         )
 
     def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
-        if node.module == 'typing' and self._level == 0:
+        if node.level == 0 and node.module == 'typing' and self._level == 0:
             for name in node.names:
                 self.imports[name.name].append((node.lineno, node.col_offset))
                 if not name.asname:


Test patch:
diff --git a/tests/flake8_typing_imports_test.py b/tests/flake8_typing_imports_test.py
index 294dbcb..2902861 100644
--- a/tests/flake8_typing_imports_test.py
+++ b/tests/flake8_typing_imports_test.py
@@ -100,6 +100,11 @@ def test_guard_by_type_checking():
         assert ret == set()
 
 
+def test_relative_typing_module():
+    ret = results('from .typing import wat')
+    assert ret == set()
+
+
 def test_missing_overload_define():
     with version_ctx(Version(3, 5, 0)):
         ret = results('from typing import overload')