mypy icon indicating copy to clipboard operation
mypy copied to clipboard

list unpacking: List or tuple expected as variadic arguments

Open giodall opened this issue 2 years ago • 3 comments

Bug Report

Hello, I'm experiencing an error while unpacking lists with union type. the problem is not occuring for a flat list (see simple exampe bellow)

To Reproduce

# Ideally, a small sample program that demonstrates the problem.
# Or even better, a reproducible playground link https://mypy-play.net/ (use the "Gist" button)
class A:
    pass


EMPTY_LIST: list[str | A] = []

a: list[str | A] = ["a", "b"] if "a" == "b" else EMPTY_LIST
""" no error """
b: list[str | A] = [
    *(["a", "b"] if "a" == "b" else EMPTY_LIST),
]
""" error : List or tuple expected as variadic arguments """

Expected Behavior

No error, expression must be valid

Actual Behavior

error : List or tuple expected as variadic arguments (this is a function args error, nothing to do with lists)

Your Environment

  • Mypy version used: 0.971
  • Mypy command-line flags: mypy src
  • Mypy configuration options from mypy.ini (and other config files): [tool.mypy] ignore_missing_imports = "True" plugins = ["sqlalchemy.ext.mypy.plugin"]
  • Python version used: 3.10

giodall avatar Mar 13 '23 14:03 giodall

Hello! I have a similar problem with a tuple

To Reproduce

class A:

    def __init__(self, value1: str, value2: str) -> None:
        self.value1 = value1
        self.value2 = value2

    def __iter__(self) -> tuple[str, str]:
        return self.value1, self.value2


def function(by: str, value: str):
    ...

a = A(value1="ID", value2="42")
function(*a)

And after run mypy we will get:

15: error: List or tuple expected as variadic arguments  [misc]

M1troll avatar Sep 11 '23 05:09 M1troll

@M1troll, I think mypy is correct to flag an error in your code sample. The __iter__ method must return an iterator, but your code returns a tuple. If you run this code sample, it crashes (TypeError: iter() returned non-iterator of type 'tuple').

erictraut avatar Sep 11 '23 06:09 erictraut

Have the same trouble.

thunderamur avatar Jun 28 '24 14:06 thunderamur

This now typechecks without errors (it would however infer imprecise type without annotation, I will open a separate issue for this).

ilevkivskyi avatar Feb 10 '25 21:02 ilevkivskyi