Skip to content

Cache types.GenericAlias in getitems #101859

@eltoder

Description

@eltoder

The "old-style" generic classes from the typing module (List, Dict, etc) cached typing._GenericAlias instances retuned from __getitem__:

>>> from typing import List
>>> List[int] is List[int]
True

But when this functionality was ported to built-in types, caching was not implemented:

>>> list[int] is list[int]
False

Since it is very common to subscript the same types with the same arguments, caching will be very beneficial here and will reduce memory footprint of heavily type-annotated modules.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions