Title: "INTERNAL_ERROR": generic fun w/ Callable param w/ constrained return type followed by `*args` typed as `*Tuple[int, float]` crashes mypy · Issue #21907 · python/mypy · GitHub
Open Graph Title: "INTERNAL_ERROR": generic fun w/ Callable param w/ constrained return type followed by `*args` typed as `*Tuple[int, float]` crashes mypy · Issue #21907 · python/mypy
X Title: "INTERNAL_ERROR": generic fun w/ Callable param w/ constrained return type followed by `*args` typed as `*Tuple[int, float]` crashes mypy · Issue #21907 · python/mypy
Description: Crash Report Create crash_example.py as follows: from typing import * # mypy latest from master branch crashes (mypy 2.4.0+dev.b974556f84bd4ff5280b66bce1413fa942d1261e) (and earlier prod releases as well) def generic_fun[ TypeVarWithCons...
Open Graph Description: Crash Report Create crash_example.py as follows: from typing import * # mypy latest from master branch crashes (mypy 2.4.0+dev.b974556f84bd4ff5280b66bce1413fa942d1261e) (and earlier prod releases a...
X Description: Crash Report Create crash_example.py as follows: from typing import * # mypy latest from master branch crashes (mypy 2.4.0+dev.b974556f84bd4ff5280b66bce1413fa942d1261e) (and earlier prod releases a...
Opengraph URL: https://github.com/python/mypy/issues/21907
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"\"INTERNAL_ERROR\": generic fun w/ Callable param w/ constrained return type followed by `*args` typed as `*Tuple[int, float]` crashes mypy","articleBody":"**Crash Report**\n\n- Create crash_example.py as follows:\n```\nfrom typing import *\n\n# mypy latest from master branch crashes (mypy 2.4.0+dev.b974556f84bd4ff5280b66bce1413fa942d1261e) (and earlier prod releases as well)\ndef generic_fun[\n TypeVarWithConstraints: (str, bytes),\n](\n a: Callable[..., TypeVarWithConstraints],\n *args: *Tuple[int, float]\n): pass\n```\n- run mypy 2.3.1 or latest master on Github (2.4.0+dev) and observe a crash with \"INTERNAL_ERROR\":\n```\n[liveuser@localhost-live ~]$ mypy crash_example.py --show-traceback\ncrash_example.py:4: error: INTERNAL ERROR -- Please try using mypy master on GitHub:\nhttps://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build\nPlease report a bug at https://github.com/python/mypy/issues\nversion: 2.4.0+dev.b974556f84bd4ff5280b66bce1413fa942d1261e\n[--show-traceback output is included further down]\n```\n\n---\n\n\n**Traceback**\n\n```python-traceback\nTraceback (most recent call last):\n File \"/home/liveuser/.local/bin/mypy\", line 8, in \u003cmodule>\n sys.exit(console_entry())\n File \"/home/liveuser/.local/lib/python3.13/site-packages/mypy/__main__.py\", line 16, in console_entry\n main()\n File \"/home/liveuser/.local/lib/python3.13/site-packages/mypy/main.py\", line 154, in main\n res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)\n File \"/home/liveuser/.local/lib/python3.13/site-packages/mypy/main.py\", line 244, in run_build\n res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)\n File \"/home/liveuser/.local/lib/python3.13/site-packages/mypy/build.py\", line 422, in build\n result = build_inner(\n File \"/home/liveuser/.local/lib/python3.13/site-packages/mypy/build.py\", line 537, in build_inner\n graph = dispatch(sources, manager, stdout, connect_threads)\n File \"/home/liveuser/.local/lib/python3.13/site-packages/mypy/build.py\", line 4150, in dispatch\n process_graph(graph, manager)\n File \"/home/liveuser/.local/lib/python3.13/site-packages/mypy/build.py\", line 4618, in process_graph\n done, still_working, results = manager.wait_for_done(graph)\n File \"/home/liveuser/.local/lib/python3.13/site-packages/mypy/build.py\", line 1484, in wait_for_done\n process_stale_scc(graph, next_scc, self)\n File \"/home/liveuser/.local/lib/python3.13/site-packages/mypy/build.py\", line 4793, in process_stale_scc\n graph[id].type_check_first_pass()\n File \"/home/liveuser/.local/lib/python3.13/site-packages/mypy/build.py\", line 3410, in type_check_first_pass\n self.type_checker().check_first_pass(recurse_into_functions=recurse_into_functions)\n File \"/home/liveuser/.local/lib/python3.13/site-packages/mypy/checker.py\", line 632, in check_first_pass\n self.accept(d)\n File \"/home/liveuser/.local/lib/python3.13/site-packages/mypy/checker.py\", line 779, in accept\n stmt.accept(self)\n ~~~~~~~~~~~^^^^^^\n File \"/home/liveuser/.local/lib/python3.13/site-packages/mypy/nodes.py\", line 1165, in accept\n return visitor.visit_func_def(self)\n ~~~~~~~~~~~~~~~~~~~~~~^^^^^^\n File \"/home/liveuser/.local/lib/python3.13/site-packages/mypy/checker.py\", line 1346, in visit_func_def\n self.visit_func_def_impl(defn)\n ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^\n File \"/home/liveuser/.local/lib/python3.13/site-packages/mypy/checker.py\", line 1350, in visit_func_def_impl\n self.check_func_item(defn, name=defn.name)\n ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^\n File \"/home/liveuser/.local/lib/python3.13/site-packages/mypy/checker.py\", line 1383, in check_func_item\n self.check_func_def(defn, typ, name, allow_empty)\n ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/home/liveuser/.local/lib/python3.13/site-packages/mypy/checker.py\", line 1514, in check_func_def\n store_argument_type(item, i, typ, self.named_generic_type)\n ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/home/liveuser/.local/lib/python3.13/site-packages/mypy/types_utils.py\", line 183, in store_argument_type\n defn.arguments[i].variable.type = arg_type\n ~~~~~~~~~~~~~~^^^\nIndexError: list index out of range\n\ncrash_example.py:4: note: use --pdb to drop into pdb\n```\n\n---\n\n**Context/motivation**\n\nIn PEP-0646 ( https://peps.python.org/pep-0646/ ), it is shown how an unpacked Tuple can be used to specify the type of `*args`:\n```\n# example from PEP-0646\ndef foo(*args: *Tuple[int, str]) -> None: pass\n\nfoo(1, \"hello\") # OK\n```\n\nIn https://docs.python.org/3.13/reference/compound_stmts.html#type-parameter-lists\n(separate from the SyntaxError in their `overly_generic` example which I have reported to the python team and am working with them to fix),\nsaying\n> A parenthesized tuple of expressions after the colon indicates a set of constraints (e.g. T: (str, bytes)). Each member of the tuple should be a type (again, this is not enforced at runtime). Constrained type variables can only take on one of the types in the list of constraints.\ne.g.\n```\nfrom typing import *\n\ndef generic_fun_takes_a_callable_that_returns_either_str_or_bytes[\n TypeVarWithConstraints: (str, bytes),\n](a: Callable[..., TypeVarWithConstraints]):\n pass\n\ndef some_fun_returns_str() -> str:\n return 'abc'\n\ndef some_fun_returns_bytes() -> bytes:\n return b'abc'\n\ndef some_fun_returns_int() -> int:\n return 1\n\ngeneric_fun_takes_a_callable_that_returns_either_str_or_bytes(some_fun_returns_str) # ok\n\ngeneric_fun_takes_a_callable_that_returns_either_str_or_bytes(some_fun_returns_bytes) # ok\n\n# not ok:\n# error: Value of type variable \"TypeVarWithConstraints\" of \"generic_fun_takes_a_callable_that_returns_either_str_or_bytes\" cannot be \"int\" [type-var]\n# generic_fun_takes_a_callable_that_returns_either_str_or_bytes(some_fun_returns_int)\n````\n\nSo given \n```\n def generic_fun_takes_a_callable_that_returns_either_str_or_bytes[\n TypeVarWithConstraints: (str, bytes),\n](a: Callable[..., TypeVarWithConstraints]):\n pass\n```\n\nis ok and\n```\n# example from PEP-0646\ndef foo(*args: *Tuple[int, str]) -> None: pass\n```\nis ok,\nwhat about a function that has the arguments of the 1st followed by the arguments of the 2nd example, which both work fine with mypy on their own? (note PEP-0646 gives examples of `*args` being typed by an unpacked tuple even when preceded by other arguments)\n```\ndef generic_fun[\n TypeVarWithConstraints: (str, bytes),\n](\n a: Callable[..., TypeVarWithConstraints],\n *args: *Tuple[int, float]\n): pass\n```\n\nIt turns out that the above crashes mypy unexpectedly in a way that causes the problem to tell me to file a bug, so I am doing so (see traceback towards top of ticket).\n\n---\n\n\n**Your Environment**\n\n- Fedora Linux 42\n- mypy 2.3.1 and mypy 2.4.0+dev.b974556f84bd4ff5280b66bce1413fa942d1261e\n- python 3.13.2 in this case\n\n---\n\nThanks very much!\n\n","author":{"url":"https://github.com/willy-b","@type":"Person","name":"willy-b"},"datePublished":"2026-08-28T23:57:02Z","url":"https://github.com/python/mypy/issues/21907"}
| 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:90e6b722-1505-807e-a2fb-52e4866e9740 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AF1C:4D1C6:2158060:2D598C4:6B00255E |
| html-safe-nonce | d5453f7524764ff29e82c01e38b53155a3ed32787f2e12c63b1e4c315c5a8859 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRjFDOjREMUM2OjIxNTgwNjA6MkQ1OThDNDo2QjAwMjU1RSIsInZpc2l0b3JfaWQiOiI0ODY0MzA0NTI3OTg3Mjc1MTgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 7983296443475344a91dd77510bfa81e7b72b1522dbd652274c4a857866971c2 |
| hovercard-subject-tag | issue:5283294803 |
| 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/mypy/21907/issue_layout |
| twitter:image | https://opengraph.githubassets.com/60883456c603ccee32bdf497b7305b6aab2de13fb965770006a8b3c03797b1b5/python/mypy/issues/21907 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/60883456c603ccee32bdf497b7305b6aab2de13fb965770006a8b3c03797b1b5/python/mypy/issues/21907 |
| og:image:alt | Crash Report Create crash_example.py as follows: from typing import * # mypy latest from master branch crashes (mypy 2.4.0+dev.b974556f84bd4ff5280b66bce1413fa942d1261e) (and earlier prod releases a... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | willy-b |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| 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