@@ -388,6 +388,8 @@ ListOrTuple = list[T] | tuple[T, ...]
388388ListOrTupleLegacy = Union[list[T], tuple[T, ... ]]
389389MyCallable = Callable[P, T]
390390AnnotatedType = Annotated[T, " tag" ]
391+ TransparentAlias = T
392+ MyOptional = T | None
391393
392394# TODO : Consider displaying this as `<class 'list[T]'>`, … instead? (and similar for some others below)
393395reveal_type(MyList) # revealed: <class 'list[typing.TypeVar]'>
@@ -400,43 +402,40 @@ reveal_type(ListOrTuple) # revealed: types.UnionType
400402reveal_type(ListOrTupleLegacy) # revealed: types.UnionType
401403reveal_type(MyCallable) # revealed: GenericAlias
402404reveal_type(AnnotatedType) # revealed: <typing.Annotated special form>
405+ reveal_type(TransparentAlias) # revealed: typing.TypeVar
406+ reveal_type(MyOptional) # revealed: types.UnionType
403407
404408def _ (
405409 list_of_ints : MyList[int ],
406410 dict_str_to_int : MyDict[str , int ],
407- # TODO : no error here
408- # error: [invalid-type-form] "`typing.TypeVar` is not a generic class"
409411 subclass_of_int : MyType[int ],
410412 int_and_str : IntAndType[str ],
411413 pair_of_ints : Pair[int ],
412414 int_and_bytes : Sum[int , bytes ],
413415 list_or_tuple : ListOrTuple[int ],
414416 list_or_tuple_legacy : ListOrTupleLegacy[int ],
415- # TODO : no error here
417+ # TODO : no errors here
416418 # error: [invalid-type-form] "List literals are not allowed in this context in a type expression: Did you mean `tuple[str, bytes]`?"
419+ # error: [too-many-positional-arguments] "Too many positional arguments: expected 1, got 2"
417420 my_callable : MyCallable[[str , bytes ], int ],
418421 annotated_int : AnnotatedType[int ],
422+ transparent_alias : TransparentAlias[int ],
423+ optional_int : MyOptional[int ],
419424):
420- # TODO : This should be `list[int]`
421- reveal_type(list_of_ints) # revealed: @Todo(specialized generic alias in type expression)
422- # TODO : This should be `dict[str, int]`
423- reveal_type(dict_str_to_int) # revealed: @Todo(specialized generic alias in type expression)
424- # TODO : This should be `type[int]`
425- reveal_type(subclass_of_int) # revealed: Unknown
426- # TODO : This should be `tuple[int, str]`
427- reveal_type(int_and_str) # revealed: @Todo(specialized generic alias in type expression)
428- # TODO : This should be `tuple[int, int]`
429- reveal_type(pair_of_ints) # revealed: @Todo(specialized generic alias in type expression)
430- # TODO : This should be `tuple[int, bytes]`
431- reveal_type(int_and_bytes) # revealed: @Todo(specialized generic alias in type expression)
432- # TODO : This should be `list[int] | tuple[int, ...]`
433- reveal_type(list_or_tuple) # revealed: @Todo(Generic specialization of types.UnionType)
434- # TODO : This should be `list[int] | tuple[int, ...]`
435- reveal_type(list_or_tuple_legacy) # revealed: @Todo(Generic specialization of types.UnionType)
425+ reveal_type(list_of_ints) # revealed: list[int]
426+ reveal_type(dict_str_to_int) # revealed: dict[str, int]
427+ reveal_type(subclass_of_int) # revealed: type[int]
428+ reveal_type(int_and_str) # revealed: tuple[int, str]
429+ reveal_type(pair_of_ints) # revealed: tuple[int, int]
430+ reveal_type(int_and_bytes) # revealed: tuple[int, bytes]
431+ reveal_type(list_or_tuple) # revealed: list[int] | tuple[int, ...]
432+ reveal_type(list_or_tuple_legacy) # revealed: list[int] | tuple[int, ...]
433+ reveal_type(list_or_tuple_legacy) # revealed: list[int] | tuple[int, ...]
436434 # TODO : This should be `(str, bytes) -> int`
437- reveal_type(my_callable) # revealed: @Todo(Generic specialization of typing.Callable)
438- # TODO : This should be `int`
439- reveal_type(annotated_int) # revealed: @Todo(Generic specialization of typing.Annotated)
435+ reveal_type(my_callable) # revealed: Unknown
436+ reveal_type(annotated_int) # revealed: int
437+ reveal_type(transparent_alias) # revealed: int
438+ reveal_type(optional_int) # revealed: int | None
440439```
441440
442441Generic implicit type aliases can be partially specialized:
@@ -446,15 +445,12 @@ U = TypeVar("U")
446445
447446DictStrTo = MyDict[str , U]
448447
449- reveal_type(DictStrTo) # revealed: GenericAlias
448+ reveal_type(DictStrTo) # revealed: <class 'dict[str, typing.TypeVar]'>
450449
451450def _ (
452- # TODO : No error here
453- # error: [invalid-type-form] "Invalid subscript of object of type `GenericAlias` in type expression"
454451 dict_str_to_int : DictStrTo[int ],
455452):
456- # TODO : This should be `dict[str, int]`
457- reveal_type(dict_str_to_int) # revealed: Unknown
453+ reveal_type(dict_str_to_int) # revealed: dict[str, int]
458454```
459455
460456Using specializations of generic implicit type aliases in other implicit type aliases works as
@@ -465,25 +461,31 @@ IntsOrNone = MyList[int] | None
465461IntsOrStrs = Pair[int ] | Pair[str ]
466462ListOfPairs = MyList[Pair[str ]]
467463
468- reveal_type(IntsOrNone) # revealed: UnionType
469- reveal_type(IntsOrStrs) # revealed: UnionType
470- reveal_type(ListOfPairs) # revealed: GenericAlias
464+ reveal_type(IntsOrNone) # revealed: types. UnionType
465+ reveal_type(IntsOrStrs) # revealed: types. UnionType
466+ reveal_type(ListOfPairs) # revealed: <class 'list[tuple[str, str]]'>
471467
472468def _ (
473- # TODO : This should not be an error
474- # error: [invalid-type-form] "Variable of type `UnionType` is not allowed in a type expression"
475469 ints_or_none : IntsOrNone,
476- # TODO : This should not be an error
477- # error: [invalid-type-form] "Variable of type `UnionType` is not allowed in a type expression"
478470 ints_or_strs : IntsOrStrs,
479471 list_of_pairs : ListOfPairs,
480472):
481- # TODO : This should be `list[int] | None`
482- reveal_type(ints_or_none) # revealed: Unknown
483- # TODO : This should be `tuple[int, int] | tuple[str, str]`
484- reveal_type(ints_or_strs) # revealed: Unknown
485- # TODO : This should be `list[tuple[str, str]]`
486- reveal_type(list_of_pairs) # revealed: @Todo(Support for `typing.GenericAlias` instances in type expressions)
473+ reveal_type(ints_or_none) # revealed: list[int] | None
474+ reveal_type(ints_or_strs) # revealed: tuple[int, int] | tuple[str, str]
475+ reveal_type(list_of_pairs) # revealed: list[tuple[str, str]]
476+ ```
477+
478+ A generic implicit type alias can also be used in another generic implicit type alias:
479+
480+ ``` py
481+ MyOtherList = MyList[T]
482+
483+ reveal_type(MyOtherList) # revealed: <class 'list[typing.TypeVar]'>
484+
485+ def _ (
486+ list_of_ints : MyOtherList[int ],
487+ ):
488+ reveal_type(list_of_ints) # revealed: list[int]
487489```
488490
489491If a generic implicit type alias is used unspecialized in a type expression, we treat it as an
@@ -522,8 +524,6 @@ reveal_mro(Derived1)
522524
523525GenericBaseAlias = GenericBase[T]
524526
525- # TODO : No error here
526- # error: [non-subscriptable] "Cannot subscript object of type `<class 'GenericBase[typing.TypeVar]'>` with no `__class_getitem__` method"
527527class Derived2 (GenericBaseAlias[int ]):
528528 pass
529529```
@@ -533,26 +533,23 @@ A generic alias that is already fully specialized cannot be specialized again:
533533``` py
534534ListOfInts = list[int ]
535535
536- # TODO : this should be an error
536+ # error: [too-many-positional-arguments] "Too many positional arguments: expected 0, got 1"
537537def _ (doubly_specialized : ListOfInts[int ]):
538- # TODO : this should be `Unknown`
539- reveal_type(doubly_specialized) # revealed: @Todo(specialized generic alias in type expression)
538+ reveal_type(doubly_specialized) # revealed: Unknown
540539```
541540
542541Specializing a generic implicit type alias with an incorrect number of type arguments also results
543542in an error:
544543
545544``` py
546545def _ (
547- # TODO : this should be an error
546+ # error: [too-many-positional-arguments] "Too many positional arguments: expected 1, got 2"
548547 list_too_many_args : MyList[int , str ],
549- # TODO : this should be an error
548+ # error: [missing-argument] "No argument provided for required parameter `U`"
550549 dict_too_few_args : MyDict[int ],
551550):
552- # TODO : this should be `Unknown`
553- reveal_type(list_too_many_args) # revealed: @Todo(specialized generic alias in type expression)
554- # TODO : this should be `Unknown`
555- reveal_type(dict_too_few_args) # revealed: @Todo(specialized generic alias in type expression)
551+ reveal_type(list_too_many_args) # revealed: Unknown
552+ reveal_type(dict_too_few_args) # revealed: Unknown
556553```
557554
558555## ` Literal ` s
@@ -642,8 +639,7 @@ Deprecated = Annotated[T, "deprecated attribute"]
642639class C :
643640 old: Deprecated[int ]
644641
645- # TODO : Should be `int`
646- reveal_type(C().old) # revealed: @Todo(Generic specialization of typing.Annotated)
642+ reveal_type(C().old) # revealed: int
647643```
648644
649645If the metadata argument is missing, we emit an error (because this code fails at runtime), but
0 commit comments