Skip to content

typing._GenericAlias objects allow arbitrary dunder attribute creation #136731

@JelleZijlstra

Description

@JelleZijlstra

Bug report

Bug description:

Objects like typing.List[int] allow arbitrary dunders to be set, and because of the cache, those attributes can leak to other objects created elsewhere in the program:

>>> x = List[int]
>>> x.__whatever__ = 42
>>> List[int].__whatever__
42

The cache behavior seems like it could lead to undesirable behavior. In 3.14 we somewhat accidentally fixed a similar bug with Union objects, and it turns out some people were relying on this: #132139. Let's think about whether we also want to remove this wart for other typing objects in 3.15.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtopic-typingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions