11
11
from mypy import errorcodes as codes , message_registry , nodes
12
12
from mypy .errorcodes import ErrorCode
13
13
from mypy .exprtotype import TypeTranslationError , expr_to_unanalyzed_type
14
- from mypy .messages import MessageBuilder , format_type_bare , quote_type_string
14
+ from mypy .messages import MessageBuilder , format_type_bare , quote_type_string , wrong_type_arg_count
15
15
from mypy .nodes import (
16
16
ARG_NAMED ,
17
17
ARG_NAMED_OPT ,
@@ -571,6 +571,9 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Typ
571
571
elif fullname in ("typing.Unpack" , "typing_extensions.Unpack" ):
572
572
if not self .api .incomplete_feature_enabled (UNPACK , t ):
573
573
return AnyType (TypeOfAny .from_error )
574
+ if len (t .args ) != 1 :
575
+ self .fail ("Unpack[...] requires exactly one type argument" , t )
576
+ return AnyType (TypeOfAny .from_error )
574
577
return UnpackType (self .anal_type (t .args [0 ]), line = t .line , column = t .column )
575
578
return None
576
579
@@ -644,14 +647,28 @@ def analyze_type_with_type_info(
644
647
# The class has a Tuple[...] base class so it will be
645
648
# represented as a tuple type.
646
649
if info .special_alias :
647
- return TypeAliasType (info .special_alias , self .anal_array (args ))
650
+ return expand_type_alias (
651
+ info .special_alias ,
652
+ self .anal_array (args ),
653
+ self .fail ,
654
+ False ,
655
+ ctx ,
656
+ use_standard_error = True ,
657
+ )
648
658
return tup .copy_modified (items = self .anal_array (tup .items ), fallback = instance )
649
659
td = info .typeddict_type
650
660
if td is not None :
651
661
# The class has a TypedDict[...] base class so it will be
652
662
# represented as a typeddict type.
653
663
if info .special_alias :
654
- return TypeAliasType (info .special_alias , self .anal_array (args ))
664
+ return expand_type_alias (
665
+ info .special_alias ,
666
+ self .anal_array (args ),
667
+ self .fail ,
668
+ False ,
669
+ ctx ,
670
+ use_standard_error = True ,
671
+ )
655
672
# Create a named TypedDictType
656
673
return td .copy_modified (
657
674
item_types = self .anal_array (list (td .items .values ())), fallback = instance
@@ -1535,16 +1552,11 @@ def fix_instance(
1535
1552
t .args = (any_type ,) * len (t .type .type_vars )
1536
1553
return
1537
1554
# Invalid number of type parameters.
1538
- n = len (t .type .type_vars )
1539
- s = f"{ n } type arguments"
1540
- if n == 0 :
1541
- s = "no type arguments"
1542
- elif n == 1 :
1543
- s = "1 type argument"
1544
- act = str (len (t .args ))
1545
- if act == "0" :
1546
- act = "none"
1547
- fail (f'"{ t .type .name } " expects { s } , but { act } given' , t , code = codes .TYPE_ARG )
1555
+ fail (
1556
+ wrong_type_arg_count (len (t .type .type_vars ), str (len (t .args )), t .type .name ),
1557
+ t ,
1558
+ code = codes .TYPE_ARG ,
1559
+ )
1548
1560
# Construct the correct number of type arguments, as
1549
1561
# otherwise the type checker may crash as it expects
1550
1562
# things to be right.
@@ -1561,6 +1573,7 @@ def expand_type_alias(
1561
1573
* ,
1562
1574
unexpanded_type : Type | None = None ,
1563
1575
disallow_any : bool = False ,
1576
+ use_standard_error : bool = False ,
1564
1577
) -> Type :
1565
1578
"""Expand a (generic) type alias target following the rules outlined in TypeAlias docstring.
1566
1579
@@ -1602,7 +1615,13 @@ def expand_type_alias(
1602
1615
tp .column = ctx .column
1603
1616
return tp
1604
1617
if act_len != exp_len :
1605
- fail (f"Bad number of arguments for type alias, expected: { exp_len } , given: { act_len } " , ctx )
1618
+ if use_standard_error :
1619
+ # This is used if type alias is an internal representation of another type,
1620
+ # for example a generic TypedDict or NamedTuple.
1621
+ msg = wrong_type_arg_count (exp_len , str (act_len ), node .name )
1622
+ else :
1623
+ msg = f"Bad number of arguments for type alias, expected: { exp_len } , given: { act_len } "
1624
+ fail (msg , ctx , code = codes .TYPE_ARG )
1606
1625
return set_any_tvars (node , ctx .line , ctx .column , from_error = True )
1607
1626
typ = TypeAliasType (node , args , ctx .line , ctx .column )
1608
1627
assert typ .alias is not None
0 commit comments