1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
|
-- TypeForm Type
[case testRecognizesUnparameterizedTypeFormInAnnotation]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm
typx: TypeForm = str
reveal_type(typx) # N: Revealed type is "TypeForm[Any]"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testRecognizesParameterizedTypeFormInAnnotation]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm
typx: TypeForm[str] = str
reveal_type(typx) # N: Revealed type is "TypeForm[builtins.str]"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
-- Type Expression Location: Assignment
[case testCanAssignTypeExpressionToTypeFormVariable]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm
typx: TypeForm[str] = str
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testCanAssignTypeExpressionToUnionTypeFormVariable]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm
typx: TypeForm[str | None] = str | None
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testCannotAssignTypeExpressionToTypeFormVariableWithIncompatibleItemType]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm
typx: TypeForm[str] = int # E: Incompatible types in assignment (expression has type "TypeForm[int]", variable has type "TypeForm[str]")
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testCanAssignValueExpressionToTypeFormVariableIfValueIsATypeForm1]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm
typx1: TypeForm = str
typx2: TypeForm = typx1 # looks like a type expression: name
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testCanAssignValueExpressionToTypeFormVariableIfValueIsATypeForm2]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm
def identity_tf(x: TypeForm) -> TypeForm:
return x
typx1: TypeForm = str
typx2: TypeForm = identity_tf(typx1) # does not look like a type expression
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testCannotAssignValueExpressionToTypeFormVariableIfValueIsNotATypeForm]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm
val: int = 42
typx: TypeForm = val # E: Incompatible types in assignment (expression has type "int", variable has type "TypeForm[Any]")
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testCanAssignNoneTypeExpressionToTypeFormVariable]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm
typx: TypeForm = None
reveal_type(typx) # N: Revealed type is "TypeForm[Any]"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testCanAssignTypeExpressionToTypeFormVariableDeclaredEarlier]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing import Type, TypeForm
typ: Type
typ = int | None # E: Incompatible types in assignment (expression has type "object", variable has type "type[Any]")
typx: TypeForm
typx = int | None
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testCanAssignTypeExpressionWithStringAnnotationToTypeFormVariable]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm
typx: TypeForm[str | None] = 'str | None'
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
-- Type Expression Location: Function Parameter
[case testCanPassTypeExpressionToTypeFormParameterInFunction]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm
def is_type(typx: TypeForm) -> bool:
return isinstance(typx, type)
is_type(int | None)
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testCannotPassTypeExpressionToTypeParameter]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
def is_type(typ: type) -> bool:
return isinstance(typ, type)
is_type(int | None) # E: Argument 1 to "is_type" has incompatible type "object"; expected "type"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testCanPassTypeExpressionToTypeFormParameterInMethod]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm
class C:
def is_type(self, typx: TypeForm) -> bool:
return isinstance(typx, type)
C().is_type(int | None)
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testCanPassTypeExpressionToTypeFormParameterInOverload]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing import overload, TypeForm
@overload
def is_type(typx: TypeForm) -> bool: ...
@overload
def is_type(typx: type) -> bool: ...
def is_type(typx):
return isinstance(typx, type)
is_type(int | None)
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testCanPassTypeExpressionToTypeFormParameterInDecorator]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing import Callable, TypeForm, TypeVar
P = TypeVar('P')
R = TypeVar('R')
def expects_type(typx: TypeForm) -> Callable[[Callable[[P], R]], Callable[[P], R]]:
def wrap(func: Callable[[P], R]) -> Callable[[P], R]:
func.expected_type = typx # type: ignore[attr-defined]
return func
return wrap
@expects_type(int | None)
def sum_ints(x: int | None) -> int:
return (x or 0)
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testCanPassTypeExpressionToTypeFormVarargsParameter]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing import Callable, ParamSpec, TypeForm, TypeVar
P = ParamSpec('P')
R = TypeVar('R')
def expects_types(*typxs: TypeForm) -> Callable[[Callable[P, R]], Callable[P, R]]:
def wrap(func: Callable[P, R]) -> Callable[P, R]:
func.expected_types = typxs # type: ignore[attr-defined]
return func
return wrap
@expects_types(int | None, int)
def sum_ints(x: int | None, y: int) -> tuple[int, int]:
return ((x or 0), y)
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testCanPassTypeExpressionWithStringAnnotationToTypeFormParameter]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm
def is_type(typx: TypeForm) -> bool:
return isinstance(typx, type)
is_type('int | None')
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
-- Type Expression Location: Return Statement
[case testCanReturnTypeExpressionInFunctionWithTypeFormReturnType]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm
def maybe_int_type() -> TypeForm:
return int | None
reveal_type(maybe_int_type()) # N: Revealed type is "TypeForm[Any]"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testCanReturnTypeExpressionWithStringAnnotationInFunctionWithTypeFormReturnType]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm
def maybe_int_type() -> TypeForm:
return 'int | None'
reveal_type(maybe_int_type()) # N: Revealed type is "TypeForm[Any]"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
-- Type Expression Location: Other
-- In particular ensure that ExpressionChecker.try_parse_as_type_expression() in
-- the TypeChecker pass is able to parse types correctly even though it doesn't
-- have the same rich context as SemanticAnalyzer.try_parse_as_type_expression().
[case testTypeExpressionWithoutStringAnnotationRecognizedInOtherSyntacticLocations]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing import Dict, List, TypeForm
list_of_typx: List[TypeForm] = [int | str]
dict_with_typx_keys: Dict[TypeForm, int] = {
int | str: 1,
str | None: 2,
}
dict_with_typx_keys[int | str] + 1
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testTypeExpressionWithStringAnnotationNotRecognizedInOtherSyntacticLocations]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing import Dict, List, TypeForm
list_of_typx: List[TypeForm] = ['int | str'] # E: TypeForm containing a string annotation cannot be recognized here. Surround with TypeForm(...) to recognize. \
# E: List item 0 has incompatible type "str"; expected "TypeForm[Any]"
dict_with_typx_keys: Dict[TypeForm, int] = {
'int | str': 1, # E: TypeForm containing a string annotation cannot be recognized here. Surround with TypeForm(...) to recognize. \
# E: Dict entry 0 has incompatible type "str": "int"; expected "TypeForm[Any]": "int"
'str | None': 2, # E: TypeForm containing a string annotation cannot be recognized here. Surround with TypeForm(...) to recognize. \
# E: Dict entry 1 has incompatible type "str": "int"; expected "TypeForm[Any]": "int"
}
dict_with_typx_keys['int | str'] += 1 # E: TypeForm containing a string annotation cannot be recognized here. Surround with TypeForm(...) to recognize. \
# E: Invalid index type "str" for "dict[TypeForm[Any], int]"; expected type "TypeForm[Any]"
[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]
[case testValueExpressionWithStringInTypeFormContextEmitsConservativeWarning]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing import Any, Dict, List, TypeForm
types: Dict[str, TypeForm] = {'any': Any}
# Ensure warning can be ignored if does not apply.
list_of_typx1: List[TypeForm] = [types['any']] # E: TypeForm containing a string annotation cannot be recognized here. Surround with TypeForm(...) to recognize.
list_of_typx2: List[TypeForm] = [types['any']] # type: ignore[maybe-unrecognized-str-typeform]
# Ensure warning can be fixed using the suggested fix in the warning message.
list_of_typx3: List[TypeForm] = ['Any'] # E: TypeForm containing a string annotation cannot be recognized here. Surround with TypeForm(...) to recognize. \
# E: List item 0 has incompatible type "str"; expected "TypeForm[Any]"
list_of_typx4: List[TypeForm] = [TypeForm('Any')]
[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]
[case testSelfRecognizedInOtherSyntacticLocations]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing import List, Self, TypeForm
class C:
def foo(self) -> None:
list_of_typx1: List[TypeForm] = [Self]
typx1: TypeForm = Self
typx2: TypeForm = 'Self'
list_of_typx2: List[TypeForm] = [Self] # E: List item 0 has incompatible type "int"; expected "TypeForm[Any]"
typx3: TypeForm = Self # E: Incompatible types in assignment (expression has type "int", variable has type "TypeForm[Any]")
typx4: TypeForm = 'Self' # E: Incompatible types in assignment (expression has type "str", variable has type "TypeForm[Any]")
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testNameOrDottedNameRecognizedInOtherSyntacticLocations]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
import typing
from typing import List, TypeForm
list_of_typx: List[TypeForm] = [List | typing.Optional[str]]
typx: TypeForm = List | typing.Optional[str]
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testInvalidNameOrDottedNameRecognizedInOtherSyntacticLocations]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing import List, TypeForm
list_of_typx1: List[TypeForm] = [NoSuchType] # E: Name "NoSuchType" is not defined
list_of_typx2: List[TypeForm] = [no_such_module.NoSuchType] # E: Name "no_such_module" is not defined
typx1: TypeForm = NoSuchType # E: Name "NoSuchType" is not defined
typx2: TypeForm = no_such_module.NoSuchType # E: Name "no_such_module" is not defined
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
-- Type Expression Context: Union[TypeForm, <non-TypeForm>]
[case testAcceptsTypeFormLiteralAssignedToUnionOfTypeFormAndNonStr]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm
typx_or_int1: TypeForm[int | None] | int = int | None # No error; interpret as TypeForm
typx_or_int2: TypeForm[int | None] | int = str | None # E: Incompatible types in assignment (expression has type "object", variable has type "Union[TypeForm[Optional[int]], int]")
typx_or_int3: TypeForm[int | None] | int = 1
typx_or_int4: TypeForm[int | None] | int = object() # E: Incompatible types in assignment (expression has type "object", variable has type "Union[TypeForm[Optional[int]], int]")
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testAcceptsTypeFormLiteralAssignedToUnionOfTypeFormAndStr]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm
typx_or_str1: TypeForm[int | None] | str = 'int | None'
typx_or_str2: TypeForm[int | None] | str = 'str | None' # No error; interpret as str
typx_or_str3: TypeForm[int | None] | str = 'hello'
typx_or_str4: TypeForm[int | None] | str = object() # E: Incompatible types in assignment (expression has type "object", variable has type "Union[TypeForm[Optional[int]], str]")
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testValueExpressionWithStringInTypeFormUnionContextEmitsConservativeWarning1]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing import List, TypeForm
list_of_typx1: List[TypeForm[int | None] | str] = ['int | None'] # E: TypeForm containing a string annotation cannot be recognized here. Surround with TypeForm(...) to recognize.
list_of_typx2: List[TypeForm[int | None] | str] = ['str | None'] # E: TypeForm containing a string annotation cannot be recognized here. Surround with TypeForm(...) to recognize.
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testValueExpressionWithStringInTypeFormUnionContextEmitsConservativeWarning2]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing import List, TypeForm
list_of_typx3: List[TypeForm[int | None] | int] = ['int | None'] # E: TypeForm containing a string annotation cannot be recognized here. Surround with TypeForm(...) to recognize. \
# E: List item 0 has incompatible type "str"; expected "Union[TypeForm[Optional[int]], int]"
list_of_typx4: List[TypeForm[str | None] | int] = ['str | None'] # E: TypeForm containing a string annotation cannot be recognized here. Surround with TypeForm(...) to recognize. \
# E: List item 0 has incompatible type "str"; expected "Union[TypeForm[Optional[str]], int]"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
-- Assignability (is_subtype)
[case testTypeFormToTypeFormAssignability]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
# - TypeForm[T1] is assignable to TypeForm[T2] iff T1 is assignable to T2.
# - In particular TypeForm[Any] is assignable to TypeForm[Any].
from typing_extensions import TypeForm
INT_OR_STR_TF: TypeForm[int | str] = int | str
INT_TF: TypeForm[int] = int
STR_TF: TypeForm[str] = str
OBJECT_TF: TypeForm[object] = object
ANY_TF: TypeForm = object
reveal_type(ANY_TF) # N: Revealed type is "TypeForm[Any]"
typx1: TypeForm[int | str] = INT_OR_STR_TF
typx2: TypeForm[int | str] = INT_TF
typx3: TypeForm[int | str] = STR_TF
typx4: TypeForm[int | str] = OBJECT_TF # E: Incompatible types in assignment (expression has type "TypeForm[object]", variable has type "TypeForm[Union[int, str]]")
typx5: TypeForm[int | str] = ANY_TF # no error
typx6: TypeForm[int] = INT_OR_STR_TF # E: Incompatible types in assignment (expression has type "TypeForm[Union[int, str]]", variable has type "TypeForm[int]")
typx7: TypeForm[int] = INT_TF
typx8: TypeForm[int] = STR_TF # E: Incompatible types in assignment (expression has type "TypeForm[str]", variable has type "TypeForm[int]")
typx9: TypeForm[int] = OBJECT_TF # E: Incompatible types in assignment (expression has type "TypeForm[object]", variable has type "TypeForm[int]")
typx10: TypeForm[int] = ANY_TF # no error
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testTypeToTypeFormAssignability]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
# - Type[C] is assignable to TypeForm[T] iff C is assignable to T.
# - In particular Type[Any] is assignable to TypeForm[Any].
from typing import Type, TypeForm
INT_T: Type[int] = int
STR_T: Type[str] = str
OBJECT_T: Type[object] = object
ANY_T: Type = object
reveal_type(ANY_T) # N: Revealed type is "type[Any]"
typx1: TypeForm[int | str] = INT_T
typx2: TypeForm[int | str] = STR_T
typx3: TypeForm[int | str] = OBJECT_T # E: Incompatible types in assignment (expression has type "type[object]", variable has type "TypeForm[Union[int, str]]")
typx4: TypeForm[int | str] = ANY_T # no error
typx5: TypeForm[int] = INT_T
typx6: TypeForm[int] = STR_T # E: Incompatible types in assignment (expression has type "type[str]", variable has type "TypeForm[int]")
typx7: TypeForm[int] = OBJECT_T # E: Incompatible types in assignment (expression has type "type[object]", variable has type "TypeForm[int]")
typx8: TypeForm[int] = ANY_T # no error
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testTypeFormToTypeAssignability]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
# - TypeForm[T] is NOT assignable to Type[C].
# - In particular TypeForm[Any] is NOT assignable to Type[Any].
from typing import Type, TypeForm
INT_OR_STR_TF: TypeForm[int | str] = int | str
INT_TF: TypeForm[int] = int
STR_TF: TypeForm[str] = str
OBJECT_TF: TypeForm[object] = object
ANY_TF: TypeForm = object
reveal_type(ANY_TF) # N: Revealed type is "TypeForm[Any]"
typ1: Type[int] = INT_OR_STR_TF # E: Incompatible types in assignment (expression has type "TypeForm[Union[int, str]]", variable has type "type[int]")
typ2: Type[int] = INT_TF # E: Incompatible types in assignment (expression has type "TypeForm[int]", variable has type "type[int]")
typ3: Type[int] = STR_TF # E: Incompatible types in assignment (expression has type "TypeForm[str]", variable has type "type[int]")
typ4: Type[int] = OBJECT_TF # E: Incompatible types in assignment (expression has type "TypeForm[object]", variable has type "type[int]")
typ5: Type[int] = ANY_TF # E: Incompatible types in assignment (expression has type "TypeForm[Any]", variable has type "type[int]")
typ6: Type[object] = INT_OR_STR_TF # E: Incompatible types in assignment (expression has type "TypeForm[Union[int, str]]", variable has type "type[object]")
typ7: Type[object] = INT_TF # E: Incompatible types in assignment (expression has type "TypeForm[int]", variable has type "type[object]")
typ8: Type[object] = STR_TF # E: Incompatible types in assignment (expression has type "TypeForm[str]", variable has type "type[object]")
typ9: Type[object] = OBJECT_TF # E: Incompatible types in assignment (expression has type "TypeForm[object]", variable has type "type[object]")
typ10: Type[object] = ANY_TF # E: Incompatible types in assignment (expression has type "TypeForm[Any]", variable has type "type[object]")
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
# NOTE: This test doesn't involve TypeForm at all, but is still illustrative
# when compared with similarly structured TypeForm-related tests above.
[case testTypeToTypeAssignability]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
# - Type[C1] is assignable to Type[C2] iff C1 is assignable to C2.
# - In particular Type[Any] is assignable to Type[Any].
from typing import Type
INT_T: Type[int] = int
STR_T: Type[str] = str
OBJECT_T: Type[object] = object
ANY_T: Type = object
reveal_type(ANY_T) # N: Revealed type is "type[Any]"
typ1: Type[int] = INT_T
typ2: Type[int] = STR_T # E: Incompatible types in assignment (expression has type "type[str]", variable has type "type[int]")
typ3: Type[int] = OBJECT_T # E: Incompatible types in assignment (expression has type "type[object]", variable has type "type[int]")
typ4: Type[int] = ANY_T # no error
typ5: Type[object] = INT_T
typ6: Type[object] = STR_T
typ7: Type[object] = OBJECT_T
typ8: Type[object] = ANY_T # no error
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testTypeFormToObjectAssignability]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
# - TypeForm[T] is assignable to object and Any.
from typing import Any, TypeForm
INT_TF: TypeForm[int] = int
OBJECT_TF: TypeForm[object] = object
ANY_TF: TypeForm = object
reveal_type(ANY_TF) # N: Revealed type is "TypeForm[Any]"
obj1: object = INT_TF
obj2: object = OBJECT_TF
obj3: object = ANY_TF
any1: Any = INT_TF
any2: Any = OBJECT_TF
any3: Any = ANY_TF
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
-- Join (join_types)
[case testTypeFormToTypeFormJoin]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
# - TypeForm[T1] join TypeForm[T2] == TypeForm[T1 join T2]
from typing_extensions import TypeForm
class AB:
pass
class A(AB):
pass
class B(AB):
pass
A_TF: TypeForm[A] = A
B_TF: TypeForm[B] = B
reveal_type([A_TF, B_TF][0]) # N: Revealed type is "TypeForm[__main__.AB]"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testTypeToTypeFormJoin]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
# - TypeForm[T1] join Type[T2] == TypeForm[T1 join T2]
from typing import Type, TypeForm
class AB:
pass
class A(AB):
pass
class B(AB):
pass
A_T: Type[A] = A
B_TF: TypeForm[B] = B
reveal_type([A_T, B_TF][0]) # N: Revealed type is "TypeForm[__main__.AB]"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testTypeFormToTypeJoin]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
# - TypeForm[T1] join Type[T2] == TypeForm[T1 join T2]
from typing import Type, TypeForm
class AB:
pass
class A(AB):
pass
class B(AB):
pass
A_TF: TypeForm[A] = A
B_T: Type[B] = B
reveal_type([A_TF, B_T][0]) # N: Revealed type is "TypeForm[__main__.AB]"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
# NOTE: This test doesn't involve TypeForm at all, but is still illustrative
# when compared with similarly structured TypeForm-related tests above.
[case testTypeToTypeJoin]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
# - Type[T1] join Type[T2] == Type[T1 join T2]
from typing import Type, TypeForm
class AB:
pass
class A(AB):
pass
class B(AB):
pass
A_T: Type[A] = A
B_T: Type[B] = B
reveal_type([A_T, B_T][0]) # N: Revealed type is "type[__main__.AB]"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
-- Meet (meet_types)
[case testTypeFormToTypeFormMeet]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
# - TypeForm[T1] meet TypeForm[T2] == TypeForm[T1 meet T2]
from typing import Callable, TypeForm, TypeVar
class AB:
pass
class A(AB):
pass
class B(AB):
pass
class C(AB):
pass
T = TypeVar('T')
def f(x: Callable[[T, T], None]) -> T: pass # type: ignore[empty-body]
def g(x: TypeForm[A | B], y: TypeForm[B | C]) -> None: pass
reveal_type(f(g)) # N: Revealed type is "TypeForm[__main__.B]"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testTypeToTypeFormMeet]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
# - TypeForm[T1] meet Type[T2] == Type[T1 meet T2]
from typing import Callable, Type, TypeForm, TypeVar
class AB:
pass
class A(AB):
pass
class B(AB):
pass
class C(AB):
pass
T = TypeVar('T')
def f(x: Callable[[T, T], None]) -> T: pass # type: ignore[empty-body]
def g(x: Type[B], y: TypeForm[B | C]) -> None: pass
reveal_type(f(g)) # N: Revealed type is "type[__main__.B]"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testTypeFormToTypeMeet]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
# - TypeForm[T1] meet Type[T2] == Type[T1 meet T2]
from typing import Callable, Type, TypeForm, TypeVar
class AB:
pass
class A(AB):
pass
class B(AB):
pass
class C(AB):
pass
T = TypeVar('T')
def f(x: Callable[[T, T], None]) -> T: pass # type: ignore[empty-body]
def g(x: TypeForm[A | B], y: Type[B]) -> None: pass
reveal_type(f(g)) # N: Revealed type is "type[__main__.B]"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
# NOTE: This test doesn't involve TypeForm at all, but is still illustrative
# when compared with similarly structured TypeForm-related tests above.
[case testTypeToTypeMeet]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
# - Type[T1] meet Type[T2] == Type[T1 meet T2]
from typing import Callable, Type, TypedDict, TypeForm, TypeVar
class AB(TypedDict):
a: str
b: str
class BC(TypedDict):
b: str
c: str
T = TypeVar('T')
def f(x: Callable[[T, T], None]) -> T: pass # type: ignore[empty-body]
def g(x: Type[AB], y: Type[BC]) -> None: pass
reveal_type(f(g)) # N: Revealed type is "type[TypedDict({'b': builtins.str, 'c': builtins.str, 'a': builtins.str})]"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
-- TypeForm(...) Expression
[case testTypeFormExpression]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm
tf1 = TypeForm(int | str)
reveal_type(tf1) # N: Revealed type is "TypeForm[Union[builtins.int, builtins.str]]"
tf2 = TypeForm('int | str')
reveal_type(tf2) # N: Revealed type is "TypeForm[Union[builtins.int, builtins.str]]"
tf3: TypeForm = TypeForm(int | str)
reveal_type(tf3) # N: Revealed type is "TypeForm[Any]"
tf4: TypeForm = TypeForm(1) # E: Invalid type: try using Literal[1] instead?
tf5: TypeForm = TypeForm(int) | TypeForm(str) # E: Incompatible types in assignment (expression has type "object", variable has type "TypeForm[Any]")
tf6: TypeForm = TypeForm(TypeForm(int) | TypeForm(str)) # E: TypeForm argument is not a type
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
-- isinstance
[case testTypeFormAndTypeIsinstance]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm
typx: TypeForm[str] = str
if isinstance(typx, type):
reveal_type(typx) # N: Revealed type is "type[builtins.str]"
else:
reveal_type(typx) # N: Revealed type is "TypeForm[builtins.str]"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
-- Type Variables
[case testLinkTypeFormToTypeFormWithTypeVariable]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm, TypeVar
T = TypeVar('T')
def as_typeform(typx: TypeForm[T]) -> TypeForm[T]:
return typx
reveal_type(as_typeform(int | str)) # N: Revealed type is "TypeForm[Union[builtins.int, builtins.str]]"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testLinkTypeFormToTypeWithTypeVariable]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing import Type, TypeForm, TypeVar
T = TypeVar('T')
def as_type(typx: TypeForm[T]) -> Type[T] | None:
if isinstance(typx, type):
return typx
else:
return None
reveal_type(as_type(int | str)) # N: Revealed type is "Union[type[builtins.int], type[builtins.str], None]"
reveal_type(as_type(int)) # N: Revealed type is "Union[type[builtins.int], None]"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testLinkTypeFormToInstanceWithTypeVariable]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm, TypeVar
T = TypeVar('T')
def as_instance(typx: TypeForm[T]) -> T | None:
if isinstance(typx, type):
return typx()
else:
return None
reveal_type(as_instance(int | str)) # N: Revealed type is "Union[builtins.int, builtins.str, None]"
reveal_type(as_instance(int)) # N: Revealed type is "Union[builtins.int, None]"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testLinkTypeFormToTypeIsWithTypeVariable]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm, TypeVar
from typing_extensions import TypeIs
T = TypeVar('T')
def isassignable(value: object, typx: TypeForm[T]) -> TypeIs[T]:
raise BaseException()
count: int | str = 1
if isassignable(count, int):
reveal_type(count) # N: Revealed type is "builtins.int"
else:
reveal_type(count) # N: Revealed type is "builtins.str"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testLinkTypeFormToTypeGuardWithTypeVariable]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm, TypeVar
from typing_extensions import TypeGuard
T = TypeVar('T')
def isassignable(value: object, typx: TypeForm[T]) -> TypeGuard[T]:
raise BaseException()
count: int | str = 1
if isassignable(count, int):
reveal_type(count) # N: Revealed type is "builtins.int"
else:
reveal_type(count) # N: Revealed type is "Union[builtins.int, builtins.str]"
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
-- Type Expressions Assignable To TypeForm Variable
[case testEveryKindOfTypeExpressionIsAssignableToATypeFormVariable]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
# NOTE: Importing Callable from collections.abc also works OK
from typing import (
Any, Callable, Dict, List, Literal, LiteralString, NoReturn,
Optional, ParamSpec, Self, Type, TypeGuard, TypeVar, Union,
)
from typing_extensions import (
Annotated, Concatenate, Never, TypeAlias, TypeForm, TypeIs,
TypeVarTuple, Unpack,
)
#
class SomeClass:
pass
SomeTypeAlias: TypeAlias = SomeClass
SomeTypeVar = TypeVar('SomeTypeVar')
Ts = TypeVarTuple('Ts')
IntTuple: TypeAlias = tuple[int, Unpack[Ts]]
P = ParamSpec('P')
R = TypeVar('R')
#
typx: TypeForm
# Begin rules taken from: https://typing.python.org/en/latest/spec/annotations.html#grammar-token-expression-grammar-type_expression
# <Any>
typx = Any
class SelfBinder:
def bind_self(self) -> Self:
typx: TypeForm
# <Self> (valid only in some contexts)
typx = Self
return self
# <LiteralString>
typx = LiteralString
# <NoReturn>
typx = NoReturn
# <Never>
typx = Never
# <None>
typx = None
# name (where name must refer to a valid in-scope class)
typx = SomeClass
# name (where name must refer to a valid in-scope type alias)
typx = SomeTypeAlias
# name (where name must refer to a valid in-scope TypeVar)
# NOTE: Unbound TypeVar isn't currently accepted as a TypeForm. Is that OK?
typx = SomeTypeVar # E: Incompatible types in assignment (expression has type "TypeVar", variable has type "TypeForm[Any]")
# name '[' type_expression (',' type_expression)* ']'
typx = Dict[str, int]
# (TODO: Add: name '[' unpacked ']')
# (TODO: Add: name '[' type_expression_list (',' type_expression_list)* ']')
# name '[' '(' ')' ']' (denoting specialization with an empty TypeVarTuple)
typx = IntTuple[()]
# <Literal> '[' expression (',' expression) ']'
typx = Literal[1]
# type_expression '|' type_expression
typx = int | str
# <Optional> '[' type_expression ']'
typx = Optional[str]
# <Union> '[' type_expression (',' type_expression)* ']'
typx = Union[int, str]
# <type> '[' <Any> ']'
typx = type[Any]
# <type> '[' name ']' (where name must refer to a valid in-scope class)
typx = type[int]
# (TODO: Add: <type> '[' name ']' (where name must refer to a valid in-scope TypeVar))
# <Callable> '[' '...' ',' type_expression ']'
typx = Callable[..., str]
def bind_R(input: R) -> R:
typx: TypeForm
# <Callable> '[' name ',' type_expression ']' (where name must be a valid in-scope ParamSpec)
typx = Callable[P, R]
# <Callable> '[' <Concatenate> '[' (type_expression ',')+
# (name | '...') ']' ',' type_expression ']'
# (where name must be a valid in-scope ParamSpec)
typx = Callable[Concatenate[int, P], R]
return input
# <Callable> '[' '[' maybe_unpacked (',' maybe_unpacked)* ']' ',' type_expression ']'
typx = Callable[[int, str], None]
# <tuple> '[' '(' ')' ']' (representing an empty tuple)
typx = tuple[()]
# <tuple> '[' type_expression ',' '...' ']' (representing an arbitrary-length tuple)
typx = tuple[int, ...]
# <tuple> '[' maybe_unpacked (',' maybe_unpacked)* ']'
typx = tuple[int, str]
# <Annotated> '[' type_expression ',' expression (',' expression)* ']'
typx = Annotated[str, 'uppercase']
# <TypeGuard> '[' type_expression ']' (valid only in some contexts)
typx = TypeGuard[List[str]]
# <TypeIs> '[' type_expression ']' (valid only in some contexts)
typx = TypeIs[List[str]]
# string_annotation (must evaluate to a valid type_expression)
typx = 'int | str'
# End rules
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
-- Misc
[case testTypeFormHasAllObjectAttributesAndMethods]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm
typx: TypeForm[int | str] = int | str
print(typx.__class__) # OK
print(typx.__hash__()) # OK
obj: object = typx
[file builtins.py]
class object:
def __init__(self) -> None: pass
__class__: None
def __hash__(self) -> int: pass
def print(x):
raise BaseException()
class int: pass
class dict: pass
class str: pass
class type: pass
class tuple: pass
class ellipsis: pass
class BaseException: pass
class float: pass
[typing fixtures/typing-full.pyi]
[case testDottedTypeFormsAreRecognized]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing_extensions import TypeForm
import typing
class C1:
class C2:
pass
typx1: TypeForm[C1.C2] = C1.C2 # OK
typx2: TypeForm[typing.Any] = typing.Any # OK
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
-- mypy already refused to recognize TypeVars in value expressions before
-- the TypeForm feature was introduced.
[case testTypeVarTypeFormsAreOnlyRecognizedInStringAnnotation]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing import Generic, List, TypeForm, TypeVar
E = TypeVar('E')
class Box(Generic[E]):
def foo(self, e: E) -> None:
list_of_typx: List[TypeForm] = [E] # E: "E" is a type variable and only valid in type context
typx1: TypeForm = E # E: "E" is a type variable and only valid in type context
typx2: TypeForm = 'E'
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testIncompleteTypeFormsAreNotRecognized]
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm
from typing import Optional, TypeForm
typx: TypeForm = Optional # E: Incompatible types in assignment (expression has type "int", variable has type "TypeForm[Any]")
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
|