Title: ENH: Support slack variables (lower bound emulation) · Issue #20648 · python/mypy · GitHub
Open Graph Title: ENH: Support slack variables (lower bound emulation) · Issue #20648 · python/mypy
X Title: ENH: Support slack variables (lower bound emulation) · Issue #20648 · python/mypy
Description: Describe the Bug Context: python/typing#674 Slack variables would allow us to emulate lower bounds on type variables, which is relevant for methods like dict.__or__ or list concatenation, when an outer context is present. from typing imp...
Open Graph Description: Describe the Bug Context: python/typing#674 Slack variables would allow us to emulate lower bounds on type variables, which is relevant for methods like dict.__or__ or list concatenation, when an o...
X Description: Describe the Bug Context: python/typing#674 Slack variables would allow us to emulate lower bounds on type variables, which is relevant for methods like dict.__or__ or list concatenation, when an o...
Opengraph URL: https://github.com/python/mypy/issues/20648
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ENH: Support slack variables (lower bound emulation)","articleBody":"### Describe the Bug\n\nContext: https://github.com/python/typing/issues/674\n\n[Slack variables](https://en.wikipedia.org/wiki/Slack_variable) would allow us to emulate lower bounds on type variables, which is relevant for methods like `dict.__or__` or list concatenation, when an outer context is present.\n\n```python\nfrom typing import Never, assert_type, reveal_type\n\nclass Vec[T]: # invariant in T\n def _items(self) -> list[T]: ...\n def _append(self, item: T) -> None: ...\n\ndef concat_vecs[T1, T2, _T_slack=Never](\n left: Vec[T1],\n right: Vec[T2],\n) -> Vec[T1 | T2 | _T_slack]: # \u003c-- Slack variable makes return a supertype of T1 | T2\n return Vec()\n\nclass A: ...\nclass B: ...\nclass C: ...\n\ndef test_vec_concat(left: Vec[A], right: Vec[B]) -> None:\n # without outer context, the type is precise\n assert_type(concat_vecs(left, right), Vec[A | B]) # ✅️\n # outer context allows widening due to slack variables\n _0: Vec[A | B] = concat_vecs(left, right) # ❌️\n _1: Vec[A | B | C] = concat_vecs(left, right) # ❌️\n _2: Vec[object] = concat_vecs(left, right) # ❌️\n```\n\n`mypy` fails to find existing solutions on some of these function calls:\n\n- `_0`: (`T1=A`, `T2=B`, `_T_slack=Never`)\n- `_1`: (`T1=A`, `T2=B`, `_T_slack=C`)\n- `_2`: (`T1=A`, `T2=B`, `_T_slack=object`)\n\n```\n21:38: error: Argument 1 to \"concat_vecs\" has incompatible type \"Vec[A]\"; expected \"Vec[A | B]\" [arg-type]\n21:44: error: Argument 2 to \"concat_vecs\" has incompatible type \"Vec[B]\"; expected \"Vec[A | B]\" [arg-type]\n22:38: error: Argument 1 to \"concat_vecs\" has incompatible type \"Vec[A]\"; expected \"Vec[A | B | C]\" [arg-type]\n22:44: error: Argument 2 to \"concat_vecs\" has incompatible type \"Vec[B]\"; expected \"Vec[A | B | C]\" [arg-type]\n23:38: error: Argument 1 to \"concat_vecs\" has incompatible type \"Vec[A]\"; expected \"Vec[object]\" [arg-type]\n23:44: error: Argument 2 to \"concat_vecs\" has incompatible type \"Vec[B]\"; expected \"Vec[object]\" [arg-type]\n```\n\nno playground link as typevar defaults are still not supported.","author":{"url":"https://github.com/randolf-scholz","@type":"Person","name":"randolf-scholz"},"datePublished":"2026-01-24T17:58:29Z","url":"https://github.com/python/mypy/issues/20648"}
| 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:f34dc45b-ed48-830d-3da4-9205684d95cc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B7B4:2573D7:1FF3D7D:2B77DBC:6B001CE7 |
| html-safe-nonce | 3e8ed32cf1f63a84647c49e9e7b812465f6f45722154a182f60739f7e793fad6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCN0I0OjI1NzNENzoxRkYzRDdEOjJCNzdEQkM6NkIwMDFDRTciLCJ2aXNpdG9yX2lkIjoiNjUyNDgzMTgyNzcxMzEzODk1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 194b094838ad010e795d4bd5f074c4f49a8299576ac801a3bfbf058720f2da18 |
| hovercard-subject-tag | issue:3851715560 |
| 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/20648/issue_layout |
| twitter:image | https://opengraph.githubassets.com/90d00529749cc1a115db75fcf509c5a8c73d76a6639afa42c0371a10ccc058d2/python/mypy/issues/20648 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/90d00529749cc1a115db75fcf509c5a8c73d76a6639afa42c0371a10ccc058d2/python/mypy/issues/20648 |
| og:image:alt | Describe the Bug Context: python/typing#674 Slack variables would allow us to emulate lower bounds on type variables, which is relevant for methods like dict.__or__ or list concatenation, when an o... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | randolf-scholz |
| 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