summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Sottile <asottile@umich.edu>2020-04-27 00:45:05 -0700
committerGitHub <noreply@github.com>2020-04-27 00:45:05 -0700
commitda385d5d65c6f515ae0ca88338b0a2e7a5c19c9c (patch)
tree29007dec8788daa42cf21f41bc4dacb5481d94df
parente83d920aafa92df9bda7dad606d401dda1ba722d (diff)
downloadpyflakes-da385d5d65c6f515ae0ca88338b0a2e7a5c19c9c.tar.gz
Add tests demonstrating common use of TYPE_CHECKING guards (#531)
* Add tests demonstrating common use of TYPE_CHECKING guards * Add another example using a runtime guard for Protocol
-rw-r--r--pyflakes/test/test_type_annotations.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/pyflakes/test/test_type_annotations.py b/pyflakes/test/test_type_annotations.py
index ed28127..abe5473 100644
--- a/pyflakes/test/test_type_annotations.py
+++ b/pyflakes/test/test_type_annotations.py
@@ -552,3 +552,47 @@ class TestTypeAnnotations(TestCase):
def f() -> Optional['Queue[str]']:
return None
""")
+
+ def test_idomiatic_typing_guards(self):
+ # typing.TYPE_CHECKING: python3.5.3+
+ self.flakes("""
+ from typing import TYPE_CHECKING
+
+ if TYPE_CHECKING:
+ from t import T
+
+ def f(): # type: () -> T
+ pass
+ """)
+ # False: the old, more-compatible approach
+ self.flakes("""
+ if False:
+ from t import T
+
+ def f(): # type: () -> T
+ pass
+ """)
+ # some choose to assign a constant and do it that way
+ self.flakes("""
+ MYPY = False
+
+ if MYPY:
+ from t import T
+
+ def f(): # type: () -> T
+ pass
+ """)
+
+ def test_typing_guard_for_protocol(self):
+ self.flakes("""
+ from typing import TYPE_CHECKING
+
+ if TYPE_CHECKING:
+ from typing import Protocol
+ else:
+ Protocol = object
+
+ class C(Protocol):
+ def f(): # type: () -> int
+ pass
+ """)