Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JonathanPlasse opened this issue Jun 4, 2024 · 2 comments
Closed

Comments

@JonathanPlasse
Copy link
Contributor

Currently the tuple constructor only accepts iterables.

class tuple(Sequence[_T_co]):
def __new__(cls, iterable: Iterable[_T_co] = ..., /) -> Self: ...

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.

typeshed/stdlib/builtins.pyi

Lines 1424 to 1431 in 97ccd89

@overload
def iter(object: SupportsIter[_SupportsNextT], /) -> _SupportsNextT: ...
@overload
def iter(object: _GetItemIterable[_T], /) -> Iterator[_T]: ...
@overload
def iter(object: Callable[[], _T | None], sentinel: None, /) -> Iterator[_T]: ...
@overload
def iter(object: Callable[[], _T], sentinel: object, /) -> Iterator[_T]: ...

I am open to make a PR to fix it.

@AlexWaygood
Copy link
Member

AlexWaygood commented Jun 4, 2024

Sort of a duplicate of #7813, which we closed as "wontfix" (see detailed discussion in that issue for why :-)

@JonathanPlasse
Copy link
Contributor Author

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

@JonathanPlasse JonathanPlasse closed this as not planned Won't fix, can't repro, duplicate, stale Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants