python / python/mypy

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

Open
#14,470 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the provided Python snippet with mypy to reproduce the missing error for func(*X()). Then trace handling of starred positional arguments and add a regression test covering a custom iter method; done means the first call reports an incompatible iterable element type like the explicit iterator call.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.