René's URL Explorer Experiment


Title: Re-think our high level logic for type inference · Issue #22063 · python/mypy · GitHub

Open Graph Title: Re-think our high level logic for type inference · Issue #22063 · python/mypy

X Title: Re-think our high level logic for type inference · Issue #22063 · python/mypy

Description: There is a fundamental problem with how type inference for generic functions works in mypy. To illustrate, this simple basic example still fails as of now: class B[T]: ... class C(B[int]): ... def first[T](xs: list[T]) -> T: ... b: B[int...

Open Graph Description: There is a fundamental problem with how type inference for generic functions works in mypy. To illustrate, this simple basic example still fails as of now: class B[T]: ... class C(B[int]): ... def ...

X Description: There is a fundamental problem with how type inference for generic functions works in mypy. To illustrate, this simple basic example still fails as of now: class B[T]: ... class C(B[int]): ... def ...

Opengraph URL: https://github.com/python/mypy/issues/22063

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Re-think our high level logic for type inference","articleBody":"There is a fundamental problem with how type inference for generic functions works in mypy. To illustrate, this simple basic example still fails as of now:\n```python\nclass B[T]: ...\nclass C(B[int]): ...\n\ndef first[T](xs: list[T]) -> T: ...\n\nb: B[int]\ncs: list[C]\nb = first(cs)  # E: some bogus stuff about list being invariant\n```\nThere are many similar examples where type inference fails. This problem is sometimes referred to as \"overusing the outer context\". As of now we have at least four weird ad-hoc workarounds in the code to compensate for this problem:\n* We special-case some type contexts if a return type is a type variable or a union of type variables\n* If both type context and return type are optional we \"unwrap\" them.\n* If type of a variable is a union, we accept r.h.s. in assignment to it twice, with full and empty context, and check which one gives a more narrow type.\n* We accept `return` value twice (again, in full and empty context) and see which one doesn't result in an error.\n\nThis all happens because mypy doesn't do a \"full expression\" inference (i.e. solving all type variables in an expression at the same time). I considered doing this a while ago, but:\n* This approach has its downsides, such as bad performance, and cryptic error messages.\n* It may be a too large change to do.\n\nHowever, few days ago I realized that our current algorithm (Hanoi towers inference, as I call it) may actually be implemented not as intended. At very high level this is how it roughly works currently:\n1. Use return/outer type context to infer some type variables, and apply them.\n2. Use this partially inferred function type to infer types of arguments (in partial/erased context).\n3. Use the types of arguments to infer the rest of type variables of the _partially inferred function_.\n4. Re-accept the arguments, now in the full type context.\n\nI think the emphasized part of step 3 is problematic. IMO it should work like this (and maybe it was actually intended to work like this):\n\n  3. Infer argument-side constrains, and solve them together with constrains from step 1 to infer the type variables of the _original function_.\n\nIt seems to me this (relatively modest, but still quite fundamental) change may solve many/most existing problems with overusing outer type context. We may not need the full expression type inference, it seems like simply solving return- and argument-side constraints together _at each nesting level_ may be sufficient. Currently type context propagates one way: from outside in, with this change the inner type context will have a chance to \"fight back\". I played with this on the weekend, and first results look promising. But before fully committing to this, I wanted to see if there are any thoughts/objections or cases to consider. cc @JukkaL @hauntsaninja \n\nSome non-trivial things to consider:\n* Preserving current error message logic requires falling back to outer-first inference if the two-sided fails (at given nesting level).\n* I am not sure yet what to do with two-pass argument inference (for lambdas). We may potentially integrate it fully in the new logic, but this will make overall logic too complex/hard to grasp.\n* Polymorphic inference may need some care. Right now it is still used as a fallback. Potentially we may keep it this way, since in most cases where it is useful, the outer context is empty (e.g. for decorators or functional programming).\n* We need to re-inspect (and potentially unify) what is considered a valid solution for a type variable, right now the logic is slightly different between various steps.\n\nFinally, this will have some performance implications. Here are two things I have to mitigate:\n* Make expression cache more robust/reliable, see e.g. https://github.com/python/mypy/pull/22059\n* (Optionally, if problems arise) limit the nesting level after which we fallback to the outer-first inference.\n\nA vague analogy would be how PEG parsers work: it is \"conceptually\" a recursive descent parser (which has exponential complexity) with extensive and robust memoization.","author":{"url":"https://github.com/ilevkivskyi","@type":"Person","name":"ilevkivskyi"},"datePublished":"2026-09-28T16:25:28Z","url":"https://github.com/python/mypy/issues/22063"}

react-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:020ffe8c-6966-e230-4d70-1a56b36ea5c9
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id84C6:23FDF9:2068410:2C2B296:6B00247D
html-safe-noncea292d30b50d8ce659b9ab711c2101554b005d3cb3762b484d21c5aa0be448c7b
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NEM2OjIzRkRGOToyMDY4NDEwOjJDMkIyOTY6NkIwMDI0N0QiLCJ2aXNpdG9yX2lkIjoiOTU5OTM2MzQ2NDgxMjM5MTY1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac7de68b639ca99084cd34f3f2746ef7de37374287336485c2c1e4889d4886213c
hovercard-subject-tagissue:5617842093
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/mypy/22063/issue_layout
twitter:imagehttps://opengraph.githubassets.com/afbb3d3d50138e90ffebfe73d19c744be9953a762f99820f9aa50aec02407e5d/python/mypy/issues/22063
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/afbb3d3d50138e90ffebfe73d19c744be9953a762f99820f9aa50aec02407e5d/python/mypy/issues/22063
og:image:altThere is a fundamental problem with how type inference for generic functions works in mypy. To illustrate, this simple basic example still fails as of now: class B[T]: ... class C(B[int]): ... def ...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameilevkivskyi
hostnamegithub.com
expected-hostnamegithub.com
None4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0
turbo-cache-controlno-preview
go-importgithub.com/python/mypy git https://github.com/python/mypy.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id7053637
octolytics-dimension-repository_nwopython/mypy
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id7053637
octolytics-dimension-repository_network_root_nwopython/mypy
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release44c658b24abf00491fb7ae5f56d42c9f38cbeab2
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/mypy/issues/22063#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fmypy%2Fissues%2F22063
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fmypy%2Fissues%2F22063
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fmypy
Reloadhttps://github.com/python/mypy/issues/22063
Reloadhttps://github.com/python/mypy/issues/22063
Reloadhttps://github.com/python/mypy/issues/22063
pythonhttps://github.com/python
mypyhttps://github.com/python/mypy
Codehttps://github.com/python/mypy
Issues2.7k (2.7k)https://github.com/python/mypy/issues
Pull requests546 (546)https://github.com/python/mypy/pulls
Actionshttps://github.com/python/mypy/actions
Projectshttps://github.com/python/mypy/projects
Wikihttps://github.com/python/mypy/wiki
Security and qualityhttps://github.com/python/mypy/security
Insightshttps://github.com/python/mypy/insights
https://github.com/python/mypy
New issuehttps://github.com/login?return_to=https://github.com/python/mypy/issues/22063
New issuehttps://github.com/login?return_to=https://github.com/python/mypy/issues/22063
Re-think our high level logic for type inferencehttps://github.com/python/mypy/issues/22063#top
metaIssues tracking a broad area of workhttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22meta%22
topic-inferenceWhen to infer types or require explicit annotationshttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22topic-inference%22
topic-type-contextType context / bidirectional inferencehttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22topic-type-context%22
https://github.com/ilevkivskyi
ilevkivskyihttps://github.com/ilevkivskyi
on Sep 28, 2026https://github.com/python/mypy/issues/22063#issue-5617842093
@JukkaLhttps://github.com/JukkaL
@hauntsaninjahttps://github.com/hauntsaninja
Make expression cache robust for nested generic calls #22059https://github.com/python/mypy/pull/22059
ilevkivskyihttps://github.com/ilevkivskyi
metaIssues tracking a broad area of workhttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22meta%22
topic-type-contextType context / bidirectional inferencehttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22topic-type-context%22
topic-inferenceWhen to infer types or require explicit annotationshttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22topic-inference%22
on Sep 28, 2026https://github.com/python/mypy/issues/22063#event-32009961343
https://github.com/vinitsonawane45
vinitsonawane45https://github.com/vinitsonawane45
on Sep 30, 2026https://github.com/python/mypy/issues/22063#issuecomment-5905431982
@ilevkivskyihttps://github.com/ilevkivskyi
https://github.com/ilevkivskyi
ilevkivskyihttps://github.com/ilevkivskyi
on Oct 4, 2026https://github.com/python/mypy/issues/22063#issuecomment-5979647051
#21803https://github.com/python/mypy/pull/21803
https://github.com/vinitsonawane45
vinitsonawane45https://github.com/vinitsonawane45
on Oct 4, 2026https://github.com/python/mypy/issues/22063#issuecomment-5982684288
@ilevkivskyihttps://github.com/ilevkivskyi
#21803https://github.com/python/mypy/pull/21803
#21803https://github.com/python/mypy/pull/21803
https://github.com/ilevkivskyi
ilevkivskyihttps://github.com/ilevkivskyi
on Oct 4, 2026https://github.com/python/mypy/issues/22063#issuecomment-5982759325
@vinitsonawane45https://github.com/vinitsonawane45
https://github.com/rheard
rheardhttps://github.com/rheard
on Oct 4, 2026https://github.com/python/mypy/issues/22063#issuecomment-5983225496
#21803https://github.com/python/mypy/pull/21803
#21803https://github.com/python/mypy/pull/21803
#20648https://github.com/python/mypy/issues/20648
#21803https://github.com/python/mypy/pull/21803
Assigning to intermediate variable changes type checking results #19304https://github.com/python/mypy/issues/19304
error: "key" to "max" has incompatible type #21104https://github.com/python/mypy/issues/21104
functools.reduce over sets becomes unacceptable when used in a larger expression #17694https://github.com/python/mypy/issues/17694
https://github.com/ilevkivskyi
ilevkivskyihttps://github.com/ilevkivskyi
on Oct 4, 2026https://github.com/python/mypy/issues/22063#issuecomment-5983330352
@rheardhttps://github.com/rheard
#21803https://github.com/python/mypy/pull/21803
https://github.com/rheard
rheardhttps://github.com/rheard
on Oct 7, 2026https://github.com/python/mypy/issues/22063#issuecomment-6047861640
@ilevkivskyihttps://github.com/ilevkivskyi
mypyc/mypyc#1231https://github.com/mypyc/mypyc/issues/1231
#22138https://github.com/python/mypy/issues/22138
https://github.com/ilevkivskyi
ilevkivskyihttps://github.com/ilevkivskyi
on Oct 7, 2026https://github.com/python/mypy/issues/22063#issuecomment-6049136598
@rheardhttps://github.com/rheard
https://github.com/rheard
rheardhttps://github.com/rheard
on Oct 8, 2026https://github.com/python/mypy/issues/22063#issuecomment-6060772473
@ilevkivskyihttps://github.com/ilevkivskyi
#22142https://github.com/python/mypy/pull/22142
#20622https://github.com/python/mypy/pull/20622
https://github.com/ilevkivskyi
ilevkivskyihttps://github.com/ilevkivskyi
on Oct 13, 2026https://github.com/python/mypy/issues/22063#issuecomment-6156827168
@rheardhttps://github.com/rheard
#21803https://github.com/python/mypy/pull/21803
https://github.com/rheard
rheardhttps://github.com/rheard
on Oct 14, 2026https://github.com/python/mypy/issues/22063#issuecomment-6158527699
@ilevkivskyihttps://github.com/ilevkivskyi
#22059https://github.com/python/mypy/pull/22059
ilevkivskyihttps://github.com/ilevkivskyi
on Nov 4, 2026https://github.com/python/mypy/issues/22063#event-35119024583
Use both sides of type context for generic type inference (part 1) (#…https://github.com/python/mypy/commit/902ebb2a29df611e642e28b39bd8cbeb4eb1c876
#…https://github.com/python/mypy/pull/22286
902ebb2https://github.com/python/mypy/commit/902ebb2a29df611e642e28b39bd8cbeb4eb1c876
Use both sides of type context for generic type inference (part 2) (#…https://github.com/python/mypy/commit/a0dda1c08e861018029fea6aaa2140e4d6c58333
#…https://github.com/python/mypy/pull/22394
a0dda1chttps://github.com/python/mypy/commit/a0dda1c08e861018029fea6aaa2140e4d6c58333
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/mypy/issues/22063
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/mypy/issues/22063
metaIssues tracking a broad area of workhttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22meta%22
topic-inferenceWhen to infer types or require explicit annotationshttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22topic-inference%22
topic-type-contextType context / bidirectional inferencehttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22topic-type-context%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.