Skip to content

Commit a8e664a

Browse files
refi64ilevkivskyi
authored andcommitted
Avoid telling the user to use type annotations on pre-3.5 (#7167)
Closes #6673
1 parent 18a9fe1 commit a8e664a

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

mypy/messages.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,7 @@ def unimported_type_becomes_any(self, prefix: str, typ: Type, ctx: Context) -> N
10871087
def need_annotation_for_var(self, node: SymbolNode, context: Context,
10881088
python_version: Optional[Tuple[int, int]] = None) -> None:
10891089
hint = ''
1090+
has_variable_annotations = not python_version or python_version >= (3, 6)
10901091
# Only gives hint if it's a variable declaration and the partial type is a builtin type
10911092
if (python_version and isinstance(node, Var) and isinstance(node.type, PartialType) and
10921093
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,
10951096
type_dec = '<type>'
10961097
if alias == 'Dict':
10971098
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:
11011100
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)
11031110

11041111
def explicit_any(self, ctx: Context) -> None:
11051112
self.fail('Explicit "Any" is not allowed', ctx)

test-data/unit/check-python2.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,5 +366,5 @@ b = none.__bool__() # E: "None" has no attribute "__bool__"
366366

367367
[case testDictWithoutTypeCommentInPython2]
368368
# flags: --py2
369-
d = dict() # E: Need type annotation for 'd' (hint: "d = ... \# type: Dict[<type>, <type>]")
369+
d = dict() # E: Need type comment for 'd' (hint: "d = ... \# type: Dict[<type>, <type>]")
370370
[builtins_py2 fixtures/floatdict_python2.pyi]

0 commit comments

Comments
 (0)