@@ -236,8 +236,6 @@ def is_singleton_type(self) -> bool:
236236class TypeAliasType (Type ):
237237 """A type alias to another type.
238238
239- NOTE: this is not being used yet, and the implementation is still incomplete.
240-
241239 To support recursive type aliases we don't immediately expand a type alias
242240 during semantic analysis, but create an instance of this type that records the target alias
243241 definition node (mypy.nodes.TypeAlias) and type arguments (for generic aliases).
@@ -3197,6 +3195,40 @@ def union_items(typ: Type) -> List[ProperType]:
31973195 return [typ ]
31983196
31993197
3198+ def invalid_recursive_alias (seen_nodes : Set [mypy .nodes .TypeAlias ], target : Type ) -> bool :
3199+ """Flag aliases like A = Union[int, A] (and similar mutual aliases).
3200+
3201+ Such aliases don't make much sense, and cause problems in later phases.
3202+ """
3203+ if isinstance (target , TypeAliasType ):
3204+ if target .alias in seen_nodes :
3205+ return True
3206+ assert target .alias , f"Unfixed type alias { target .type_ref } "
3207+ return invalid_recursive_alias (seen_nodes | {target .alias }, get_proper_type (target ))
3208+ assert isinstance (target , ProperType )
3209+ if not isinstance (target , UnionType ):
3210+ return False
3211+ return any (invalid_recursive_alias (seen_nodes , item ) for item in target .items )
3212+
3213+
3214+ def bad_type_type_item (item : Type ) -> bool :
3215+ """Prohibit types like Type[Type[...]].
3216+
3217+ Such types are explicitly prohibited by PEP 484. Also they cause problems
3218+ with recursive types like T = Type[T], because internal representation of
3219+ TypeType item is normalized (i.e. always a proper type).
3220+ """
3221+ item = get_proper_type (item )
3222+ if isinstance (item , TypeType ):
3223+ return True
3224+ if isinstance (item , UnionType ):
3225+ return any (
3226+ isinstance (get_proper_type (i ), TypeType )
3227+ for i in flatten_nested_unions (item .items , handle_type_alias_type = True )
3228+ )
3229+ return False
3230+
3231+
32003232def is_union_with_any (tp : Type ) -> bool :
32013233 """Is this a union with Any or a plain Any type?"""
32023234 tp = get_proper_type (tp )
0 commit comments