Skip to content

Custom __iter__ method yields false positive when used with star args (*) #14470

@flaeppe

Description

@flaeppe

Bug Report

Custom implemented __iter__ method on class mismatches when passing positional arguments with asterisk(*)

To Reproduce

from typing import Iterator

class X:
    def __iter__(self) -> Iterator[str]:
        return iter([""])


def func(*args: int) -> None:
    return None

        
func(*X())  # OK (false positive, should have incompatible type error)
func(*X().__iter__())  # 13: error: Argument 1 to "func" has incompatible type "*Iterator[str]"; expected "int"  [arg-type]

Expected Behavior

Mypy should yield an error when passing positional parameters with asterisk(*) that do not match the expected type.

In this case it should complain on the function call on line 12, as it calls func with a mismatching type of iterable.

Actual Behavior

Passing iterator with only * yields no error.

Your Environment

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions