Title: 2.2 Regression with placeholder types in a TypeVar · Issue #21640 · python/mypy · GitHub
Open Graph Title: 2.2 Regression with placeholder types in a TypeVar · Issue #21640 · python/mypy
X Title: 2.2 Regression with placeholder types in a TypeVar · Issue #21640 · python/mypy
Description: Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub.
Open Graph Description: Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub.
X Description: Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub.
Opengraph URL: https://github.com/python/mypy
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"2.2 Regression with placeholder types in a TypeVar","articleBody":"**Crash Report**\n\nI was testing the [mypy 2.2 branch](https://github.com/python/mypy/issues/21618#issuecomment-4784328932) and I came across some errors around placeholder types.\n\n**Traceback**\n\nThere's kinda two errors and I think they're related\n\n\u003cdetails>\n\u003csummary>AssertionError: Must not defer during final iteration\u003c/summary>\n\n```\nPlease report a bug at https://github.com/python/mypy/issues\nversion: 2.2.0+dev.5ef090270579013c337a9bcc79e2c58417f09538\nTraceback (most recent call last):\n File \"\u003cfrozen runpy>\", line 198, in _run_module_as_main\n File \"\u003cfrozen runpy>\", line 88, in _run_code\n File \"mypy/semanal.py\", line 7732, in accept\n File \"mypy/nodes.py\", line 1165, in accept\n File \"mypy/semanal.py\", line 991, in visit_func_def\n File \"mypy/semanal.py\", line 1036, in analyze_func_def\n File \"mypy/semanal.py\", line 7333, in defer\nAssertionError: Must not defer during final iteration\n```\n\u003c/details>\n\nIf I use the source directly so I get more of a traceback\n\n\u003cdetails>\n\u003csummary>More details\u003c/summary>\n\n```\nversion: 2.2.0+dev.5ef090270579013c337a9bcc79e2c58417f09538\nTraceback (most recent call last):\n File \"\u003cfrozen runpy>\", line 198, in _run_module_as_main\n File \"\u003cfrozen runpy>\", line 88, in _run_code\n File \"/Users/stephen.moore/Projects/external/mypy/mypy/__main__.py\", line 52, in \u003cmodule>\n console_entry()\n File \"/Users/stephen.moore/Projects/external/mypy/mypy/__main__.py\", line 16, in console_entry\n main()\n File \"/Users/stephen.moore/Projects/external/mypy/mypy/main.py\", line 154, in main\n res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)\n File \"/Users/stephen.moore/Projects/external/mypy/mypy/main.py\", line 244, in run_build\n res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)\n File \"/Users/stephen.moore/Projects/external/mypy/mypy/build.py\", line 422, in build\n result = build_inner(\n File \"/Users/stephen.moore/Projects/external/mypy/mypy/build.py\", line 537, in build_inner\n graph = dispatch(sources, manager, stdout, connect_threads)\n File \"/Users/stephen.moore/Projects/external/mypy/mypy/build.py\", line 4146, in dispatch\n process_graph(graph, manager)\n File \"/Users/stephen.moore/Projects/external/mypy/mypy/build.py\", line 4614, in process_graph\n done, still_working, results = manager.wait_for_done(graph)\n File \"/Users/stephen.moore/Projects/external/mypy/mypy/build.py\", line 1484, in wait_for_done\n process_stale_scc(graph, next_scc, self)\n File \"/Users/stephen.moore/Projects/external/mypy/mypy/build.py\", line 4782, in process_stale_scc\n mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)\n File \"/Users/stephen.moore/Projects/external/mypy/mypy/semanal_main.py\", line 92, in semantic_analysis_for_scc\n process_functions(graph, scc, patches)\n File \"/Users/stephen.moore/Projects/external/mypy/mypy/semanal_main.py\", line 275, in process_functions\n process_top_level_function(\n File \"/Users/stephen.moore/Projects/external/mypy/mypy/semanal_main.py\", line 314, in process_top_level_function\n deferred, incomplete, progress = semantic_analyze_target(\n File \"/Users/stephen.moore/Projects/external/mypy/mypy/semanal_main.py\", line 380, in semantic_analyze_target\n analyzer.refresh_partial(\n File \"/Users/stephen.moore/Projects/external/mypy/mypy/semanal.py\", line 709, in refresh_partial\n self.accept(node)\n File \"/Users/stephen.moore/Projects/external/mypy/mypy/semanal.py\", line 7732, in accept\n node.accept(self)\n ~~~~~~~~~~~^^^^^^\n File \"/Users/stephen.moore/Projects/external/mypy/mypy/nodes.py\", line 1165, in accept\n return visitor.visit_func_def(self)\n ~~~~~~~~~~~~~~~~~~~~~~^^^^^^\n File \"/Users/stephen.moore/Projects/external/mypy/mypy/semanal.py\", line 991, in visit_func_def\n self.analyze_func_def(defn)\n ~~~~~~~~~~~~~~~~~~~~~^^^^^^\n File \"/Users/stephen.moore/Projects/external/mypy/mypy/semanal.py\", line 1036, in analyze_func_def\n self.defer(defn)\n ~~~~~~~~~~^^^^^^\n File \"/Users/stephen.moore/Projects/external/mypy/mypy/semanal.py\", line 7333, in defer\n assert not self.final_iteration, \"Must not defer during final iteration\"\n ^^^^^^^^^^^^^^^^^^^^^^^^\nAssertionError: Must not defer during final iteration\n```\n\u003c/details>\n\nAnd if I start trying to fill in default values I start to get this happening:\n\n\u003cdetails>\n\u003csummary>NotImplementedError: Cannot serialize PlaceholderType instance\u003c/summary>\n\n```\nTraceback (most recent call last):\n File \"\u003cfrozen runpy>\", line 198, in _run_module_as_main\n File \"\u003cfrozen runpy>\", line 88, in _run_code\n File \"/Users/stephen.moore/.virtualenvs/project/lib/python3.13/site-packages/mypy/__main__.py\", line 52, in \u003cmodule>\n console_entry()\n File \"/Users/stephen.moore/.virtualenvs/project/lib/python3.13/site-packages/mypy/__main__.py\", line 16, in console_entry\n main()\n ~~~~^^\n File \"/Users/stephen.moore/.virtualenvs/project/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 ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/Users/stephen.moore/.virtualenvs/project/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 \"/Users/stephen.moore/.virtualenvs/project/lib/python3.13/site-packages/mypy/build.py\", line 422, in build\n result = build_inner(\n sources,\n ...\u003c9 lines>...\n metastore,\n )\n File \"/Users/stephen.moore/.virtualenvs/project/lib/python3.13/site-packages/mypy/build.py\", line 537, in build_inner\n graph = dispatch(sources, manager, stdout, connect_threads)\n File \"/Users/stephen.moore/.virtualenvs/project/lib/python3.13/site-packages/mypy/build.py\", line 4146, in dispatch\n process_graph(graph, manager)\n ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^\n File \"/Users/stephen.moore/.virtualenvs/project/lib/python3.13/site-packages/mypy/build.py\", line 4614, in process_graph\n done, still_working, results = manager.wait_for_done(graph)\n ~~~~~~~~~~~~~~~~~~~~~^^^^^^^\n File \"/Users/stephen.moore/.virtualenvs/project/lib/python3.13/site-packages/mypy/build.py\", line 1484, in wait_for_done\n process_stale_scc(graph, next_scc, self)\n ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^\n File \"/Users/stephen.moore/.virtualenvs/project/lib/python3.13/site-packages/mypy/build.py\", line 4821, in process_stale_scc\n meta_tuple = graph[id].write_cache()\n File \"/Users/stephen.moore/.virtualenvs/project/lib/python3.13/site-packages/mypy/build.py\", line 3604, in write_cache\n new_interface_hash, meta_tuple = write_cache(\n ~~~~~~~~~~~^\n self.id,\n ^^^^^^^^\n ...\u003c13 lines>...\n self.manager,\n ^^^^^^^^^^^^^\n )\n ^\n File \"/Users/stephen.moore/.virtualenvs/project/lib/python3.13/site-packages/mypy/build.py\", line 2325, in write_cache\n tree.write(data_io)\n ~~~~~~~~~~^^^^^^^^^\n File \"/Users/stephen.moore/.virtualenvs/project/lib/python3.13/site-packages/mypy/nodes.py\", line 599, in write\n self.names.write(data, self._fullname)\n ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^\n File \"/Users/stephen.moore/.virtualenvs/project/lib/python3.13/site-packages/mypy/nodes.py\", line 5169, in write\n value.write(data, fullname, key)\n ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^\n File \"/Users/stephen.moore/.virtualenvs/project/lib/python3.13/site-packages/mypy/nodes.py\", line 5074, in write\n self.node.write(data)\n ~~~~~~~~~~~~~~~^^^^^^\n File \"/Users/stephen.moore/.virtualenvs/project/lib/python3.13/site-packages/mypy/nodes.py\", line 4721, in write\n self.target.write(data)\n ~~~~~~~~~~~~~~~~~^^^^^^\n File \"/Users/stephen.moore/.virtualenvs/project/lib/python3.13/site-packages/mypy/types.py\", line 1756, in write\n write_type_list(data, self.args)\n ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^\n File \"/Users/stephen.moore/.virtualenvs/project/lib/python3.13/site-packages/mypy/types.py\", line 4553, in write_type_list\n item.write(data)\n ~~~~~~~~~~^^^^^^\n File \"/Users/stephen.moore/.virtualenvs/project/lib/python3.13/site-packages/mypy/types.py\", line 318, in write\n raise NotImplementedError(f\"Cannot serialize {self.__class__.__name__} instance\")\nNotImplementedError: Cannot serialize PlaceholderType instance\n```\n\u003c/details>\n\n**To Reproduce**\n\nIt appears that ultimately this is an interaction with how my [mypy plugin](https://github.com/delfick/extended-mypy-django-plugin) works, which complicates it a little.\nEssentially I add dependencies with the `get_additional_deps` hook that contain aliases (I call these virtual dependencies), and then with a `get_type_analyze_hook` hook I look for a special class that I replace with the target of an appropriate alias from those \"virtual dependencies\".\n\nI have created an example with a readme that replicates the problem. Seems I can't upload a git bundle to a github issue, so\nhere is a repo: https://github.com/delfick/mypy-release-2.2-bug-demonstration\n\nIt seems there is one change required to fix it\n\n```patch\nFrom 84140edcb76aaf4b6bf3a2fd1f6a5ffc80277810 Mon Sep 17 00:00:00 2001\nFrom: Stephen Moore \u003cstephen.moore@kraken.tech>\nDate: Wed, 24 Jun 2026 10:13:32 +1000\nSubject: [PATCH] WIP: this makes a difference\n\n---\n mypy/semanal.py | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/mypy/semanal.py b/mypy/semanal.py\nindex e010273b0..324910809 100644\n--- a/mypy/semanal.py\n+++ b/mypy/semanal.py\n@@ -2009,7 +2009,7 @@ class SemanticAnalyzer(\n # re-analyze this class. Note we force progress to handle cases like\n # class C[T = C], this matches logic in process_typevar_parameters()\n # for \"old style\" type variables.\n- self.defer(force_progress=tvar_defs != defn.type_vars)\n+ self.mark_incomplete(defn.name, defn)\n \n self.analyze_class_keywords(defn)\n bases_result = self.analyze_base_classes(defn.name, bases)\n-- \n2.50.1 (Apple Git-155)\n```\n\n(undoing a change in https://github.com/python/mypy/pull/21491)\n\nHowever when I do this mypy unit tests start to fail. I can't figure out how to fix that, or even how to replicate the errors in a mypy unit test.\n\n**Your Environment**\n\n\u003c!-- Include as many relevant details about the environment you experienced the bug in -->\n\n- Mypy version used: release-2.2 branch (5ef090270579013c337a9bcc79e2c58417f09538)\n- Python version used: 3.13.2\n- Operating system and version: MacOS Tahoe 26.5.1\n\nAI Disclosure: None: I actively avoid LLMs\n","author":{"url":"https://github.com/delfick","@type":"Person","name":"delfick"},"datePublished":"2026-06-24T01:56:40Z","url":"https://github.com/python/mypy/issues/21640"}
| 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 | /:user_id/:repository/issues/:id(.:format) |
| route-controller | issues |
| route-action | show |
| fetch-nonce | v2:2323e224-d0ca-fd3b-a94e-0e7bd9e4bb0d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C11C:3B60C0:4074B7:569ACE:6B002AA8 |
| html-safe-nonce | 501d7bee8acc8ce5125033acc0b0d63a69ae83efbf85d32b86b4f316abcae446 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMTFDOjNCNjBDMDo0MDc0Qjc6NTY5QUNFOjZCMDAyQUE4IiwidmlzaXRvcl9pZCI6IjY5MTA2MTAwNjA0NjA2OTgyODAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e72d4723cc3c1ee46e825ed97d05eb4df31583526fe854714177270dec57727f |
| hovercard-subject-tag | repository:7053637 |
| 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/python/mypy/issues/21640 |
| twitter:image | https://opengraph.githubassets.com/f79ae5636353a0db17350af0a02700f2e9c3bbcf096f9450bf0c369c77cd1022/python/mypy |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f79ae5636353a0db17350af0a02700f2e9c3bbcf096f9450bf0c369c77cd1022/python/mypy |
| og:image:alt | Optional static typing for Python. Contribute to python/mypy development by creating an account on GitHub. |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| turbo-cache-control | no-cache |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width