Pyright: unpack tuple with asterisk cause Tuple size mismatch error

Created on 16 Apr 2020  路  2Comments  路  Source: microsoft/pyright

To Reproduce

pyt/b.py

from typing import Tuple

t: Tuple[int, int, int, int] = (1, 2, 3, 4)

a, *b, c = t  # should be ok.
d, e, f, g, *h = t  # should be ok. `h` gets an empty list
i, j, k, l, m, *n = t  # should be ng.
$ pyright ./pyt/b.py 
typingsPath /home/kc5m/src/github.com/kc5m/pyt/typings is not a valid directory.
Searching for source files
Found 1 source file
/home/kc5m/src/github.com/kc5m/pyt/pyt/b.py
  5:1 - error: Tuple size mismatch: expected 3 but got 4 (reportGeneralTypeIssues)
  6:1 - error: Tuple size mismatch: expected at least 5 entries but got 4 (reportGeneralTypeIssues)
  7:1 - error: Tuple size mismatch: expected at least 6 entries but got 4 (reportGeneralTypeIssues)
3 errors, 0 warnings 
Completed in 0.813sec

# mypy also reports errors
$ mypy --strict ./pyt/b.py
pyt/b.py:6: error: Need type annotation for 'h' (hint: "h: List[<type>] = ...")
pyt/b.py:7: error: Need more than 4 values to unpack (5 expected)
Found 2 errors in 1 file (checked 1 source file)

Expected behavior

no error reported at line:5, 6

VS Code extension or command-line

$ pyright --version
pyright 1.1.31

$ mypy --version
mypy 0.770

Additional context

Behaviour of pyright to unpack tuple with variable length is different from mypy. And I think those behavior which I reported is pyright's bug. But especially in situation as below, I prefer to pyright's one to avoid runtime exception馃榾.

pyt/d.py

from typing import Iterable

def a(*args: int) -> Iterable[int]:
    _, *rest = args  # It would be better to replaced with `rest = args[1:]`. But it's just an example.
    return rest

a()  # ValueError: not enough values to unpack (expected at least 1, got 0)
$ pyright ./pyt/d.py                                                                  
typingsPath /home/kc5m/src/github.com/kc5m/pyt/typings is not a valid directory.
Searching for source files
Found 1 source file
/home/kc5m/src/github.com/kc5m/pyt/pyt/d.py
  4:5 - error: Tuple size mismatch: expected at least 2 entries but got 1 (reportGeneralTypeIssues)
1 error, 0 warnings 
Completed in 0.858sec

$ mypy --strict ./pyt/d.py                                                                        
Success: no issues found in 1 source file
addressed in next version bug

All 2 comments

Thanks for the bug report. This will be fixed in the next published version of pyright.

This is now fixed in version 1.1.33, which I just published.

Was this page helpful?
0 / 5 - 0 ratings