@@ -1119,6 +1119,14 @@ def update_function_type_variables(self, fun_type: CallableType, defn: FuncItem)
1119
1119
fun_type .variables , has_self_type = a .bind_function_type_variables (fun_type , defn )
1120
1120
if has_self_type and self .type is not None :
1121
1121
self .setup_self_type ()
1122
+ if defn .type_args :
1123
+ bound_fullnames = {v .fullname for v in fun_type .variables }
1124
+ declared_fullnames = {self .qualified_name (p .name ) for p in defn .type_args }
1125
+ extra = sorted (bound_fullnames - declared_fullnames )
1126
+ if extra :
1127
+ self .msg .type_parameters_should_be_declared (
1128
+ [n .split ("." )[- 1 ] for n in extra ], defn
1129
+ )
1122
1130
return has_self_type
1123
1131
1124
1132
def setup_self_type (self ) -> None :
@@ -2076,11 +2084,19 @@ class Foo(Bar, Generic[T]): ...
2076
2084
continue
2077
2085
result = self .analyze_class_typevar_declaration (base )
2078
2086
if result is not None :
2079
- if declared_tvars :
2080
- self .fail ("Only single Generic[...] or Protocol[...] can be in bases" , context )
2081
- removed .append (i )
2082
2087
tvars = result [0 ]
2083
2088
is_protocol |= result [1 ]
2089
+ if declared_tvars :
2090
+ if defn .type_args :
2091
+ if is_protocol :
2092
+ self .fail ('No arguments expected for "Protocol" base class' , context )
2093
+ else :
2094
+ self .fail ("Generic[...] base class is redundant" , context )
2095
+ else :
2096
+ self .fail (
2097
+ "Only single Generic[...] or Protocol[...] can be in bases" , context
2098
+ )
2099
+ removed .append (i )
2084
2100
declared_tvars .extend (tvars )
2085
2101
if isinstance (base , UnboundType ):
2086
2102
sym = self .lookup_qualified (base .name , base )
@@ -2092,15 +2108,21 @@ class Foo(Bar, Generic[T]): ...
2092
2108
2093
2109
all_tvars = self .get_all_bases_tvars (base_type_exprs , removed )
2094
2110
if declared_tvars :
2095
- if len (remove_dups (declared_tvars )) < len (declared_tvars ):
2111
+ if len (remove_dups (declared_tvars )) < len (declared_tvars ) and not defn . type_args :
2096
2112
self .fail ("Duplicate type variables in Generic[...] or Protocol[...]" , context )
2097
2113
declared_tvars = remove_dups (declared_tvars )
2098
2114
if not set (all_tvars ).issubset (set (declared_tvars )):
2099
- self .fail (
2100
- "If Generic[...] or Protocol[...] is present"
2101
- " it should list all type variables" ,
2102
- context ,
2103
- )
2115
+ if defn .type_args :
2116
+ undeclared = sorted (set (all_tvars ) - set (declared_tvars ))
2117
+ self .msg .type_parameters_should_be_declared (
2118
+ [tv [0 ] for tv in undeclared ], context
2119
+ )
2120
+ else :
2121
+ self .fail (
2122
+ "If Generic[...] or Protocol[...] is present"
2123
+ " it should list all type variables" ,
2124
+ context ,
2125
+ )
2104
2126
# In case of error, Generic tvars will go first
2105
2127
declared_tvars = remove_dups (declared_tvars + all_tvars )
2106
2128
else :
0 commit comments