facebook / facebook/pyrefly

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

Open
#2,480 7 comments 0 reactions 0 assignees View on GitHub
typechecking typing-spec
Dominant language
Rust
Stars
7k
Forks
519
PR merge metrics
No merged PRs in 30d

Description

### 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.

```python
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:

```python
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_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.