Are there types for built-in iterators

In python we have the types, collections.abc, and typing module for various typing. But is it unreasonable for me to wonder why we don’t have for example type(iter(range(1))) or type(iter(memoryview(bytearray())))? Has this been discussed or is this simply not desired as a feature in python? Thank you.

Also, I’m new here and don’t have a lot of experience or knowledge on the python philosophy of typing in python.

I can’t see an advantage in typing either of those specifically rather than simply typing as Iterator[int]? What are you hoping for?
(Iterator is imported from collections.abc)

I was doing instance based checks and needed a memory_iterator type. It’s easy enough to do it myself but I was just curious if this is something people actually wanted or was missing.

For duck typing, the behaviour (and not the actual implementation) is important.
Does it suffice if the instance can iterate and yields ints? Or does your code differentiate between a range iterator and a memory view iterator?

If no difference, you can have wider applicable logic by using a generic test for Iterator.
If there is a difference, you may want to use Protocol to test for the minimal set of aspects / attributes / methods that differ and matter for your code.

Ah, that makes sense. Thank you.

Why do you need to test for an iterator?

In my experience if you find yourself doing this then something went wrong somewhere.

Do you really need to test for an iterator rather than an iterable? I also recommend not testing for an iterable but requiring an iterator specifically is unusual.

Do you need to test for an iterator because you need an iterator rather than a non-iterator iterable? If so then it is usually better to use iter which converts any iterable into an iterator.

Do you have a function with a signature like:

def func(x: int | Iterator[int]):
    if isinstance(x, Iterator):
        # do one thing
    else:
        # do other thing

If so then I recommend not having a function with that signature: it should just be two different functions. I have many times found myself needing to rewrite a function while preserving this sort of signature for compatibility and it is clear to me that it is a mistake to have a function with this signature in the first place.