python / typeshed

Collection of library stubs for Python, with static types
Other
4.29k stars 1.72k forks source link

tuple.__new__ does not accept class with only __getitem__ method #12094

Closed JonathanPlasse closed 3 months ago

JonathanPlasse commented 3 months ago

Currently the tuple constructor only accepts iterables. https://github.com/python/typeshed/blob/97ccd8985a3b8fd7b888c0653e92f296db6fd56a/stdlib/builtins.pyi#L932-L933 But the tuple constructor can also take in classes that only have the __getitem__ method and is not iterable.

class A:
    def __getitem__(self, key):
        if key >= 3:
            raise IndexError
        return key

print(tuple(A()))  # (0, 1, 2)

A _GetItemIterable could be added to tuple.__new__ signature like for iter. https://github.com/python/typeshed/blob/97ccd8985a3b8fd7b888c0653e92f296db6fd56a/stdlib/builtins.pyi#L1424-L1431

I am open to make a PR to fix it.

AlexWaygood commented 3 months ago

Sort of a duplicate of https://github.com/python/typeshed/issues/7813, which we closed as "wontfix" (see detailed discussion in that issue for why :-)

JonathanPlasse commented 3 months ago

Thank you, I need to improve my issue searching methodology.