Skip to content

Unpacking an iterable in a list comprehension leads to the type inferred as list[Any]#15747

@hvenev

Description

@hvenev

Bug Report

When a value of a user-defined class with __iter__ is converted into a list using [*value], the type of the list is inferred as list[Any].

To Reproduce

importtypingclassSpam: def__iter__(self, /) ->typing.Iterator[int]: yield1a=Spam() # list[int]reveal_type(list(a)) # list[int]reveal_type([iforiina]) # list[int]reveal_type([*(iforiina)]) # list[int]reveal_type([*a.__iter__()]) # list[Any] ???reveal_type([*a]) b, =a# intreveal_type(b)

Expected Behavior

The type of [*a] is list[int]

Actual Behavior

example.py:22: note: Revealed type is "builtins.list[Any]" 

Your Environment

  • Mypy version used: 1.4.1
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.11.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions