Skip to content

Commit da385d5

Browse files
authored
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
1 parent e83d920 commit da385d5

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

pyflakes/test/test_type_annotations.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,3 +552,47 @@ def test_partial_string_annotations_with_future_annotations(self):
552552
def f() -> Optional['Queue[str]']:
553553
return None
554554
""")
555+
556+
def test_idomiatic_typing_guards(self):
557+
# typing.TYPE_CHECKING: python3.5.3+
558+
self.flakes("""
559+
from typing import TYPE_CHECKING
560+
561+
if TYPE_CHECKING:
562+
from t import T
563+
564+
def f(): # type: () -> T
565+
pass
566+
""")
567+
# False: the old, more-compatible approach
568+
self.flakes("""
569+
if False:
570+
from t import T
571+
572+
def f(): # type: () -> T
573+
pass
574+
""")
575+
# some choose to assign a constant and do it that way
576+
self.flakes("""
577+
MYPY = False
578+
579+
if MYPY:
580+
from t import T
581+
582+
def f(): # type: () -> T
583+
pass
584+
""")
585+
586+
def test_typing_guard_for_protocol(self):
587+
self.flakes("""
588+
from typing import TYPE_CHECKING
589+
590+
if TYPE_CHECKING:
591+
from typing import Protocol
592+
else:
593+
Protocol = object
594+
595+
class C(Protocol):
596+
def f(): # type: () -> int
597+
pass
598+
""")

0 commit comments

Comments
 (0)