Skip to content

No way to express fixed-size heterogeneous iterator/sequence w/o extending tuple #2480

@ms-pablo-ruiz

Description

@ms-pablo-ruiz

Describe the Bug

Iter in python is used both for implementing iterations as in loops or as for defining the unpacking of the class, but doesn't seems to produce correct types, and fails in a somewhat strange manner.

class DummyIter:
    def __iter__(self) -> tuple[int, str]:
        return 1, "s"

a,b = DummyIter() # Type `DummyIter` is not iterable
# but this is valid in python and works, 
print(a)  # 1 
print(b) # "s"

The real code that made me realize this was a bit more confusing as makes you believe that you are iterating the element in the loop instead of the list:

my_list = [DummyIter(), DummyIter()]
for i, j in my_list:  # Type `DummyIter` is not iterable
    # but this is valid in python and works:
    print(i) # 1
    print(j) # "s"

In this case DummyIter is a iterable as far as i know even if is only iterable for unpacking porpoises

For tuples / NamedTuples this does work and inheriting from tuple can make it work, but that solution has its own drawbacks.

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEmqALqgMZSpxzx0Q3G5KTBszYcuAHXRT2nOHQAiAVxo1SASSYxKiKXX0MYYOgH0TELZTMAKblDABKOgFoAfHSZLisANoR0TAA0dHBMlAC6uugGMXSUMJ6U0QCMwRIgcOlSUqiB2HQAvIoqapra1g5SxJT%2BTNaolejVtdbYjdnoaiZQEKGFdD7KqhqWFcFDpaMO4VJggrzBAFa80V09oVExzQHWEI1bNTuL7dLoIIEgSkzQcCTkiCAAxHQAqtc9TKR0YErorNe4dCZU6YIzfQQ0ZgmdAqbDlfCIFZMJxuEJhTYGeKJaJgdIAOVh2kRwHwAF8smcLmR4mAoKRCExaFAKM8AAqkGl0kIYHAEOisQGQADmSkozAggMIUmeAGUYDA6AALJhMYhwRAAeg11KMdMIgiFGpg6A1mFwrDgGoF6GFovFgI14ModFQADdUNBUNhYPzBRARWKAdFcMQg7cqqQmIrAc5Xdo4BLokV0gBmQjJABMFJApIubGucYAYtAYBQ0Fg8EQyDmgA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions