@@ -350,11 +350,11 @@ def __init__(
350350 self .args = args
351351 self .type_ref : str | None = None
352352
353- def expand_once (self , skip_normalization : bool = False ) -> Type :
353+ def _expand_once (self ) -> Type :
354354 """Expand to the target type exactly once.
355355
356356 This doesn't do full expansion, i.e. the result can contain another
357- (or even this same) type alias. Use this helper only when needed,
357+ (or even this same) type alias. Use this internal helper only when really needed,
358358 its public wrapper mypy.types.get_proper_type() is preferred.
359359 """
360360 assert self .alias is not None
@@ -381,8 +381,7 @@ def expand_once(self, skip_normalization: bool = False) -> Type:
381381 ):
382382 mapping [tvar .id ] = sub
383383
384- visitor = InstantiateAliasVisitor (mapping , skip_normalization = skip_normalization )
385- return self .alias .target .accept (visitor )
384+ return self .alias .target .accept (InstantiateAliasVisitor (mapping ))
386385
387386 @property
388387 def is_recursive (self ) -> bool :
@@ -3708,7 +3707,7 @@ def get_proper_type(typ: Type | None) -> ProperType | None:
37083707 if isinstance (typ , TypeGuardedType ):
37093708 typ = typ .type_guard
37103709 while isinstance (typ , TypeAliasType ):
3711- typ = typ .expand_once ()
3710+ typ = typ ._expand_once ()
37123711 # TODO: store the name of original type alias on this type, so we can show it in errors.
37133712 return cast (ProperType , typ )
37143713
@@ -4252,12 +4251,12 @@ def find_unpack_in_list(items: Sequence[Type]) -> int | None:
42524251 # Funky code here avoids mypyc narrowing the type of unpack_index.
42534252 old_index = unpack_index
42544253 assert old_index is None
4255- # Don't return so that we can also sanity check there is only one.
4254+ # Don't return so that we can also sanity- check there is only one.
42564255 unpack_index = i
42574256 return unpack_index
42584257
42594258
4260- def flatten_nested_tuples (types : Iterable [Type ]) -> list [Type ]:
4259+ def flatten_nested_tuples (types : Iterable [Type ], handle_recursive : bool = True ) -> list [Type ]:
42614260 """Recursively flatten TupleTypes nested with Unpack.
42624261
42634262 For example this will transform
@@ -4271,7 +4270,12 @@ def flatten_nested_tuples(types: Iterable[Type]) -> list[Type]:
42714270 res .append (typ )
42724271 continue
42734272 p_type = get_proper_type (typ .type )
4274- if not isinstance (p_type , TupleType ):
4273+ if (
4274+ not isinstance (p_type , TupleType )
4275+ or not handle_recursive
4276+ and isinstance (typ .type , TypeAliasType )
4277+ and typ .type .is_recursive
4278+ ):
42754279 res .append (typ )
42764280 continue
42774281 if isinstance (typ .type , TypeAliasType ):
0 commit comments