| 1 |
[case testTypeIsBasic] |
| 2 |
from typing_extensions import TypeIs |
| 3 |
class Point: pass |
| 4 |
def is_point(a: object) -> TypeIs[Point]: pass |
| 5 |
def main(a: object) -> None: |
| 6 |
if is_point(a): |
| 7 |
reveal_type(a) # N: Revealed type is "__main__.Point" |
| 8 |
else: |
| 9 |
reveal_type(a) # N: Revealed type is "builtins.object" |
| 10 |
[builtins fixtures/tuple.pyi] |
| 11 |
|
| 12 |
[case testTypeIsElif] |
| 13 |
from typing_extensions import TypeIs |
| 14 |
from typing import Union |
| 15 |
class Point: pass |
| 16 |
def is_point(a: object) -> TypeIs[Point]: pass |
| 17 |
class Line: pass |
| 18 |
def is_line(a: object) -> TypeIs[Line]: pass |
| 19 |
def main(a: Union[Point, Line, int]) -> None: |
| 20 |
if is_point(a): |
| 21 |
reveal_type(a) # N: Revealed type is "__main__.Point" |
| 22 |
elif is_line(a): |
| 23 |
reveal_type(a) # N: Revealed type is "__main__.Line" |
| 24 |
else: |
| 25 |
reveal_type(a) # N: Revealed type is "builtins.int" |
| 26 |
|
| 27 |
[builtins fixtures/tuple.pyi] |
| 28 |
|
| 29 |
[case testTypeIsTypeArgsNone] |
| 30 |
from typing_extensions import TypeIs |
| 31 |
def foo(a: object) -> TypeIs: # E: TypeIs must have exactly one type argument |
| 32 |
pass |
| 33 |
[builtins fixtures/tuple.pyi] |
| 34 |
|
| 35 |
[case testTypeIsTypeArgsTooMany] |
| 36 |
from typing_extensions import TypeIs |
| 37 |
def foo(a: object) -> TypeIs[int, int]: # E: TypeIs must have exactly one type argument |
| 38 |
pass |
| 39 |
[builtins fixtures/tuple.pyi] |
| 40 |
|
| 41 |
[case testTypeIsTypeArgType] |
| 42 |
from typing_extensions import TypeIs |
| 43 |
def foo(a: object) -> TypeIs[42]: # E: Invalid type: try using Literal[42] instead? |
| 44 |
pass |
| 45 |
[builtins fixtures/tuple.pyi] |
| 46 |
|
| 47 |
[case testTypeIsRepr] |
| 48 |
from typing_extensions import TypeIs |
| 49 |
def foo(a: object) -> TypeIs[int]: |
| 50 |
pass |
| 51 |
reveal_type(foo) # N: Revealed type is "def (a: builtins.object) -> TypeIs[builtins.int]" |
| 52 |
[builtins fixtures/tuple.pyi] |
| 53 |
|
| 54 |
[case testTypeIsCallArgsNone] |
| 55 |
from typing_extensions import TypeIs |
| 56 |
class Point: pass |
| 57 |
|
| 58 |
def is_point() -> TypeIs[Point]: pass # E: "TypeIs" functions must have a positional argument |
| 59 |
def main(a: object) -> None: |
| 60 |
if is_point(): |
| 61 |
reveal_type(a) # N: Revealed type is "builtins.object" |
| 62 |
[builtins fixtures/tuple.pyi] |
| 63 |
|
| 64 |
[case testTypeIsCallArgsMultiple] |
| 65 |
from typing_extensions import TypeIs |
| 66 |
class Point: pass |
| 67 |
def is_point(a: object, b: object) -> TypeIs[Point]: pass |
| 68 |
def main(a: object, b: object) -> None: |
| 69 |
if is_point(a, b): |
| 70 |
reveal_type(a) # N: Revealed type is "__main__.Point" |
| 71 |
reveal_type(b) # N: Revealed type is "builtins.object" |
| 72 |
[builtins fixtures/tuple.pyi] |
| 73 |
|
| 74 |
[case testTypeIsIsBool] |
| 75 |
from typing_extensions import TypeIs |
| 76 |
def f(a: TypeIs[int]) -> None: pass |
| 77 |
reveal_type(f) # N: Revealed type is "def (a: builtins.bool)" |
| 78 |
a: TypeIs[int] |
| 79 |
reveal_type(a) # N: Revealed type is "builtins.bool" |
| 80 |
class C: |
| 81 |
a: TypeIs[int] |
| 82 |
reveal_type(C().a) # N: Revealed type is "builtins.bool" |
| 83 |
[builtins fixtures/tuple.pyi] |
| 84 |
|
| 85 |
[case testTypeIsWithTypeVar] |
| 86 |
from typing import TypeVar, Tuple, Type |
| 87 |
from typing_extensions import TypeIs |
| 88 |
T = TypeVar('T') |
| 89 |
def is_tuple_of_type(a: Tuple[object, ...], typ: Type[T]) -> TypeIs[Tuple[T, ...]]: pass |
| 90 |
def main(a: Tuple[object, ...]): |
| 91 |
if is_tuple_of_type(a, int): |
| 92 |
reveal_type(a) # N: Revealed type is "builtins.tuple[builtins.int, ...]" |
| 93 |
[builtins fixtures/tuple.pyi] |
| 94 |
|
| 95 |
[case testTypeIsTypeVarReturn] |
| 96 |
from typing import Callable, Optional, TypeVar |
| 97 |
from typing_extensions import TypeIs |
| 98 |
T = TypeVar('T') |
| 99 |
def is_str(x: object) -> TypeIs[str]: pass |
| 100 |
def main(x: object, type_check_func: Callable[[object], TypeIs[T]]) -> T: |
| 101 |
if not type_check_func(x): |
| 102 |
raise Exception() |
| 103 |
return x |
| 104 |
reveal_type(main("a", is_str)) # N: Revealed type is "builtins.str" |
| 105 |
[builtins fixtures/exception.pyi] |
| 106 |
|
| 107 |
[case testTypeIsPassedAsTypeVarIsBool] |
| 108 |
from typing import Callable, TypeVar |
| 109 |
from typing_extensions import TypeIs |
| 110 |
T = TypeVar('T') |
| 111 |
def is_str(x: object) -> TypeIs[str]: pass |
| 112 |
def main(f: Callable[[object], T]) -> T: pass |
| 113 |
reveal_type(main(is_str)) # N: Revealed type is "builtins.bool" |
| 114 |
[builtins fixtures/tuple.pyi] |
| 115 |
|
| 116 |
[case testTypeIsUnionIn] |
| 117 |
from typing import Union |
| 118 |
from typing_extensions import TypeIs |
| 119 |
def is_foo(a: Union[int, str]) -> TypeIs[str]: pass |
| 120 |
def main(a: Union[str, int]) -> None: |
| 121 |
if is_foo(a): |
| 122 |
reveal_type(a) # N: Revealed type is "builtins.str" |
| 123 |
else: |
| 124 |
reveal_type(a) # N: Revealed type is "builtins.int" |
| 125 |
reveal_type(a) # N: Revealed type is "builtins.str | builtins.int" |
| 126 |
[builtins fixtures/tuple.pyi] |
| 127 |
|
| 128 |
[case testTypeIsUnionOut] |
| 129 |
from typing import Union |
| 130 |
from typing_extensions import TypeIs |
| 131 |
def is_foo(a: object) -> TypeIs[Union[int, str]]: pass |
| 132 |
def main(a: object) -> None: |
| 133 |
if is_foo(a): |
| 134 |
reveal_type(a) # N: Revealed type is "builtins.int | builtins.str" |
| 135 |
[builtins fixtures/tuple.pyi] |
| 136 |
|
| 137 |
[case testTypeIsUnionWithGeneric] |
| 138 |
from typing import Any, List, Sequence, Union |
| 139 |
from typing_extensions import TypeIs |
| 140 |
|
| 141 |
def is_int_list(a: object) -> TypeIs[List[int]]: pass |
| 142 |
def is_int_seq(a: object) -> TypeIs[Sequence[int]]: pass |
| 143 |
def is_seq(a: object) -> TypeIs[Sequence[Any]]: pass |
| 144 |
|
| 145 |
def f1(a: Union[List[int], List[str]]) -> None: |
| 146 |
if is_int_list(a): |
| 147 |
reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]" |
| 148 |
else: |
| 149 |
reveal_type(a) # N: Revealed type is "builtins.list[builtins.str]" |
| 150 |
reveal_type(a) # N: Revealed type is "builtins.list[builtins.int] | builtins.list[builtins.str]" |
| 151 |
|
| 152 |
def f2(a: Union[List[int], int]) -> None: |
| 153 |
if is_int_list(a): |
| 154 |
reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]" |
| 155 |
else: |
| 156 |
reveal_type(a) # N: Revealed type is "builtins.int" |
| 157 |
reveal_type(a) # N: Revealed type is "builtins.list[builtins.int] | builtins.int" |
| 158 |
|
| 159 |
def f3(a: Union[List[bool], List[str]]) -> None: |
| 160 |
if is_int_seq(a): |
| 161 |
reveal_type(a) # N: Revealed type is "builtins.list[builtins.bool]" |
| 162 |
else: |
| 163 |
reveal_type(a) # N: Revealed type is "builtins.list[builtins.str]" |
| 164 |
reveal_type(a) # N: Revealed type is "builtins.list[builtins.bool] | builtins.list[builtins.str]" |
| 165 |
|
| 166 |
def f4(a: Union[List[int], int]) -> None: |
| 167 |
if is_seq(a): |
| 168 |
reveal_type(a) # N: Revealed type is "builtins.list[builtins.int]" |
| 169 |
else: |
| 170 |
reveal_type(a) # N: Revealed type is "builtins.int" |
| 171 |
reveal_type(a) # N: Revealed type is "builtins.list[builtins.int] | builtins.int" |
| 172 |
[builtins fixtures/tuple.pyi] |
| 173 |
|
| 174 |
[case testTypeIsTupleGeneric] |
| 175 |
# flags: --warn-unreachable |
| 176 |
from __future__ import annotations |
| 177 |
from typing_extensions import TypeIs, Unpack |
| 178 |
|
| 179 |
class A: ... |
| 180 |
class B: ... |
| 181 |
|
| 182 |
def is_tuple_of_B(v: tuple[A | B, ...]) -> TypeIs[tuple[B, ...]]: ... |
| 183 |
|
| 184 |
def test1(t: tuple[A]) -> None: |
| 185 |
if is_tuple_of_B(t): |
| 186 |
reveal_type(t) # E: Statement is unreachable |
| 187 |
else: |
| 188 |
reveal_type(t) # N: Revealed type is "tuple[__main__.A]" |
| 189 |
|
| 190 |
def test2(t: tuple[B, A]) -> None: |
| 191 |
if is_tuple_of_B(t): |
| 192 |
reveal_type(t) # E: Statement is unreachable |
| 193 |
else: |
| 194 |
reveal_type(t) # N: Revealed type is "tuple[__main__.B, __main__.A]" |
| 195 |
|
| 196 |
def test3(t: tuple[A | B]) -> None: |
| 197 |
if is_tuple_of_B(t): |
| 198 |
reveal_type(t) # N: Revealed type is "tuple[__main__.B]" |
| 199 |
else: |
| 200 |
reveal_type(t) # N: Revealed type is "tuple[__main__.A | __main__.B]" |
| 201 |
|
| 202 |
def test4(t: tuple[A | B, A | B]) -> None: |
| 203 |
if is_tuple_of_B(t): |
| 204 |
reveal_type(t) # N: Revealed type is "tuple[__main__.B, __main__.B]" |
| 205 |
else: |
| 206 |
reveal_type(t) # N: Revealed type is "tuple[__main__.A | __main__.B, __main__.A | __main__.B]" |
| 207 |
|
| 208 |
def test5(t: tuple[A | B, ...]) -> None: |
| 209 |
if is_tuple_of_B(t): |
| 210 |
reveal_type(t) # N: Revealed type is "builtins.tuple[__main__.B, ...]" |
| 211 |
else: |
| 212 |
reveal_type(t) # N: Revealed type is "builtins.tuple[__main__.A | __main__.B, ...]" |
| 213 |
|
| 214 |
def test6(t: tuple[B, Unpack[tuple[A | B, ...]], B]) -> None: |
| 215 |
if is_tuple_of_B(t): |
| 216 |
reveal_type(t) # N: Revealed type is "tuple[__main__.B, Unpack[builtins.tuple[__main__.B, ...]], __main__.B]" |
| 217 |
else: |
| 218 |
reveal_type(t) # N: Revealed type is "tuple[__main__.B, Unpack[builtins.tuple[__main__.A | __main__.B, ...]], __main__.B]" |
| 219 |
[builtins fixtures/tuple.pyi] |
| 220 |
|
| 221 |
[case testTypeIsNonzeroFloat] |
| 222 |
from typing_extensions import TypeIs |
| 223 |
def is_nonzero(a: object) -> TypeIs[float]: pass |
| 224 |
def main(a: int): |
| 225 |
if is_nonzero(a): |
| 226 |
reveal_type(a) # N: Revealed type is "builtins.int" |
| 227 |
[builtins fixtures/tuple.pyi] |
| 228 |
|
| 229 |
[case testTypeIsHigherOrder] |
| 230 |
from typing import Callable, TypeVar, Iterable, List |
| 231 |
from typing_extensions import TypeIs |
| 232 |
T = TypeVar('T') |
| 233 |
R = TypeVar('R') |
| 234 |
def filter(f: Callable[[T], TypeIs[R]], it: Iterable[T]) -> Iterable[R]: pass |
| 235 |
def is_float(a: object) -> TypeIs[float]: pass |
| 236 |
a: List[object] = ["a", 0, 0.0] |
| 237 |
b = filter(is_float, a) |
| 238 |
reveal_type(b) # N: Revealed type is "typing.Iterable[builtins.float]" |
| 239 |
[builtins fixtures/tuple.pyi] |
| 240 |
|
| 241 |
[case testTypeIsMethod] |
| 242 |
from typing_extensions import TypeIs |
| 243 |
class C: |
| 244 |
def main(self, a: object) -> None: |
| 245 |
if self.is_float(a): |
| 246 |
reveal_type(self) # N: Revealed type is "__main__.C" |
| 247 |
reveal_type(a) # N: Revealed type is "builtins.float" |
| 248 |
def is_float(self, a: object) -> TypeIs[float]: pass |
| 249 |
[builtins fixtures/tuple.pyi] |
| 250 |
|
| 251 |
[case testTypeIsCrossModule] |
| 252 |
import guard |
| 253 |
from points import Point |
| 254 |
def main(a: object) -> None: |
| 255 |
if guard.is_point(a): |
| 256 |
reveal_type(a) # N: Revealed type is "points.Point" |
| 257 |
[file guard.py] |
| 258 |
from typing_extensions import TypeIs |
| 259 |
import points |
| 260 |
def is_point(a: object) -> TypeIs[points.Point]: pass |
| 261 |
[file points.py] |
| 262 |
class Point: pass |
| 263 |
[builtins fixtures/tuple.pyi] |
| 264 |
|
| 265 |
[case testTypeIsBodyRequiresBool] |
| 266 |
from typing_extensions import TypeIs |
| 267 |
def is_float(a: object) -> TypeIs[float]: |
| 268 |
return "not a bool" # E: Incompatible return value type (got "str", expected "bool") |
| 269 |
[builtins fixtures/tuple.pyi] |
| 270 |
|
| 271 |
[case testTypeIsNarrowToTypedDict] |
| 272 |
from typing import Mapping, TypedDict |
| 273 |
from typing_extensions import TypeIs |
| 274 |
class User(TypedDict): |
| 275 |
name: str |
| 276 |
id: int |
| 277 |
def is_user(a: Mapping[str, object]) -> TypeIs[User]: |
| 278 |
return isinstance(a.get("name"), str) and isinstance(a.get("id"), int) |
| 279 |
def main(a: Mapping[str, object]) -> None: |
| 280 |
if is_user(a): |
| 281 |
reveal_type(a) # N: Revealed type is "TypedDict('__main__.User', {'name': builtins.str, 'id': builtins.int})" |
| 282 |
[builtins fixtures/dict.pyi] |
| 283 |
[typing fixtures/typing-typeddict.pyi] |
| 284 |
|
| 285 |
[case testTypeIsInAssert] |
| 286 |
from typing_extensions import TypeIs |
| 287 |
def is_float(a: object) -> TypeIs[float]: pass |
| 288 |
def main(a: object) -> None: |
| 289 |
assert is_float(a) |
| 290 |
reveal_type(a) # N: Revealed type is "builtins.float" |
| 291 |
[builtins fixtures/tuple.pyi] |
| 292 |
|
| 293 |
[case testTypeIsFromAny] |
| 294 |
from typing import Any |
| 295 |
from typing_extensions import TypeIs |
| 296 |
def is_objfloat(a: object) -> TypeIs[float]: pass |
| 297 |
def is_anyfloat(a: Any) -> TypeIs[float]: pass |
| 298 |
def objmain(a: object) -> None: |
| 299 |
if is_objfloat(a): |
| 300 |
reveal_type(a) # N: Revealed type is "builtins.float" |
| 301 |
if is_anyfloat(a): |
| 302 |
reveal_type(a) # N: Revealed type is "builtins.float" |
| 303 |
def anymain(a: Any) -> None: |
| 304 |
if is_objfloat(a): |
| 305 |
reveal_type(a) # N: Revealed type is "builtins.float" |
| 306 |
if is_anyfloat(a): |
| 307 |
reveal_type(a) # N: Revealed type is "builtins.float" |
| 308 |
[builtins fixtures/tuple.pyi] |
| 309 |
|
| 310 |
[case testTypeIsNegatedAndElse] |
| 311 |
from typing import Union |
| 312 |
from typing_extensions import TypeIs |
| 313 |
def is_int(a: object) -> TypeIs[int]: pass |
| 314 |
def is_str(a: object) -> TypeIs[str]: pass |
| 315 |
def intmain(a: Union[int, str]) -> None: |
| 316 |
if not is_int(a): |
| 317 |
reveal_type(a) # N: Revealed type is "builtins.str" |
| 318 |
else: |
| 319 |
reveal_type(a) # N: Revealed type is "builtins.int" |
| 320 |
def strmain(a: Union[int, str]) -> None: |
| 321 |
if is_str(a): |
| 322 |
reveal_type(a) # N: Revealed type is "builtins.str" |
| 323 |
else: |
| 324 |
reveal_type(a) # N: Revealed type is "builtins.int" |
| 325 |
[builtins fixtures/tuple.pyi] |
| 326 |
|
| 327 |
[case testTypeIsClassMethod] |
| 328 |
from typing_extensions import TypeIs |
| 329 |
class C: |
| 330 |
@classmethod |
| 331 |
def is_float(cls, a: object) -> TypeIs[float]: pass |
| 332 |
def method(self, a: object) -> None: |
| 333 |
if self.is_float(a): |
| 334 |
reveal_type(a) # N: Revealed type is "builtins.float" |
| 335 |
def main(a: object) -> None: |
| 336 |
if C.is_float(a): |
| 337 |
reveal_type(a) # N: Revealed type is "builtins.float" |
| 338 |
[builtins fixtures/classmethod.pyi] |
| 339 |
|
| 340 |
[case testTypeIsRequiresPositionalArgs] |
| 341 |
from typing_extensions import TypeIs |
| 342 |
def is_float(a: object, b: object = 0) -> TypeIs[float]: pass |
| 343 |
def main1(a: object) -> None: |
| 344 |
if is_float(a=a, b=1): |
| 345 |
reveal_type(a) # N: Revealed type is "builtins.float" |
| 346 |
|
| 347 |
if is_float(b=1, a=a): |
| 348 |
reveal_type(a) # N: Revealed type is "builtins.float" |
| 349 |
|
| 350 |
[builtins fixtures/tuple.pyi] |
| 351 |
|
| 352 |
[case testTypeIsOverload] |
| 353 |
from typing import overload, Any, Callable, Iterable, Iterator, List, Optional, TypeVar |
| 354 |
from typing_extensions import TypeIs |
| 355 |
|
| 356 |
T = TypeVar("T") |
| 357 |
R = TypeVar("R") |
| 358 |
|
| 359 |
@overload |
| 360 |
def filter(f: Callable[[T], TypeIs[R]], it: Iterable[T]) -> Iterator[R]: ... |
| 361 |
@overload |
| 362 |
def filter(f: Callable[[T], bool], it: Iterable[T]) -> Iterator[T]: ... |
| 363 |
def filter(*args): pass |
| 364 |
|
| 365 |
def is_int_typeis(a: object) -> TypeIs[int]: pass |
| 366 |
def is_int_bool(a: object) -> bool: pass |
| 367 |
|
| 368 |
def main(a: List[Optional[int]]) -> None: |
| 369 |
bb = filter(lambda x: x is not None, a) |
| 370 |
reveal_type(bb) # N: Revealed type is "typing.Iterator[builtins.int | None]" |
| 371 |
# Also, if you replace 'bool' with 'Any' in the second overload, bb is Iterator[Any] |
| 372 |
cc = filter(is_int_typeis, a) |
| 373 |
reveal_type(cc) # N: Revealed type is "typing.Iterator[builtins.int]" |
| 374 |
dd = filter(is_int_bool, a) |
| 375 |
reveal_type(dd) # N: Revealed type is "typing.Iterator[builtins.int | None]" |
| 376 |
|
| 377 |
[builtins fixtures/tuple.pyi] |
| 378 |
[typing fixtures/typing-full.pyi] |
| 379 |
|
| 380 |
[case testTypeIsDecorated] |
| 381 |
from typing import TypeVar |
| 382 |
from typing_extensions import TypeIs |
| 383 |
T = TypeVar("T") |
| 384 |
def decorator(f: T) -> T: pass |
| 385 |
@decorator |
| 386 |
def is_float(a: object) -> TypeIs[float]: |
| 387 |
pass |
| 388 |
def main(a: object) -> None: |
| 389 |
if is_float(a): |
| 390 |
reveal_type(a) # N: Revealed type is "builtins.float" |
| 391 |
[builtins fixtures/tuple.pyi] |
| 392 |
|
| 393 |
[case testTypeIsMethodOverride] |
| 394 |
from typing_extensions import TypeIs |
| 395 |
class C: |
| 396 |
def is_float(self, a: object) -> TypeIs[float]: pass |
| 397 |
class D(C): |
| 398 |
def is_float(self, a: object) -> bool: pass # Fail |
| 399 |
[builtins fixtures/tuple.pyi] |
| 400 |
[out] |
| 401 |
main:5: error: Signature of "is_float" incompatible with supertype "C" |
| 402 |
main:5: note: Superclass: |
| 403 |
main:5: note: def is_float(self, a: object) -> TypeIs[float] |
| 404 |
main:5: note: Subclass: |
| 405 |
main:5: note: def is_float(self, a: object) -> bool |
| 406 |
|
| 407 |
[case testTypeIsInAnd] |
| 408 |
from typing import Any |
| 409 |
from typing_extensions import TypeIs |
| 410 |
def isclass(a: object) -> bool: |
| 411 |
pass |
| 412 |
def isfloat(a: object) -> TypeIs[float]: |
| 413 |
pass |
| 414 |
def isstr(a: object) -> TypeIs[str]: |
| 415 |
pass |
| 416 |
|
| 417 |
def coverage1(obj: Any) -> bool: |
| 418 |
if isfloat(obj) and obj.__self__ is not None and isclass(obj.__self__): # E: "float" has no attribute "__self__" |
| 419 |
reveal_type(obj) # N: Revealed type is "builtins.float" |
| 420 |
return True |
| 421 |
reveal_type(obj) # N: Revealed type is "Any" |
| 422 |
return False |
| 423 |
|
| 424 |
def coverage2(obj: Any) -> bool: |
| 425 |
if not (isfloat(obj) or isstr(obj)): |
| 426 |
reveal_type(obj) # N: Revealed type is "Any" |
| 427 |
return True |
| 428 |
reveal_type(obj) # N: Revealed type is "builtins.float | builtins.str" |
| 429 |
return False |
| 430 |
[builtins fixtures/classmethod.pyi] |
| 431 |
|
| 432 |
[case testAssignToTypeIsedVariable1] |
| 433 |
from typing_extensions import TypeIs |
| 434 |
|
| 435 |
class A: pass |
| 436 |
class B(A): pass |
| 437 |
|
| 438 |
def guard(a: A) -> TypeIs[B]: |
| 439 |
pass |
| 440 |
|
| 441 |
a = A() |
| 442 |
if not guard(a): |
| 443 |
a = A() |
| 444 |
[builtins fixtures/tuple.pyi] |
| 445 |
|
| 446 |
[case testAssignToTypeIsedVariable2] |
| 447 |
from typing_extensions import TypeIs |
| 448 |
|
| 449 |
class A: pass |
| 450 |
class B: pass |
| 451 |
|
| 452 |
def guard(a: object) -> TypeIs[B]: |
| 453 |
pass |
| 454 |
|
| 455 |
a = A() |
| 456 |
if not guard(a): |
| 457 |
a = A() |
| 458 |
[builtins fixtures/tuple.pyi] |
| 459 |
|
| 460 |
[case testAssignToTypeIsedVariable3] |
| 461 |
from typing_extensions import TypeIs |
| 462 |
|
| 463 |
class A: pass |
| 464 |
class B: pass |
| 465 |
|
| 466 |
def guard(a: object) -> TypeIs[B]: |
| 467 |
pass |
| 468 |
|
| 469 |
a = A() |
| 470 |
if guard(a): |
| 471 |
reveal_type(a) # N: Revealed type is "__main__.<subclass of "__main__.A" and "__main__.B">" |
| 472 |
a = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A") |
| 473 |
reveal_type(a) # N: Revealed type is "__main__.<subclass of "__main__.A" and "__main__.B">" |
| 474 |
a = A() |
| 475 |
reveal_type(a) # N: Revealed type is "__main__.A" |
| 476 |
reveal_type(a) # N: Revealed type is "__main__.A" |
| 477 |
[builtins fixtures/tuple.pyi] |
| 478 |
|
| 479 |
[case testTypeIsNestedRestrictionAny] |
| 480 |
from typing_extensions import TypeIs |
| 481 |
from typing import Any |
| 482 |
|
| 483 |
class A: ... |
| 484 |
def f(x: object) -> TypeIs[A]: ... |
| 485 |
def g(x: object) -> None: ... |
| 486 |
|
| 487 |
def test(x: Any) -> None: |
| 488 |
if not(f(x) or x): |
| 489 |
return |
| 490 |
g(reveal_type(x)) # N: Revealed type is "__main__.A | Any" |
| 491 |
[builtins fixtures/tuple.pyi] |
| 492 |
|
| 493 |
[case testTypeIsNestedRestrictionUnionOther] |
| 494 |
from typing_extensions import TypeIs |
| 495 |
from typing import Any |
| 496 |
|
| 497 |
class A: ... |
| 498 |
class B: ... |
| 499 |
def f(x: object) -> TypeIs[A]: ... |
| 500 |
def f2(x: object) -> TypeIs[B]: ... |
| 501 |
def g(x: object) -> None: ... |
| 502 |
|
| 503 |
def test(x: object) -> None: |
| 504 |
if not(f(x) or f2(x)): |
| 505 |
return |
| 506 |
g(reveal_type(x)) # N: Revealed type is "__main__.A | __main__.B" |
| 507 |
[builtins fixtures/tuple.pyi] |
| 508 |
|
| 509 |
[case testTypeIsComprehensionSubtype] |
| 510 |
from typing import List |
| 511 |
from typing_extensions import TypeIs |
| 512 |
|
| 513 |
class Base: ... |
| 514 |
class Foo(Base): ... |
| 515 |
class Bar(Base): ... |
| 516 |
|
| 517 |
def is_foo(item: object) -> TypeIs[Foo]: |
| 518 |
return isinstance(item, Foo) |
| 519 |
|
| 520 |
def is_bar(item: object) -> TypeIs[Bar]: |
| 521 |
return isinstance(item, Bar) |
| 522 |
|
| 523 |
def foobar(items: List[object]): |
| 524 |
a: List[Base] = [x for x in items if is_foo(x) or is_bar(x)] |
| 525 |
b: List[Base] = [x for x in items if is_foo(x)] |
| 526 |
c: List[Foo] = [x for x in items if is_foo(x)] |
| 527 |
d: List[Bar] = [x for x in items if is_foo(x)] # E: List comprehension has incompatible type List[Foo]; expected List[Bar] |
| 528 |
[builtins fixtures/tuple.pyi] |
| 529 |
|
| 530 |
[case testTypeIsNestedRestrictionUnionIsInstance] |
| 531 |
from typing_extensions import TypeIs |
| 532 |
from typing import Any, List |
| 533 |
|
| 534 |
class A: ... |
| 535 |
def f(x: List[Any]) -> TypeIs[List[str]]: ... |
| 536 |
def g(x: object) -> None: ... |
| 537 |
|
| 538 |
def test(x: List[Any]) -> None: |
| 539 |
if not(f(x) or isinstance(x, A)): |
| 540 |
return |
| 541 |
g(reveal_type(x)) # N: Revealed type is "builtins.list[builtins.str] | __main__.<subclass of "builtins.list[Any]" and "__main__.A">" |
| 542 |
[builtins fixtures/tuple.pyi] |
| 543 |
|
| 544 |
[case testTypeIsMultipleCondition] |
| 545 |
from typing_extensions import TypeIs |
| 546 |
from typing import Any, List |
| 547 |
|
| 548 |
class Foo: ... |
| 549 |
class Bar: ... |
| 550 |
|
| 551 |
def is_foo(item: object) -> TypeIs[Foo]: |
| 552 |
return isinstance(item, Foo) |
| 553 |
|
| 554 |
def is_bar(item: object) -> TypeIs[Bar]: |
| 555 |
return isinstance(item, Bar) |
| 556 |
|
| 557 |
def foobar(x: object): |
| 558 |
if not isinstance(x, Foo) or not isinstance(x, Bar): |
| 559 |
return |
| 560 |
reveal_type(x) # N: Revealed type is "__main__.<subclass of "__main__.Foo" and "__main__.Bar">" |
| 561 |
|
| 562 |
def foobar_typeis(x: object): |
| 563 |
if not is_foo(x) or not is_bar(x): |
| 564 |
return |
| 565 |
# Looks like a typo but this is what our unique name generation produces |
| 566 |
reveal_type(x) # N: Revealed type is "__main__.<subclass of "__main__.Foo" and "__main__.Bar">" |
| 567 |
[builtins fixtures/tuple.pyi] |
| 568 |
|
| 569 |
[case testTypeIsAsFunctionArgAsBoolSubtype] |
| 570 |
from typing import Callable |
| 571 |
from typing_extensions import TypeIs |
| 572 |
|
| 573 |
def accepts_bool(f: Callable[[object], bool]): pass |
| 574 |
|
| 575 |
def with_bool_typeis(o: object) -> TypeIs[bool]: pass |
| 576 |
def with_str_typeis(o: object) -> TypeIs[str]: pass |
| 577 |
def with_bool(o: object) -> bool: pass |
| 578 |
|
| 579 |
accepts_bool(with_bool_typeis) |
| 580 |
accepts_bool(with_str_typeis) |
| 581 |
accepts_bool(with_bool) |
| 582 |
[builtins fixtures/tuple.pyi] |
| 583 |
|
| 584 |
[case testTypeIsAsFunctionArg] |
| 585 |
from typing import Callable |
| 586 |
from typing_extensions import TypeIs |
| 587 |
|
| 588 |
def accepts_typeis(f: Callable[[object], TypeIs[bool]]): pass |
| 589 |
def different_typeis(f: Callable[[object], TypeIs[str]]): pass |
| 590 |
|
| 591 |
def with_typeis(o: object) -> TypeIs[bool]: pass |
| 592 |
def with_bool(o: object) -> bool: pass |
| 593 |
|
| 594 |
accepts_typeis(with_typeis) |
| 595 |
accepts_typeis(with_bool) # E: Argument 1 to "accepts_typeis" has incompatible type "Callable[[object], bool]"; expected "Callable[[object], TypeIs[bool]]" |
| 596 |
|
| 597 |
different_typeis(with_typeis) # E: Argument 1 to "different_typeis" has incompatible type "Callable[[object], TypeIs[bool]]"; expected "Callable[[object], TypeIs[str]]" |
| 598 |
different_typeis(with_bool) # E: Argument 1 to "different_typeis" has incompatible type "Callable[[object], bool]"; expected "Callable[[object], TypeIs[str]]" |
| 599 |
[builtins fixtures/tuple.pyi] |
| 600 |
|
| 601 |
[case testTypeIsAsGenericFunctionArg] |
| 602 |
from typing import Callable, TypeVar |
| 603 |
from typing_extensions import TypeIs |
| 604 |
|
| 605 |
T = TypeVar('T') |
| 606 |
|
| 607 |
def accepts_typeis(f: Callable[[object], TypeIs[T]]): pass |
| 608 |
|
| 609 |
def with_bool_typeis(o: object) -> TypeIs[bool]: pass |
| 610 |
def with_str_typeis(o: object) -> TypeIs[str]: pass |
| 611 |
def with_bool(o: object) -> bool: pass |
| 612 |
|
| 613 |
accepts_typeis(with_bool_typeis) |
| 614 |
accepts_typeis(with_str_typeis) |
| 615 |
accepts_typeis(with_bool) # E: Argument 1 to "accepts_typeis" has incompatible type "Callable[[object], bool]"; expected "Callable[[object], TypeIs[Never]]" |
| 616 |
[builtins fixtures/tuple.pyi] |
| 617 |
|
| 618 |
[case testTypeIsAsOverloadedFunctionArg] |
| 619 |
# https://github.com/python/mypy/issues/11307 |
| 620 |
from typing import Callable, TypeVar, Generic, Any, overload |
| 621 |
from typing_extensions import TypeIs |
| 622 |
|
| 623 |
_T = TypeVar('_T') |
| 624 |
|
| 625 |
class filter(Generic[_T]): |
| 626 |
@overload |
| 627 |
def __init__(self, function: Callable[[object], TypeIs[_T]]) -> None: pass |
| 628 |
@overload |
| 629 |
def __init__(self, function: Callable[[_T], Any]) -> None: pass |
| 630 |
def __init__(self, function): pass |
| 631 |
|
| 632 |
def is_int_typeis(a: object) -> TypeIs[int]: pass |
| 633 |
def returns_bool(a: object) -> bool: pass |
| 634 |
|
| 635 |
reveal_type(filter(is_int_typeis)) # N: Revealed type is "__main__.filter[builtins.int]" |
| 636 |
reveal_type(filter(returns_bool)) # N: Revealed type is "__main__.filter[builtins.object]" |
| 637 |
[builtins fixtures/tuple.pyi] |
| 638 |
|
| 639 |
[case testTypeIsSubtypingVariance] |
| 640 |
from typing import Callable |
| 641 |
from typing_extensions import TypeIs |
| 642 |
|
| 643 |
class A: pass |
| 644 |
class B(A): pass |
| 645 |
class C(B): pass |
| 646 |
|
| 647 |
def accepts_typeis(f: Callable[[object], TypeIs[B]]): pass |
| 648 |
|
| 649 |
def with_typeis_a(o: object) -> TypeIs[A]: pass |
| 650 |
def with_typeis_b(o: object) -> TypeIs[B]: pass |
| 651 |
def with_typeis_c(o: object) -> TypeIs[C]: pass |
| 652 |
|
| 653 |
accepts_typeis(with_typeis_a) # E: Argument 1 to "accepts_typeis" has incompatible type "Callable[[object], TypeIs[A]]"; expected "Callable[[object], TypeIs[B]]" |
| 654 |
accepts_typeis(with_typeis_b) |
| 655 |
accepts_typeis(with_typeis_c) # E: Argument 1 to "accepts_typeis" has incompatible type "Callable[[object], TypeIs[C]]"; expected "Callable[[object], TypeIs[B]]" |
| 656 |
[builtins fixtures/tuple.pyi] |
| 657 |
|
| 658 |
[case testTypeIsWithIdentityGeneric] |
| 659 |
from typing import TypeVar |
| 660 |
from typing_extensions import TypeIs |
| 661 |
|
| 662 |
_T = TypeVar("_T") |
| 663 |
|
| 664 |
def identity(val: _T) -> TypeIs[_T]: |
| 665 |
pass |
| 666 |
|
| 667 |
def func1(name: _T): |
| 668 |
reveal_type(name) # N: Revealed type is "_T`-1" |
| 669 |
if identity(name): |
| 670 |
reveal_type(name) # N: Revealed type is "_T`-1" |
| 671 |
|
| 672 |
def func2(name: str): |
| 673 |
reveal_type(name) # N: Revealed type is "builtins.str" |
| 674 |
if identity(name): |
| 675 |
reveal_type(name) # N: Revealed type is "builtins.str" |
| 676 |
[builtins fixtures/tuple.pyi] |
| 677 |
|
| 678 |
[case testTypeIsWithGenericOnSecondParam] |
| 679 |
from typing import TypeVar |
| 680 |
from typing_extensions import TypeIs |
| 681 |
|
| 682 |
_R = TypeVar("_R") |
| 683 |
|
| 684 |
def guard(val: object, param: _R) -> TypeIs[_R]: |
| 685 |
pass |
| 686 |
|
| 687 |
def func1(name: object): |
| 688 |
reveal_type(name) # N: Revealed type is "builtins.object" |
| 689 |
if guard(name, name): |
| 690 |
reveal_type(name) # N: Revealed type is "builtins.object" |
| 691 |
if guard(name, 1): |
| 692 |
reveal_type(name) # N: Revealed type is "builtins.int" |
| 693 |
|
| 694 |
def func2(name: int): |
| 695 |
reveal_type(name) # N: Revealed type is "builtins.int" |
| 696 |
if guard(name, True): |
| 697 |
reveal_type(name) # N: Revealed type is "builtins.bool" |
| 698 |
[builtins fixtures/tuple.pyi] |
| 699 |
|
| 700 |
[case testTypeIsWithGenericInstance] |
| 701 |
from typing import TypeVar, List, Iterable |
| 702 |
from typing_extensions import TypeIs |
| 703 |
|
| 704 |
_T = TypeVar("_T") |
| 705 |
|
| 706 |
def is_list_of_str(val: Iterable[_T]) -> TypeIs[List[_T]]: |
| 707 |
pass |
| 708 |
|
| 709 |
def func(name: Iterable[str]): |
| 710 |
reveal_type(name) # N: Revealed type is "typing.Iterable[builtins.str]" |
| 711 |
if is_list_of_str(name): |
| 712 |
reveal_type(name) # N: Revealed type is "builtins.list[builtins.str]" |
| 713 |
[builtins fixtures/tuple.pyi] |
| 714 |
|
| 715 |
[case testTypeIsWithTupleGeneric] |
| 716 |
from typing import TypeVar, Tuple |
| 717 |
from typing_extensions import TypeIs |
| 718 |
|
| 719 |
_T = TypeVar("_T") |
| 720 |
|
| 721 |
def is_two_element_tuple(val: Tuple[_T, ...]) -> TypeIs[Tuple[_T, _T]]: |
| 722 |
pass |
| 723 |
|
| 724 |
def func(names: Tuple[str, ...]): |
| 725 |
reveal_type(names) # N: Revealed type is "builtins.tuple[builtins.str, ...]" |
| 726 |
if is_two_element_tuple(names): |
| 727 |
reveal_type(names) # N: Revealed type is "tuple[builtins.str, builtins.str]" |
| 728 |
[builtins fixtures/tuple.pyi] |
| 729 |
|
| 730 |
[case testTypeIsErroneousDefinitionFails] |
| 731 |
from typing_extensions import TypeIs |
| 732 |
|
| 733 |
class Z: |
| 734 |
def typeis1(self, *, x: object) -> TypeIs[int]: # E: "TypeIs" functions must have a positional argument |
| 735 |
... |
| 736 |
|
| 737 |
@staticmethod |
| 738 |
def typeis2(x: object) -> TypeIs[int]: |
| 739 |
... |
| 740 |
|
| 741 |
@staticmethod |
| 742 |
def typeis3(*, x: object) -> TypeIs[int]: # E: "TypeIs" functions must have a positional argument |
| 743 |
... |
| 744 |
|
| 745 |
def bad_typeis(*, x: object) -> TypeIs[int]: # E: "TypeIs" functions must have a positional argument |
| 746 |
... |
| 747 |
|
| 748 |
[builtins fixtures/classmethod.pyi] |
| 749 |
|
| 750 |
[case testTypeIsWithKeywordArg] |
| 751 |
from typing_extensions import TypeIs |
| 752 |
|
| 753 |
class Z: |
| 754 |
def typeis(self, x: object) -> TypeIs[int]: |
| 755 |
... |
| 756 |
|
| 757 |
def typeis(x: object) -> TypeIs[int]: |
| 758 |
... |
| 759 |
|
| 760 |
n: object |
| 761 |
if typeis(x=n): |
| 762 |
reveal_type(n) # N: Revealed type is "builtins.int" |
| 763 |
|
| 764 |
if Z().typeis(x=n): |
| 765 |
reveal_type(n) # N: Revealed type is "builtins.int" |
| 766 |
[builtins fixtures/tuple.pyi] |
| 767 |
|
| 768 |
[case testStaticMethodTypeIs] |
| 769 |
from typing_extensions import TypeIs |
| 770 |
|
| 771 |
class Y: |
| 772 |
@staticmethod |
| 773 |
def typeis(h: object) -> TypeIs[int]: |
| 774 |
... |
| 775 |
|
| 776 |
x: object |
| 777 |
if Y().typeis(x): |
| 778 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 779 |
if Y.typeis(x): |
| 780 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 781 |
[builtins fixtures/classmethod.pyi] |
| 782 |
|
| 783 |
[case testTypeIsKwargFollowingThroughOverloaded] |
| 784 |
from typing import overload, Union |
| 785 |
from typing_extensions import TypeIs |
| 786 |
|
| 787 |
@overload |
| 788 |
def typeis(x: object, y: str) -> TypeIs[str]: |
| 789 |
... |
| 790 |
|
| 791 |
@overload |
| 792 |
def typeis(x: object, y: int) -> TypeIs[int]: |
| 793 |
... |
| 794 |
|
| 795 |
def typeis(x: object, y: Union[int, str]) -> Union[TypeIs[int], TypeIs[str]]: |
| 796 |
... |
| 797 |
|
| 798 |
x: object |
| 799 |
if typeis(x=x, y=42): |
| 800 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 801 |
|
| 802 |
if typeis(y=42, x=x): |
| 803 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 804 |
|
| 805 |
if typeis(x=x, y="42"): |
| 806 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 807 |
|
| 808 |
if typeis(y="42", x=x): |
| 809 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 810 |
[builtins fixtures/tuple.pyi] |
| 811 |
|
| 812 |
[case testGenericAliasWithTypeIs] |
| 813 |
from typing import Callable, List, TypeVar |
| 814 |
from typing_extensions import TypeIs |
| 815 |
|
| 816 |
T = TypeVar('T') |
| 817 |
A = Callable[[object], TypeIs[List[T]]] |
| 818 |
def foo(x: object) -> TypeIs[List[str]]: ... |
| 819 |
|
| 820 |
def test(f: A[T]) -> T: ... |
| 821 |
reveal_type(test(foo)) # N: Revealed type is "builtins.str" |
| 822 |
[builtins fixtures/list.pyi] |
| 823 |
|
| 824 |
[case testNoCrashOnDunderCallTypeIs] |
| 825 |
from typing_extensions import TypeIs |
| 826 |
|
| 827 |
class A: |
| 828 |
def __call__(self, x) -> TypeIs[int]: |
| 829 |
return True |
| 830 |
|
| 831 |
a: A |
| 832 |
assert a(x=1) |
| 833 |
|
| 834 |
x: object |
| 835 |
assert a(x=x) |
| 836 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 837 |
[builtins fixtures/tuple.pyi] |
| 838 |
|
| 839 |
[case testTypeIsMustBeSubtypeFunctions] |
| 840 |
from typing_extensions import TypeIs |
| 841 |
from typing import List, Sequence, TypeVar |
| 842 |
|
| 843 |
def f(x: str) -> TypeIs[int]: # E: Narrowed type "int" is not a subtype of input type "str" |
| 844 |
pass |
| 845 |
|
| 846 |
T = TypeVar('T') |
| 847 |
|
| 848 |
def g(x: List[T]) -> TypeIs[Sequence[T]]: # E: Narrowed type "Sequence[T]" is not a subtype of input type "list[T]" |
| 849 |
pass |
| 850 |
|
| 851 |
[builtins fixtures/tuple.pyi] |
| 852 |
|
| 853 |
[case testTypeIsMustBeSubtypeMethods] |
| 854 |
from typing_extensions import TypeIs |
| 855 |
|
| 856 |
class NarrowHolder: |
| 857 |
@classmethod |
| 858 |
def cls_narrower_good(cls, x: object) -> TypeIs[int]: |
| 859 |
pass |
| 860 |
|
| 861 |
@classmethod |
| 862 |
def cls_narrower_bad(cls, x: str) -> TypeIs[int]: # E: Narrowed type "int" is not a subtype of input type "str" |
| 863 |
pass |
| 864 |
|
| 865 |
@staticmethod |
| 866 |
def static_narrower_good(x: object) -> TypeIs[int]: |
| 867 |
pass |
| 868 |
|
| 869 |
@staticmethod |
| 870 |
def static_narrower_bad(x: str) -> TypeIs[int]: # E: Narrowed type "int" is not a subtype of input type "str" |
| 871 |
pass |
| 872 |
|
| 873 |
def inst_narrower_good(self, x: object) -> TypeIs[int]: |
| 874 |
pass |
| 875 |
|
| 876 |
def inst_narrower_bad(self, x: str) -> TypeIs[int]: # E: Narrowed type "int" is not a subtype of input type "str" |
| 877 |
pass |
| 878 |
|
| 879 |
|
| 880 |
[builtins fixtures/classmethod.pyi] |
| 881 |
|
| 882 |
[case testTypeIsTypeGuardNoSubtyping] |
| 883 |
from typing_extensions import TypeGuard, TypeIs |
| 884 |
from typing import Callable |
| 885 |
|
| 886 |
def accept_typeis(x: Callable[[object], TypeIs[str]]): |
| 887 |
pass |
| 888 |
|
| 889 |
def accept_typeguard(x: Callable[[object], TypeGuard[str]]): |
| 890 |
pass |
| 891 |
|
| 892 |
def typeis(x: object) -> TypeIs[str]: |
| 893 |
pass |
| 894 |
|
| 895 |
def typeguard(x: object) -> TypeGuard[str]: |
| 896 |
pass |
| 897 |
|
| 898 |
accept_typeis(typeis) |
| 899 |
accept_typeis(typeguard) # E: Argument 1 to "accept_typeis" has incompatible type "Callable[[object], TypeGuard[str]]"; expected "Callable[[object], TypeIs[str]]" |
| 900 |
accept_typeguard(typeis) # E: Argument 1 to "accept_typeguard" has incompatible type "Callable[[object], TypeIs[str]]"; expected "Callable[[object], TypeGuard[str]]" |
| 901 |
accept_typeguard(typeguard) |
| 902 |
|
| 903 |
[builtins fixtures/tuple.pyi] |
| 904 |
|
| 905 |
[case testTypeIsEnumOverlappingUnionExcludesIrrelevant] |
| 906 |
from enum import Enum |
| 907 |
from typing import Literal |
| 908 |
from typing_extensions import TypeIs |
| 909 |
|
| 910 |
class Model(str, Enum): |
| 911 |
A = 'a' |
| 912 |
B = 'a' |
| 913 |
|
| 914 |
def is_model_a(model: str) -> TypeIs[Literal[Model.A, "foo"]]: |
| 915 |
return True |
| 916 |
|
| 917 |
def handle(model: Model) -> None: |
| 918 |
if is_model_a(model): |
| 919 |
reveal_type(model) # N: Revealed type is "Literal[__main__.Model.A]" |
| 920 |
[builtins fixtures/tuple.pyi] |
| 921 |
|
| 922 |
[case testTypeIsAwaitableAny] |
| 923 |
from __future__ import annotations |
| 924 |
from typing import Any, Awaitable, Callable |
| 925 |
from typing_extensions import TypeIs |
| 926 |
|
| 927 |
def is_async_callable(obj: Any) -> TypeIs[Callable[..., Awaitable[Any]]]: ... |
| 928 |
|
| 929 |
def main(f: Callable[[], int | Awaitable[int]]) -> None: |
| 930 |
if is_async_callable(f): |
| 931 |
reveal_type(f) # N: Revealed type is "def (*Any, **Any) -> typing.Awaitable[Any]" |
| 932 |
else: |
| 933 |
reveal_type(f) # N: Revealed type is "def () -> builtins.int | typing.Awaitable[builtins.int]" |
| 934 |
[builtins fixtures/tuple.pyi] |
| 935 |
|
| 936 |
[case testTypeIsWithDefer] |
| 937 |
from typing import Union |
| 938 |
from typing_extensions import TypeIs |
| 939 |
|
| 940 |
class A: ... |
| 941 |
class B: ... |
| 942 |
|
| 943 |
def is_a(x: object) -> TypeIs[A]: |
| 944 |
return defer_not_defined() # E: Name "defer_not_defined" is not defined |
| 945 |
|
| 946 |
def main(x: Union[A, B]) -> None: |
| 947 |
if is_a(x): |
| 948 |
reveal_type(x) # N: Revealed type is "__main__.A" |
| 949 |
else: |
| 950 |
reveal_type(x) # N: Revealed type is "__main__.B" |
| 951 |
[builtins fixtures/tuple.pyi] |
| 952 |
|
| 953 |
|
| 954 |
[case testTypeIsGeneric] |
| 955 |
from __future__ import annotations |
| 956 |
from typing import Any, Callable, TypeVar |
| 957 |
from typing_extensions import TypeIs |
| 958 |
|
| 959 |
T = TypeVar('T') |
| 960 |
|
| 961 |
def positive(x: T | int, checker: Callable[[Any], TypeIs[T]], default: int) -> int: |
| 962 |
if checker(x): |
| 963 |
reveal_type(x) # N: Revealed type is "T`-1" |
| 964 |
return default |
| 965 |
else: |
| 966 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 967 |
return x |
| 968 |
|
| 969 |
def negative(x: T | int, checker: Callable[[Any], TypeIs[int]], default: T) -> T: |
| 970 |
if checker(x): |
| 971 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 972 |
return default |
| 973 |
else: |
| 974 |
reveal_type(x) # N: Revealed type is "T`-1" |
| 975 |
return x |
| 976 |
[builtins fixtures/tuple.pyi] |
| 977 |
|
| 978 |
|
| 979 |
[case testTypeIsAwaitable] |
| 980 |
from __future__ import annotations |
| 981 |
from typing import Any, Awaitable, Callable, TypeVar, overload |
| 982 |
from typing_extensions import TypeIs |
| 983 |
|
| 984 |
T = TypeVar("T") |
| 985 |
AwaitableCallable = Callable[..., Awaitable[T]] |
| 986 |
|
| 987 |
@overload |
| 988 |
def is_async_callable(obj: AwaitableCallable[T]) -> TypeIs[AwaitableCallable[T]]: ... |
| 989 |
@overload |
| 990 |
def is_async_callable(obj: Any) -> TypeIs[AwaitableCallable[Any]]: ... |
| 991 |
def is_async_callable(obj): ... |
| 992 |
|
| 993 |
def main(f: Callable[[], int | Awaitable[int]] | Callable[[], Awaitable[int]]) -> None: |
| 994 |
if is_async_callable(f): |
| 995 |
reveal_type(f) # N: Revealed type is "(def (*Any, **Any) -> typing.Awaitable[Any]) | (def () -> typing.Awaitable[builtins.int])" |
| 996 |
else: |
| 997 |
reveal_type(f) # N: Revealed type is "def () -> builtins.int | typing.Awaitable[builtins.int]" |
| 998 |
|
| 999 |
[builtins fixtures/tuple.pyi] |