Title: `typing.get_type_hints()` raises on invalid types only if they're wrapped in a ForwardRef · Issue #133959 · python/cpython · GitHub
Open Graph Title: `typing.get_type_hints()` raises on invalid types only if they're wrapped in a ForwardRef · Issue #133959 · python/cpython
X Title: `typing.get_type_hints()` raises on invalid types only if they're wrapped in a ForwardRef · Issue #133959 · python/cpython
Description: >>> import typing >>> def f(x: typing.ClassVar[int]): pass ... >>> typing.get_type_hints(f) {'x': typing.ClassVar[int]} >>> def f(x: "typing.ClassVar[int]"): pass ... >>> typing.get_type_hints(f) Traceback (most recent call last): File "...
Open Graph Description: >>> import typing >>> def f(x: typing.ClassVar[int]): pass ... >>> typing.get_type_hints(f) {'x': typing.ClassVar[int]} >>> def f(x: "typing.ClassVar[int]"): pass ... >>> typing.get_type_hints(f) T...
X Description: >>> import typing >>> def f(x: typing.ClassVar[int]): pass ... >>> typing.get_type_hints(f) {'x': typing.ClassVar[int]} >>> def f(x: "typing.ClassVa...
Opengraph URL: https://github.com/python/cpython/issues/133959
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`typing.get_type_hints()` raises on invalid types only if they're wrapped in a ForwardRef","articleBody":"```pytb\n>>> import typing\n>>> def f(x: typing.ClassVar[int]): pass\n... \n>>> typing.get_type_hints(f)\n{'x': typing.ClassVar[int]}\n>>> def f(x: \"typing.ClassVar[int]\"): pass\n... \n>>> typing.get_type_hints(f)\nTraceback (most recent call last):\n File \"\u003cpython-input-4>\", line 1, in \u003cmodule>\n typing.get_type_hints(f)\n ~~~~~~~~~~~~~~~~~~~~~^^^\n File \"/Users/jelle/py/cpython/Lib/typing.py\", line 2390, in get_type_hints\n hints[name] = _eval_type(value, globalns, localns, type_params, format=format, owner=obj)\n ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/Users/jelle/py/cpython/Lib/typing.py\", line 448, in _eval_type\n return evaluate_forward_ref(t, globals=globalns, locals=localns,\n type_params=type_params, owner=owner,\n _recursive_guard=recursive_guard, format=format)\n File \"/Users/jelle/py/cpython/Lib/typing.py\", line 993, in evaluate_forward_ref\n type_ = _type_check(\n value,\n ...\u003c2 lines>...\n allow_special_forms=forward_ref.__forward_is_class__,\n )\n File \"/Users/jelle/py/cpython/Lib/typing.py\", line 204, in _type_check\n raise TypeError(f\"{arg} is not valid as type argument\")\nTypeError: typing.ClassVar[int] is not valid as type argument\n```\n\n(On main but also reproduced on 3.13.)\n\nThis seems inconsistent and I don't think it's helpful for users. We should either raise in both cases or neither. I prefer to not raise in either case, because it's less disruptive for callers (they already have to deal with `ClassVar` etc. potentially appearing in annotations), and provides more information.","author":{"url":"https://github.com/JelleZijlstra","@type":"Person","name":"JelleZijlstra"},"datePublished":"2025-05-13T02:01:38Z","url":"https://github.com/python/cpython/issues/133959"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:fe4f6ae3-5ea4-3523-6d1a-00982448e702 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9FD4:3E7030:346C4A:46BCA7:6B0028D2 |
| html-safe-nonce | 1e0e759d730611e5dc6acaa67b698d2b92977ef40361acb0dcce7941f451e753 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RkQ0OjNFNzAzMDozNDZDNEE6NDZCQ0E3OjZCMDAyOEQyIiwidmlzaXRvcl9pZCI6IjU3MTEwMzk4OTgxNDQ3NDU2ODIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 82e556e389406f9eea2c33c3b888ccb5394bae9ed398e2b7b5694972c4f5cf8f |
| hovercard-subject-tag | issue:3058558899 |
| 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/133959/issue_layout |
| twitter:image | https://opengraph.githubassets.com/10796bd2b62b326116c8abfd117eb64f184128c51328e0945c61dfb9cf65cad0/python/cpython/issues/133959 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/10796bd2b62b326116c8abfd117eb64f184128c51328e0945c61dfb9cf65cad0/python/cpython/issues/133959 |
| og:image:alt | >>> import typing >>> def f(x: typing.ClassVar[int]): pass ... >>> typing.get_type_hints(f) {'x': typing.ClassVar[int]} >>> def f(x: "typing.ClassVar[int]"): pass ... >>> typing.get_type_hints(f) T... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JelleZijlstra |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| 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 | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width