Skip to content

Allow iter[T] as builtin generic for typing.Iterator[T] (PEP 585) #102465

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
randolf-scholz opened this issue Mar 6, 2023 · 5 comments
Closed
Labels
topic-typing type-feature A feature request or enhancement

Comments

@randolf-scholz
Copy link
Contributor

Feature or enhancement

PEP 585 allows us to use set, list, dict, tuple, etc. instead of the typing.Dict, ... analogues.

Pitch

Amend PEP 585 to allow iter[T] to act as a builtin substitute for typing.Iterator[T].

@randolf-scholz randolf-scholz added the type-feature A feature request or enhancement label Mar 6, 2023
@sobolevn
Copy link
Member

sobolevn commented Mar 6, 2023

I think that iter is different from dict / list / etc, because iter is a function, not a class.

As far as I know, we don't have any generic functions at the moment.

@Fidget-Spinner
Copy link
Member

Fidget-Spinner commented Mar 6, 2023

Agree with Nikita. Someone previously suggested callable[args, ret] as a substitute for collections.abc.Callable/typing.Callable too and we rejected it. See the reasoning at #86268.

@AlexWaygood
Copy link
Member

I'm also not a fan of this idea. iter is a function that produces instances of collections.abc.Iterator, but it isn't the same thing as collections.abc.Iterator. Overloading it more like this would just lead to confusion, in my opinion.

@AlexWaygood AlexWaygood added the pending The issue will be closed if no feedback is provided label Mar 6, 2023
@sobolevn sobolevn closed this as not planned Won't fix, can't repro, duplicate, stale Mar 11, 2023
@rainwoodman
Copy link

Interesting discussion. I came across this from #86268 while relearning type hints post typing module deprecation,.

One difference I could appreciate is that unlike dict/list, collection.abc.Iterator is not a concrete class, and thus should not be instantiated; hence iter() cannot be a construtor for the class (and thus currently it is not); it is something that always returns a subclass of Iterator.

The confusion I can construct is when we try to annotate the iter function itself after this proposal:

def iter[T](obj : T) -> abc.Iterator[getElementType(T)]

becomes

type iter[E] = abc.Iterator[E]
def iter[T](obj: T) -> iter[getElementType(T)]

This does look strange,(and the lack of facility for getElementType(T) makes it worse) but the two use of [] are different: the first [T] is a type parameter, and the second [] an object type hint, and we most likely already make that distinction for other use of type parameters.

Is this a severe enough confusion, or are there more severe confusions beyond this one?

@picnixz picnixz removed the pending The issue will be closed if no feedback is provided label Dec 14, 2024
@JelleZijlstra
Copy link
Member

I have some discussion of this idea at https://jellezijlstra.github.io/why-cant-we#builtin-functions-as-type-expressions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-typing type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

7 participants