Skip to content

implement __class_getitem__ for some classes in itertools module #112896

@denballakh

Description

@denballakh

Feature or enhancement

Proposal:

Currently only itertools.chain can be subscripted at runtime. I think most of other iterators should be subscriptable too.
Most of these classes are generic in typeshed, so typecheckers thing that subscripting them is fine, which is inconsistent with behaviour at runtime.

With pyright:

import itertools
x = itertools.cycle[str]('abc')
reveal_type(x) # cycle[str]

At runtime:

>>> import itertools as it
>>> it.cycle[int]
  File "<stdin>", line 1, in <module>
TypeError: type 'itertools.cycle' is not subscriptable
>>> it.count[int]
TypeError: type 'itertools.repeat' is not subscriptable
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: type 'itertools.accumulate' is not subscriptable
>>> it.chain[int] # this is the only subscriptable class in itertools module
itertools.chain[int]

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

#108761 and #108761 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions