Skip to content

type inference failure on custom iterable class unpacking #20435

@danmur97

Description

@danmur97

Bug Report

When unpacking a custom iterable class to build another iterable, mypy does not correctly infer the resulting type i.e. CustomIter[CustomIter[_T]] is equal to CustomIter[_T] (that should not be true)

To Reproduce

proof-of-concept

Expected Behavior

Nested iterable unpacking type checking works fine when using standard types (e.g. list[list[_T]]) however custom iterable classes should have the same behavior.

Actual Behavior

As presented on the playground proof-of-concept the issue is that mypy does not mark the failure on this incorrectly defined function:

def case_1(iter_of_iter: CustomIter[list[int]]) -> CustomIter[int]:
    # Mypy fails
    #
    # Mypy does not flag that the return type does not match.
    # It is `CustomIter[list[int]]` instead of `CustomIter[int]`
    return CustomIter.new(*iter_of_iter)

Your Environment

  • details available on the playground example.

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