-
Notifications
You must be signed in to change notification settings - Fork 259
Open
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 sequence concatenation, when an outer context is present.
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) # ❌️pyrefly fails to find existing solutions to these function calls:
_0: (T1=A,T2=B,_T_slack=Never)_1: (T1=A,T2=B,_T_slack=C)_2: (T1=A,T2=B,_T_slack=object)
ERROR sandbox.py:21:38-42: Argument `Vec[A]` is not assignable to parameter `left` with type `Vec[A | B]` in function `concat_vecs`
ERROR sandbox.py:21:44-49: Argument `Vec[B]` is not assignable to parameter `right` with type `Vec[A | B]` in function `concat_vecs`
ERROR sandbox.py:22:38-42: Argument `Vec[A]` is not assignable to parameter `left` with type `Vec[A | B | C]` in function `concat_vecs`
ERROR sandbox.py:22:44-49: Argument `Vec[B]` is not assignable to parameter `right` with type `Vec[A | B | C]` in function `concat_vecs`
ERROR sandbox.py:23:38-42: Argument `Vec[A]` is not assignable to parameter `left` with type `Vec[object]` in function `concat_vecs`
Sandbox Link
(Only applicable for extension issues) IDE Information
No response
Metadata
Metadata
Assignees
Labels
No labels