Title: `get_type_hints` occurs NameError only if import `__future__.annotations` · Issue #2132 · python/typing · GitHub
Open Graph Title: `get_type_hints` occurs NameError only if import `__future__.annotations` · Issue #2132 · python/typing
X Title: `get_type_hints` occurs NameError only if import `__future__.annotations` · Issue #2132 · python/typing
Description: from typing import get_type_hints class A[T]: def __init__(self, x: T): self.x = x def foo(self): def bar() -> T: return self.x print(get_type_hints(bar)) A[int](5).foo() Output: {'return': T} from __future__ import annotations # Add thi...
Open Graph Description: from typing import get_type_hints class A[T]: def __init__(self, x: T): self.x = x def foo(self): def bar() -> T: return self.x print(get_type_hints(bar)) A[int](5).foo() Output: {'return': T} from...
X Description: from typing import get_type_hints class A[T]: def __init__(self, x: T): self.x = x def foo(self): def bar() -> T: return self.x print(get_type_hints(bar)) A[int](5).foo() Output: {'return...
Opengraph URL: https://github.com/python/typing/issues/2132
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`get_type_hints` occurs NameError only if import `__future__.annotations`","articleBody":"```\nfrom typing import get_type_hints\n\n\nclass A[T]:\n def __init__(self, x: T):\n self.x = x\n\n def foo(self):\n def bar() -\u003e T:\n return self.x\n\n print(get_type_hints(bar))\n\n\nA[int](5).foo()\n```\n\nOutput: `{'return': T}`\n\n```\nfrom __future__ import annotations # Add this\n\nfrom typing import get_type_hints\n\n\nclass A[T]:\n def __init__(self, x: T):\n self.x = x\n\n def foo(self):\n def bar() -\u003e T:\n return self.x\n\n print(get_type_hints(bar))\n\n\nA[int](5).foo()\n```\n\nError:\n\n```\nNameError: name 'T' is not defined\n```\n\nIs this the expected behavior?\n\nAlso, is there a way to make it work if `__future__.annotations` is imported?\n","author":{"url":"https://github.com/ornew","@type":"Person","name":"ornew"},"datePublished":"2025-12-13T14:20:14.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/2132/typing/issues/2132"}
| 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:a63cb64f-41d9-e947-27ed-42c751da4a01 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 81DA:2EB8B3:93A046:CD5714:696F2A0B |
| html-safe-nonce | e325e430659664cfea702b4299a2724e57310e1a471973adeba9a540645ace65 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MURBOjJFQjhCMzo5M0EwNDY6Q0Q1NzE0OjY5NkYyQTBCIiwidmlzaXRvcl9pZCI6IjE0MjQ1OTUwNzU2NTkxNDc3ODciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | a35595ab94044aa2ae0f3c184ec613bf78d103a5c857034d9dc3ef912a87ccfa |
| hovercard-subject-tag | issue:3726016909 |
| 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/typing/2132/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a3c3830066fbcd6ffd4aa40fc5235e1a49c0eebe71a997175178217434a7c5e3/python/typing/issues/2132 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a3c3830066fbcd6ffd4aa40fc5235e1a49c0eebe71a997175178217434a7c5e3/python/typing/issues/2132 |
| og:image:alt | from typing import get_type_hints class A[T]: def __init__(self, x: T): self.x = x def foo(self): def bar() -> T: return self.x print(get_type_hints(bar)) A[int](5).foo() Output: {'return': T} from... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ornew |
| hostname | github.com |
| expected-hostname | github.com |
| None | b278ad162d35332b6de714dfb005de04386c4d92df6475522bef910f491a35ee |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typing git https://github.com/python/typing.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 24604008 |
| octolytics-dimension-repository_nwo | python/typing |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24604008 |
| octolytics-dimension-repository_network_root_nwo | python/typing |
| 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 | 39aed5006635ab6f45e6b77d23e73b08a00272a3 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width