22
22
23
23
def f (a ) -> TypeGuard[str ]: ...
24
24
def g (a ) -> TypeIs[str ]: ...
25
-
26
25
def _ (a : object ):
27
26
reveal_type(f(a)) # revealed: TypeGuard[a, str]
28
27
reveal_type(g(a)) # revealed: TypeIs[a, str]
@@ -43,12 +42,14 @@ def _() -> TypeGuard[str]: ...
43
42
def _ (** kwargs ) -> TypeIs[str ]: ...
44
43
45
44
class _ :
45
+ # fine
46
46
def _ (self , / , a ) -> TypeGuard[str ]: ...
47
47
@ classmethod
48
48
def _ (cls , a ) -> TypeGuard[str ]: ...
49
49
@ staticmethod
50
50
def _ (a ) -> TypeIs[str ]: ...
51
51
52
+ # errors
52
53
def _ (self ) -> TypeGuard[str ]: ... # error: [invalid-type-guard-definition]
53
54
def _ (self , / , * , a ) -> TypeGuard[str ]: ... # error: [invalid-type-guard-definition]
54
55
@ classmethod
@@ -74,6 +75,7 @@ def _(a) -> TypeIs[str]: ...
74
75
75
76
# error: [invalid-type-guard-definition]
76
77
def _ (a : int ) -> TypeIs[str ]: ...
78
+
77
79
# error: [invalid-type-guard-definition]
78
80
def _ (a : bool | str ) -> TypeIs[int ]: ...
79
81
```
@@ -89,6 +91,7 @@ a = 123
89
91
90
92
# error: [invalid-type-form]
91
93
def f (_ ) -> TypeGuard[int , str ]: ...
94
+
92
95
# error: [invalid-type-form]
93
96
def g (_ ) -> TypeIs[a, str ]: ...
94
97
@@ -109,14 +112,14 @@ def f(a: object, flag: bool) -> TypeGuard[str]:
109
112
return 1
110
113
111
114
# TODO : Emit a diagnostic
112
- return ' '
115
+ return " "
113
116
114
- def g (a : Literal[' foo' , ' bar' ]) -> TypeIs[Literal[' foo' ]]:
117
+ def g (a : Literal[" foo" , " bar" ]) -> TypeIs[Literal[" foo" ]]:
115
118
match a:
116
- case ' foo' :
119
+ case " foo" :
117
120
# Logically wrong, but allowed regardless
118
121
return False
119
- case ' bar' :
122
+ case " bar" :
120
123
return False
121
124
case _:
122
125
assert_never(a)
@@ -130,10 +133,9 @@ from typing_extensions import TypeGuard, TypeIs
130
133
131
134
def f (a : object ) -> TypeGuard[str ]: ...
132
135
def g (a : object ) -> TypeIs[int ]: ...
133
-
134
136
def _ (d : Any):
135
137
if f(): # error: [missing-argument]
136
- ...
138
+ ...
137
139
138
140
# TODO : Is this error correct?
139
141
if g(* d): # error: [missing-argument]
@@ -143,7 +145,7 @@ def _(d: Any):
143
145
...
144
146
145
147
if g(a = d): # error: [invalid-type-guard-call]
146
- ...
148
+ ...
147
149
148
150
def _ (a : tuple[str , int ] | tuple[int , str ]):
149
151
if g(a[0 ]): # error: [invalid-type-guard-call]
@@ -159,7 +161,6 @@ from typing_extensions import TypeGuard, TypeIs
159
161
160
162
def guard_str (a : object ) -> TypeGuard[str ]: ...
161
163
def is_int (a : object ) -> TypeIs[int ]: ...
162
-
163
164
def _ (a : str | int ):
164
165
if guard_str(a):
165
166
reveal_type(a) # revealed: str
@@ -194,7 +195,7 @@ def _(x: str | int, flag: bool) -> None:
194
195
reveal_type(b) # revealed: TypeIs[x, int]
195
196
196
197
if flag:
197
- x = ' '
198
+ x = " "
198
199
199
200
if b:
200
201
reveal_type(x) # revealed: str | int
@@ -208,7 +209,6 @@ from typing_extensions import TypeGuard
208
209
209
210
def guard_int (a : object ) -> TypeGuard[int ]: ...
210
211
def is_int (a : object ) -> TypeGuard[int ]: ...
211
-
212
212
def does_not_narrow_in_negative_case (a : str | int ):
213
213
if not guard_int(a):
214
214
reveal_type(a) # revealed: str | int
0 commit comments