@@ -1087,6 +1087,7 @@ def unimported_type_becomes_any(self, prefix: str, typ: Type, ctx: Context) -> N
1087
1087
def need_annotation_for_var (self , node : SymbolNode , context : Context ,
1088
1088
python_version : Optional [Tuple [int , int ]] = None ) -> None :
1089
1089
hint = ''
1090
+ has_variable_annotations = not python_version or python_version >= (3 , 6 )
1090
1091
# Only gives hint if it's a variable declaration and the partial type is a builtin type
1091
1092
if (python_version and isinstance (node , Var ) and isinstance (node .type , PartialType ) and
1092
1093
node .type .type and node .type .type .fullname () in reverse_builtin_aliases ):
@@ -1095,11 +1096,17 @@ def need_annotation_for_var(self, node: SymbolNode, context: Context,
1095
1096
type_dec = '<type>'
1096
1097
if alias == 'Dict' :
1097
1098
type_dec = '{}, {}' .format (type_dec , type_dec )
1098
- if python_version < (3 , 6 ):
1099
- hint = ' (hint: "{} = ... # type: {}[{}]")' .format (node .name (), alias , type_dec )
1100
- else :
1099
+ if has_variable_annotations :
1101
1100
hint = ' (hint: "{}: {}[{}] = ...")' .format (node .name (), alias , type_dec )
1102
- self .fail ("Need type annotation for '{}'{}" .format (unmangle (node .name ()), hint ), context )
1101
+ else :
1102
+ hint = ' (hint: "{} = ... # type: {}[{}]")' .format (node .name (), alias , type_dec )
1103
+
1104
+ if has_variable_annotations :
1105
+ needed = 'annotation'
1106
+ else :
1107
+ needed = 'comment'
1108
+
1109
+ self .fail ("Need type {} for '{}'{}" .format (needed , unmangle (node .name ()), hint ), context )
1103
1110
1104
1111
def explicit_any (self , ctx : Context ) -> None :
1105
1112
self .fail ('Explicit "Any" is not allowed' , ctx )
0 commit comments