Title: extremely slow type checking for constrained `TypeVars` and overloads on generic classes · Issue #20439 · python/mypy · GitHub
Open Graph Title: extremely slow type checking for constrained `TypeVars` and overloads on generic classes · Issue #20439 · python/mypy
X Title: extremely slow type checking for constrained `TypeVars` and overloads on generic classes · Issue #20439 · python/mypy
Description: I'm seeing extremely slow type-checking (around 1–2 minutes) for a very small, self-contained example involving constrained TypeVars, generic subclasses, and many overloads. The code produces no errors but mypy appears to spend a disprop...
Open Graph Description: I'm seeing extremely slow type-checking (around 1–2 minutes) for a very small, self-contained example involving constrained TypeVars, generic subclasses, and many overloads. The code produces no er...
X Description: I'm seeing extremely slow type-checking (around 1–2 minutes) for a very small, self-contained example involving constrained TypeVars, generic subclasses, and many overloads. The code produces n...
Opengraph URL: https://github.com/python/mypy/issues/20439
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"extremely slow type checking for constrained `TypeVars` and overloads on generic classes","articleBody":"I'm seeing extremely slow type-checking (around 1–2 minutes) for a very small, self-contained example involving constrained `TypeVar`s, generic subclasses, and many overloads. The code produces no errors but mypy appears to spend a disproportionate amount of time resolving types.\n\nThe example defines two `TypeVar`s constrained to ~15 `Literal[str]` values, a generic base class `Pbase[T]`, several subclasses `P1[T]` … `P14[T]`, and a generic class `Fn[T1, T2]` whose `call` method is overloaded to preserve the concrete subclass while transforming the type parameter. The full reproduction is below.\n\n```python\nimport typing as t\n\n\nclass LetterT:\n A = t.Literal[\"a\"]\n B = t.Literal[\"b\"]\n C = t.Literal[\"c\"]\n D = t.Literal[\"d\"]\n E = t.Literal[\"e\"]\n F = t.Literal[\"f\"]\n G = t.Literal[\"g\"]\n H = t.Literal[\"h\"]\n I = t.Literal[\"i\"]\n J = t.Literal[\"j\"]\n K = t.Literal[\"k\"]\n L = t.Literal[\"l\"]\n M = t.Literal[\"m\"]\n N = t.Literal[\"n\"]\n O = t.Literal[\"o\"]\n\n\nT1 = t.TypeVar(\n \"T1\",\n LetterT.A,\n LetterT.B,\n LetterT.C,\n LetterT.D,\n LetterT.E,\n LetterT.F,\n LetterT.G,\n LetterT.H,\n LetterT.I,\n LetterT.J,\n LetterT.K,\n LetterT.L,\n LetterT.M,\n LetterT.N,\n LetterT.O,\n)\nT2 = t.TypeVar(\n \"T2\",\n LetterT.A,\n LetterT.B,\n LetterT.C,\n LetterT.D,\n LetterT.E,\n LetterT.F,\n LetterT.G,\n LetterT.H,\n LetterT.I,\n LetterT.J,\n LetterT.K,\n LetterT.L,\n LetterT.M,\n LetterT.N,\n LetterT.O,\n)\n\n\nclass Pbase(t.Generic[T1]):\n def __init__(self, v: T1) -> None:\n self.v: T1 = v\n\n\nclass P1(Pbase[T1]): ...\nclass P2(Pbase[T1]): ...\nclass P3(Pbase[T1]): ...\nclass P4(Pbase[T1]): ...\nclass P5(Pbase[T1]): ...\nclass P6(Pbase[T1]): ...\nclass P7(Pbase[T1]): ...\nclass P8(Pbase[T1]): ...\nclass P9(Pbase[T1]): ...\nclass P10(Pbase[T1]): ...\nclass P11(Pbase[T1]): ...\nclass P12(Pbase[T1]): ...\nclass P13(Pbase[T1]): ...\nclass P14(Pbase[T1]): ...\n\n\nclass Fn(t.Generic[T1, T2]):\n def __init__(self, a: T1, b: T2) -> None:\n self.a: T1 = a\n self.b: T2 = b\n\n @t.overload\n def call(self, p: P1[T1]) -> P1[T2]: ...\n @t.overload\n def call(self, p: P2[T1]) -> P2[T2]: ...\n @t.overload\n def call(self, p: P3[T1]) -> P3[T2]: ...\n @t.overload\n def call(self, p: P4[T1]) -> P4[T2]: ...\n @t.overload\n def call(self, p: P5[T1]) -> P5[T2]: ...\n @t.overload\n def call(self, p: P6[T1]) -> P6[T2]: ...\n @t.overload\n def call(self, p: P7[T1]) -> P7[T2]: ...\n @t.overload\n def call(self, p: P8[T1]) -> P8[T2]: ...\n @t.overload\n def call(self, p: P9[T1]) -> P9[T2]: ...\n @t.overload\n def call(self, p: P10[T1]) -> P10[T2]: ...\n @t.overload\n def call(self, p: P11[T1]) -> P11[T2]: ...\n @t.overload\n def call(self, p: P12[T1]) -> P12[T2]: ...\n @t.overload\n def call(self, p: P13[T1]) -> P13[T2]: ...\n @t.overload\n def call(self, p: P14[T1]) -> P14[T2]: ...\n\n def call(self, p): # type: ignore[no-untyped-def]\n return Fn(self.b, p.v)\n```\n\nRunning `mypy repro.py --no-incremental` consistently takes on the order of minutes on my machine. Removing any one of the following makes the runtime drop back to normal: the `Literal` constraints, the number of overloads, or the generic subclass hierarchy. This suggests a combinatorial blow-up in constraint solving and/or overload resolution. While using a bounded `TypeVar` instead of explicit constraints does improve performance, that is not a viable option in my case, as I need to parameterize these classes with precise, concrete `Literal` types rather than a common upper bound.\n\nThis is primarily a performance report; I'm not sure whether this is a known limitation or an opportunity for optimization, but the scaling behavior seems unexpectedly poor for such a small input.\n\nI also attach the command I used to benchmark type checking time.\n\n```console\n$ time mypy repro.py --no-incremental\n\nreal 1m44.220s\nuser 1m44.065s\nsys 0m1.409s\n```\n\nAny ideas what might be the issue? How hard would it be to solve it?","author":{"url":"https://github.com/quczer","@type":"Person","name":"quczer"},"datePublished":"2025-12-18T13:21:19Z","url":"https://github.com/python/mypy/issues/20439"}
| 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:96d4493a-065f-02e4-5d62-a9d73f3cf4fe |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A350:BACA4:2C7448B:3ABD89D:6B001F8D |
| html-safe-nonce | 9416cb106d0a979ead3162a119ebcd663a06cb0a31a6c26824e1391976bb3ed2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMzUwOkJBQ0E0OjJDNzQ0OEI6M0FCRDg5RDo2QjAwMUY4RCIsInZpc2l0b3JfaWQiOiI3Njk0MzIxMDg4MDgzMjcxNTY1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 5e39df56cc4a18ae23d7333840c90cf86eb8ced15a2ac5629491a1980acf1f22 |
| hovercard-subject-tag | issue:3743147012 |
| 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/20439/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d28006d514a97c00627b8922fe13b367aa64209279fa750a1dbc2775ce389928/python/mypy/issues/20439 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d28006d514a97c00627b8922fe13b367aa64209279fa750a1dbc2775ce389928/python/mypy/issues/20439 |
| og:image:alt | I'm seeing extremely slow type-checking (around 1–2 minutes) for a very small, self-contained example involving constrained TypeVars, generic subclasses, and many overloads. The code produces no er... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | quczer |
| 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