python / python/cpython

`_GenericAlias` caches origin's `__module__`

Open
#123,250 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic-typing type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

When subscripting a class, __module__ gets saved. This prevents fixing it up later:

https://github.com/python/cpython/blob/4c3f0cbeaec0d49212d305618743fabb0e74a696/Lib/typing.py#L1411

from typing import Generic, TypeVar
T = TypeVar("T")

class A(Generic[T]):
  pass

x = A[T]  # this originally was in ... somewhere, but is here for minimization
A.__module__ = "new_module"
assert x.__module__ == "new_module"  # this is going to raise AssertionError

This came up when updating the Sphinx version in trio due to our handling of private/public. We update __module__ for these classes in __init__ (to make Sphinx work, among other things) but this doesn't end up updating any stored _GenericAliass' __module__s.

CPython versions tested on:

3.11

Operating systems tested on:

Windows

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Lib/typing.py at the linked _GenericAlias implementation and reproduce the reported A[T] example on Python 3.11. Trace how module is obtained for the alias, then verify that changing A.module is reflected by the alias without breaking existing typing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.