Title: Add `module` keyword argument to `typing.TypeAliasType` · Issue #149132 · python/cpython · GitHub
Open Graph Title: Add `module` keyword argument to `typing.TypeAliasType` · Issue #149132 · python/cpython
X Title: Add `module` keyword argument to `typing.TypeAliasType` · Issue #149132 · python/cpython
Description: Feature or enhancement Proposal: Add an optional keyword-only module argument to typing.TypeAliasType, so that callers can override the __module__ attribute at construction time instead of relying on frame introspection. Note that while ...
Open Graph Description: Feature or enhancement Proposal: Add an optional keyword-only module argument to typing.TypeAliasType, so that callers can override the __module__ attribute at construction time instead of relying ...
X Description: Feature or enhancement Proposal: Add an optional keyword-only module argument to typing.TypeAliasType, so that callers can override the __module__ attribute at construction time instead of relying ...
Opengraph URL: https://github.com/python/cpython/issues/149132
X: @github
Domain: redirect.github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add `module` keyword argument to `typing.TypeAliasType`","articleBody":"# Feature or enhancement\n\n### Proposal:\n\nAdd an optional keyword-only `module` argument to `typing.TypeAliasType`, so that callers can override the `__module__` attribute at construction time instead of relying on frame introspection.\n\nNote that while I have posted this idea on Discourse (see linked thread), I haven't gotten any replies. I optimistically take this as the proposal being minor and uncontroversial.\n\n### Motivation\n\n`typing.TypeAliasType` currently derives `__module__` from a [frame walk](https://github.com/python/cpython/blob/d71e3bc5a0f2f7af407eb32a8a5b36acf66070aa/Objects/typevarobject.c#L391). This is unsuitable when constructing `TypeAliasType` objects inside `exec`, from C, or with a factory helper. In such cases `caller()` returns `None` or the wrong module.\n\nThere is currently no way to fix this after the fact, because `__module__` is read-only:\n\n```python\n\u003e\u003e\u003e from typing import TypeAliasType\n\u003e\u003e\u003e ta = TypeAliasType(\"A\", int)\n\u003e\u003e\u003e ta.__module__ = \"x\"\nAttributeError: attribute '__module__' of 'typing.TypeAliasType' objects is not writable\n```\n\nMeanwhile there are consumers that rely on `__module__`, such as IDE go-to-definition or introspection-based stub generation (e.g. with `f\"{alias.__module__}.{alias.__name__}\"`). Such tools have no good recourse when `__module__` is wrong.\n\nAdding a `__module__` kwarg to the `TypeAliasType` constructor is an easy, backward-compatible way to address this. \n`collections.namedtuple` and `enum.Enum` expose such a kwarg and thus serve as precedent. Admittedly, the motivations for this kwarg in [`namedtuple`](https://github.com/python/cpython/blob/d71e3bc5a0f2f7af407eb32a8a5b36acf66070aa/Lib/collections/__init__.py#L517-L521) and [`Enum`](https://github.com/python/cpython/blob/d71e3bc5a0f2f7af407eb32a8a5b36acf66070aa/Lib/enum.py#L718-L720) seem to have more to do with pickling, which is not a use-case of especial import for `TypeAliasType` (though certainly one _can_ pickle a `TypeAliasType`).\n\n### API\n\nThe new `TypeAliasType` constructor signature would be\n\n```python\nTypeAliasType(name, value, *, type_params=(), qualname=None, module=None)\n```\n\n`module=None` (default or explicit) preserves today's behavior. Any other value is stored verbatim with no validation, matching the `namedtuple` and `enum` APIs. Note that I don't object to adding validation here, but figured I'd start off by proposing something most consistent with the precedential APIs.\n\n### Has this already been discussed elsewhere?\n\nI have already discussed this feature proposal on Discourse\n\n### Links to previous discussion of this feature:\n\nhttps://discuss.python.org/t/adding-a-module-keyword-argument-to-typing-typealiastype/107087\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-149133\n* https://github.com/python/typing_extensions/pull/149133\n\u003c!-- /gh-linked-prs --\u003e","author":{"url":"https://github.com/ericmarkmartin","@type":"Person","name":"ericmarkmartin"},"datePublished":"2026-04-29T05:20:11.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":7},"url":"https://github.com/149132/cpython/issues/149132"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:44e642bc-9813-97b4-3841-0006d6f8e691 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AD0A:28B50A:3450265:3523AA3:6A4F0580 |
| html-safe-nonce | aa3dd6158c31db702a067c02970cc98468fbb3580d91b0f6c90bd49a7f1e5cf1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRDBBOjI4QjUwQTozNDUwMjY1OjM1MjNBQTM6NkE0RjA1ODAiLCJ2aXNpdG9yX2lkIjoiMzEyMTkwNzUyNDc0OTQyODA5NiIsInJlZ2lvbl9lZGdlIjoic2VhIiwicmVnaW9uX3JlbmRlciI6InNlYSJ9 |
| visitor-hmac | 4f27cea3ee3addfd1465ec3205a616b05937f3438842fb42af7f73e486f93e4e |
| hovercard-subject-tag | issue:4348106875 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/149132/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ebe62ebe8bf844a7880afb1e88aa2eb81559d9c5eff79946664ad17d6384ff44/python/cpython/issues/149132 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ebe62ebe8bf844a7880afb1e88aa2eb81559d9c5eff79946664ad17d6384ff44/python/cpython/issues/149132 |
| og:image:alt | Feature or enhancement Proposal: Add an optional keyword-only module argument to typing.TypeAliasType, so that callers can override the __module__ attribute at construction time instead of relying ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ericmarkmartin |
| hostname | github.com |
| expected-hostname | github.com |
| None | b92d11c0aa4a77d54ef4af1078b6a15fb5a70a215b30c4ecf28889d5a8e656d9 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 2b8f23afb982271f1b22258a94aede67a6b77760 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width