@@ -37,8 +37,8 @@ reveal_type(foo) # N: Revealed type is "def (a: builtins.object) -> TypeGuard[b
37
37
[case testTypeGuardCallArgsNone]
38
38
from typing_extensions import TypeGuard
39
39
class Point: pass
40
- # TODO: error on the 'def' line (insufficient args for type guard)
41
- def is_point() -> TypeGuard[Point]: pass
40
+
41
+ def is_point() -> TypeGuard[Point]: pass # E: TypeGuard functions must have a positional argument
42
42
def main(a: object) -> None:
43
43
if is_point():
44
44
reveal_type(a) # N: Revealed type is "builtins.object"
@@ -227,13 +227,13 @@ def main(a: object) -> None:
227
227
from typing_extensions import TypeGuard
228
228
def is_float(a: object, b: object = 0) -> TypeGuard[float]: pass
229
229
def main1(a: object) -> None:
230
- # This is debatable -- should we support these cases?
230
+ if is_float(a=a, b=1):
231
+ reveal_type(a) # N: Revealed type is "builtins.float"
231
232
232
- if is_float(a=a, b=1): # E: Type guard requires positional argument
233
- reveal_type(a) # N: Revealed type is "builtins.object "
233
+ if is_float(b=1, a=a):
234
+ reveal_type(a) # N: Revealed type is "builtins.float "
234
235
235
- if is_float(b=1, a=a): # E: Type guard requires positional argument
236
- reveal_type(a) # N: Revealed type is "builtins.object"
236
+ # This is debatable -- should we support these cases?
237
237
238
238
ta = (a,)
239
239
if is_float(*ta): # E: Type guard requires positional argument
@@ -597,3 +597,77 @@ def func(names: Tuple[str, ...]):
597
597
if is_two_element_tuple(names):
598
598
reveal_type(names) # N: Revealed type is "Tuple[builtins.str, builtins.str]"
599
599
[builtins fixtures/tuple.pyi]
600
+
601
+ [case testTypeGuardErroneousDefinitionFails]
602
+ from typing_extensions import TypeGuard
603
+
604
+ class Z:
605
+ def typeguard(self, *, x: object) -> TypeGuard[int]: # E: TypeGuard functions must have a positional argument
606
+ ...
607
+
608
+ def bad_typeguard(*, x: object) -> TypeGuard[int]: # E: TypeGuard functions must have a positional argument
609
+ ...
610
+ [builtins fixtures/tuple.pyi]
611
+
612
+ [case testTypeGuardWithKeywordArg]
613
+ from typing_extensions import TypeGuard
614
+
615
+ class Z:
616
+ def typeguard(self, x: object) -> TypeGuard[int]:
617
+ ...
618
+
619
+ def typeguard(x: object) -> TypeGuard[int]:
620
+ ...
621
+
622
+ n: object
623
+ if typeguard(x=n):
624
+ reveal_type(n) # N: Revealed type is "builtins.int"
625
+
626
+ if Z().typeguard(x=n):
627
+ reveal_type(n) # N: Revealed type is "builtins.int"
628
+ [builtins fixtures/tuple.pyi]
629
+
630
+ [case testStaticMethodTypeGuard]
631
+ from typing_extensions import TypeGuard
632
+
633
+ class Y:
634
+ @staticmethod
635
+ def typeguard(h: object) -> TypeGuard[int]:
636
+ ...
637
+
638
+ x: object
639
+ if Y().typeguard(x):
640
+ reveal_type(x) # N: Revealed type is "builtins.int"
641
+ if Y.typeguard(x):
642
+ reveal_type(x) # N: Revealed type is "builtins.int"
643
+ [builtins fixtures/tuple.pyi]
644
+ [builtins fixtures/classmethod.pyi]
645
+
646
+ [case testTypeGuardKwargFollowingThroughOverloaded]
647
+ from typing import overload, Union
648
+ from typing_extensions import TypeGuard
649
+
650
+ @overload
651
+ def typeguard(x: object, y: str) -> TypeGuard[str]:
652
+ ...
653
+
654
+ @overload
655
+ def typeguard(x: object, y: int) -> TypeGuard[int]:
656
+ ...
657
+
658
+ def typeguard(x: object, y: Union[int, str]) -> Union[TypeGuard[int], TypeGuard[str]]:
659
+ ...
660
+
661
+ x: object
662
+ if typeguard(x=x, y=42):
663
+ reveal_type(x) # N: Revealed type is "builtins.int"
664
+
665
+ if typeguard(y=42, x=x):
666
+ reveal_type(x) # N: Revealed type is "builtins.int"
667
+
668
+ if typeguard(x=x, y="42"):
669
+ reveal_type(x) # N: Revealed type is "builtins.str"
670
+
671
+ if typeguard(y="42", x=x):
672
+ reveal_type(x) # N: Revealed type is "builtins.str"
673
+ [builtins fixtures/tuple.pyi]
0 commit comments