Skip to content

Commit 38afbd2

Browse files
msullivangvanrossum
authored andcommitted
Suggest spurious trailing commas as a cause for invalid tuple types (#4336)
Fixes #4172
1 parent 0bed269 commit 38afbd2

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

mypy/typeanal.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,8 @@ def visit_tuple_type(self, t: TupleType) -> Type:
389389
# generate errors elsewhere, and Tuple[t1, t2, ...] must be used instead.
390390
if t.implicit and not self.allow_tuple_literal:
391391
self.fail('Invalid tuple literal type', t)
392+
if len(t.items) == 1:
393+
self.note_func('Suggestion: Is there a spurious trailing comma?', t)
392394
return AnyType(TypeOfAny.from_error)
393395
star_count = sum(1 for item in t.items if isinstance(item, StarType))
394396
if star_count > 1:

test-data/unit/check-fastparse.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,15 @@ def call() -> str: pass
423423
[case testNoCrashOnImportFromStarPython2]
424424
# flags: --py2
425425
from . import * # E: No parent module -- cannot perform relative import
426+
427+
[case testSpuriousTrailingComma_python2]
428+
from typing import Optional
429+
430+
def update_state(tid, # type: int
431+
vid, # type: int
432+
update_ts=None, # type: Optional[float],
433+
): # type: (...) -> str
434+
pass
435+
[out]
436+
main:3: error: Invalid tuple literal type
437+
main:3: note: Suggestion: Is there a spurious trailing comma?

test-data/unit/check-functions.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2212,7 +2212,7 @@ foo(y=2) # E: Missing positional arguments
22122212
def dec(f): pass
22132213

22142214
@dec
2215-
def test(a: str) -> (str,): # E: Invalid tuple literal type
2215+
def test(a: str) -> (str,): # E: Invalid tuple literal type # N: Suggestion: Is there a spurious trailing comma?
22162216
return None
22172217

22182218
[case testReturnTypeLineNumberNewLine]

0 commit comments

Comments
 (0)