microsoft / microsoft/pyright

Support usage of slack variables (lower bound emulation)

Open
#11,245 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement request
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

Context: https://github.com/python/typing/issues/674

[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 sequence concatenation, when an outer context is present.

Code sample in [pyright playground](https://pyright-play.net/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoByApgG5EgA0UAhgM63kwD68CRVIpR1ANi4kQBQggMY86tKADUiIgNoAVALoAuKFADEmFCWogk1FPlRQFg9eoAmRYFCZIYRCLQAUDHsACUUALQA%2BKB4kWhhFVSgAOijzC2tbJmoENhRLNyIPKgcnNQVvf0IwFCI1KIjhOKgRQpFqZjIRWkUARioFACYqJgUmWnERAGsAXmIyECUXGPUeGxg1GXkFJqUKSah9NAALWelZRTblwTyA%2BeaoAB9TNvO7bt7qAfDNKAAeHx8oAGU%2B-qhdfWoAEbTKAQaj9IiSTgwACuIBQNCgtGhbDwAigYFsi2u7VWUNh8PmLk8wjEEigAEEStFSfQoAAhKllGmSADCjPKNlgELqsiYVRQNRgLmmwG2J3JyzWSE2Yt2dKURwKRRUqy0AHcHBswND8NrHCBKoVHAAPGBUGAbIiwNHBKAITgiYJCCw0eiMfhsFz8wVMequEVmqUyzxUcXXeXeJ6AUHJAPB-qvROvIhqMRFNNB4PDAaskGusKFQGEs0KtMDAiO%2Bvz0BiBENWTAADHNduTw0oXVBBsmfX7hTMONKtt4tLG600m-IWxc6dcWW3O97ar7ZP6%2B0HB9G4y6mG1x3IwACAFayGBtizz6qLnsB-vBp6AGXI40A)

```python
from typing import Never, assert_type, reveal_type

class Vec[T]: # invariant in T
def _items(self) -> list[T]: ...
def _append(self, item: T) -> None: ...

def concat_vecs[T1, T2, _T_slack=Never](
left: Vec[T1],
right: Vec[T2],
) -> Vec[T1 | T2 | _T_slack]: # <-- Slack variable makes return a supertype of T1 | T2
return Vec()

class A: ...
class B: ...
class C: ...

def test_vec_concat(left: Vec[A], right: Vec[B]) -> None:
# without outer context, the type is precise
assert_type(concat_vecs(left, right), Vec[A | B]) # ✅️
# outer context allows widening due to slack variables
_0: Vec[A | B] = concat_vecs(left, right) # ✅️
_1: Vec[A | B | C] = concat_vecs(left, right) # ✅️
_2: Vec[object] = concat_vecs(left, right) # ❌️

```

In the example above, `pyright` fails to find the solution (`T1=A`, `T2=B`, `_T_slack=object`) in the last test case.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the concat_vecs example in the linked pyright playground and read the referenced typing issue for the proposed slack-variable behavior. Done means the final Vec[object] assignment succeeds while the no-context assertion remains precise, with the relevant behavior covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.