Skip to content

array.array should support __class_getitem__ #98658

Closed

Description

@JelleZijlstra

The array.array class has been marked as generic in typeshed since 2017 (python/typeshed#1649). We just got a report on mypy (python/mypy#13942) pointing out that the class is not subscriptable at runtime. Just like other generic standard library classes, we should make array.array subscriptable at runtime (PEP-585).

I'll make a PR.

Activity

  1. added a commit that references this issue on Oct 25, 2022
  2. serhiy-storchaka commented on Oct 26, 2022

    @serhiy-storchaka
    Member

    Isn't it a bug in typeshed? This class does not look generic.

  3. JelleZijlstra commented on Oct 27, 2022

    @JelleZijlstra
    MemberAuthor

    It's marked as generic because the "element" type depends on the type code:

    In [50]: a1=array.array('b', [1])
    
    In [51]: a1[0]
    Out[51]: 1
    
    In [52]: a2 = array.array('f', [1])
    
    In [53]: a2[0]
    Out[53]: 1.0
    

    So we'd want a1 to be typed as an array[int] and a2 as an array[float].

  4. serhiy-storchaka commented on Oct 27, 2022

    @serhiy-storchaka
    Member

    It makes sense.

  5. added a commit that references this issue on Aug 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions