def expand_type_alias(target: Type, alias_tvars: List[str], args: List[Type], fail: Callable[[str, Context], None], no_args: bool, ctx: Context) -> Type: """Expand a (generic) type alias target following the rules outlined in TypeAlias docstring. Here: target: original target type (contains unbound type variables) alias_tvars: type variable names args: types to be substituted in place of type variables fail: error reporter callback no_args: whether original definition used a bare generic `A = List` ctx: context where expansion happens """ exp_len = len(alias_tvars) act_len = len(args) if exp_len > 0 and act_len == 0: # Interpret bare Alias same as normal generic, i.e., Alias[Any, Any, ...] assert alias_tvars is not None return set_any_tvars(target, alias_tvars, ctx.line, ctx.column) if exp_len == 0 and act_len == 0: if no_args: assert isinstance(target, Instance) return Instance(target.type, [], line=ctx.line, column=ctx.column) return target if exp_len == 0 and act_len > 0 and isinstance(target, Instance) and no_args: tp = Instance(target.type, args) tp.line = ctx.line tp.column = ctx.column return tp if act_len != exp_len: fail( 'Bad number of arguments for type alias, expected: %s, given: %s' % (exp_len, act_len), ctx) return set_any_tvars(target, alias_tvars or [], ctx.line, ctx.column, implicit=False) typ = replace_alias_tvars(target, alias_tvars, args, ctx.line, ctx.column) # HACK: Implement FlexibleAlias[T, typ] by expanding it to typ here. if (isinstance(typ, Instance) and typ.type.fullname() == 'mypy_extensions.FlexibleAlias'): typ = typ.args[-1] return typ