|
9 | 9 |
|
10 | 10 | from mypy import errorcodes as codes, message_registry, nodes
|
11 | 11 | from mypy.errorcodes import ErrorCode
|
| 12 | +from mypy.expandtype import expand_type |
12 | 13 | from mypy.messages import MessageBuilder, format_type_bare, quote_type_string, wrong_type_arg_count
|
13 | 14 | from mypy.nodes import (
|
14 | 15 | ARG_NAMED,
|
|
75 | 76 | TypeOfTypeList,
|
76 | 77 | TypeQuery,
|
77 | 78 | TypeType,
|
| 79 | + TypeVarId, |
78 | 80 | TypeVarLikeType,
|
79 | 81 | TypeVarTupleType,
|
80 | 82 | TypeVarType,
|
@@ -1669,27 +1671,39 @@ def fix_instance(
|
1669 | 1671 |
|
1670 | 1672 | Also emit a suitable error if this is not due to implicit Any's.
|
1671 | 1673 | """
|
1672 |
| - if len(t.args) == 0: |
1673 |
| - if use_generic_error: |
1674 |
| - fullname: str | None = None |
1675 |
| - else: |
1676 |
| - fullname = t.type.fullname |
1677 |
| - any_type = get_omitted_any( |
1678 |
| - disallow_any, fail, note, t, python_version, fullname, unexpanded_type |
| 1674 | + arg_count = len(t.args) |
| 1675 | + max_tv_count = len(t.type.type_vars) |
| 1676 | + args: list[Type] = [*(t.args[:max_tv_count])] |
| 1677 | + any_type: AnyType | None = None |
| 1678 | + env: dict[TypeVarId, Type] = {} |
| 1679 | + for tv, arg in itertools.zip_longest(t.type.defn.type_vars, t.args, fillvalue=None): |
| 1680 | + if tv is None: |
| 1681 | + continue |
| 1682 | + if arg is None: |
| 1683 | + if tv.has_default(): |
| 1684 | + arg = tv.default |
| 1685 | + else: |
| 1686 | + if any_type is None: |
| 1687 | + fullname = None if use_generic_error else t.type.fullname |
| 1688 | + any_type = get_omitted_any( |
| 1689 | + disallow_any, fail, note, t, python_version, fullname, unexpanded_type |
| 1690 | + ) |
| 1691 | + arg = any_type |
| 1692 | + args.append(arg) |
| 1693 | + env[tv.id] = arg |
| 1694 | + t.args = tuple(args) |
| 1695 | + fixed = expand_type(t, env) |
| 1696 | + assert isinstance(fixed, Instance) |
| 1697 | + t.args = fixed.args |
| 1698 | + |
| 1699 | + min_tv_count = sum(tv.has_default() is False for tv in t.type.defn.type_vars) |
| 1700 | + if arg_count != 0 and not (min_tv_count <= arg_count <= max_tv_count): |
| 1701 | + fail( |
| 1702 | + wrong_type_arg_count(min_tv_count, max_tv_count, str(arg_count), t.type.name), |
| 1703 | + t, |
| 1704 | + code=codes.TYPE_ARG, |
1679 | 1705 | )
|
1680 |
| - t.args = (any_type,) * len(t.type.type_vars) |
1681 |
| - return |
1682 |
| - # Invalid number of type parameters. |
1683 |
| - fail( |
1684 |
| - wrong_type_arg_count(len(t.type.type_vars), str(len(t.args)), t.type.name), |
1685 |
| - t, |
1686 |
| - code=codes.TYPE_ARG, |
1687 |
| - ) |
1688 |
| - # Construct the correct number of type arguments, as |
1689 |
| - # otherwise the type checker may crash as it expects |
1690 |
| - # things to be right. |
1691 |
| - t.args = tuple(AnyType(TypeOfAny.from_error) for _ in t.type.type_vars) |
1692 |
| - t.invalid = True |
| 1706 | + t.invalid = True |
1693 | 1707 |
|
1694 | 1708 |
|
1695 | 1709 | def expand_type_alias(
|
@@ -1746,7 +1760,7 @@ def expand_type_alias(
|
1746 | 1760 | if use_standard_error:
|
1747 | 1761 | # This is used if type alias is an internal representation of another type,
|
1748 | 1762 | # for example a generic TypedDict or NamedTuple.
|
1749 |
| - msg = wrong_type_arg_count(exp_len, str(act_len), node.name) |
| 1763 | + msg = wrong_type_arg_count(exp_len, exp_len, str(act_len), node.name) |
1750 | 1764 | else:
|
1751 | 1765 | msg = f"Bad number of arguments for type alias, expected: {exp_len}, given: {act_len}"
|
1752 | 1766 | fail(msg, ctx, code=codes.TYPE_ARG)
|
|
0 commit comments