Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions pyrefly/lib/alt/callable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use pyrefly_types::literal::Lit;
use pyrefly_types::literal::LitStyle;
use pyrefly_types::meta_shape_dsl::MetaShapeFunction;
use pyrefly_types::meta_shape_dsl::ShapeTransform;
use pyrefly_types::simplify::simplify_tuples;
use pyrefly_types::tuple::Tuple;
use pyrefly_types::typed_dict::ExtraItems;
use pyrefly_types::types::TArgs;
Expand Down Expand Up @@ -1271,11 +1272,18 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> {
}
let unpacked_args_ty = match middle.len() {
0 => self.heap.mk_concrete_tuple(prefix),
1 => self.heap.mk_unpacked_tuple(
prefix,
self.heap.mk_unbounded_tuple(middle.pop().unwrap()),
suffix,
),
1 => {
// A TypeVarTuple element becomes `tuple[*Ts]`. Flatten that tuple into
// the surrounding prefix and suffix before checking assignability.
self.heap.mk_tuple(simplify_tuples(
Tuple::unpacked(
prefix,
self.heap.mk_unbounded_tuple(middle.pop().unwrap()),
suffix,
),
self.heap,
))
}
_ => {
let unpacked_variadic_args_count = middle
.iter()
Expand Down
39 changes: 39 additions & 0 deletions pyrefly/lib/test/type_var_tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,45 @@ def test[*Ts, T](f: Callable[[*Ts], T], t: tuple[*Ts], *args: *Ts):
"#,
);

// https://github.com/facebook/pyrefly/issues/4609
testcase!(
test_type_var_tuple_callable_gradual_fixed_params,
r#"
from typing import Any, Callable

class K[*Ts]:
func: Callable[[Any, *Ts], None]
suffix: Callable[[*Ts, Any], None]
both: Callable[[Any, *Ts, Any], None]

def g[*Ts](x: int, *xs: *Ts):
K[*Ts]().func(x, *xs)
K[*Ts]().suffix(*xs, x)
K[*Ts]().both(x, *xs, x)
"#,
);

testcase!(
test_type_var_tuple_callable_covariant_fixed_params,
r#"
from typing import Callable

class K[*Ts]:
prefix: Callable[[float, *Ts], None]
suffix: Callable[[*Ts, float], None]
both: Callable[[float, *Ts, float], None]

def g[*Ts](x: int, y: str, *xs: *Ts):
k = K[*Ts]()
k.prefix(x, *xs)
k.suffix(*xs, x)
k.both(x, *xs, x)
k.prefix(y, *xs) # E: Unpacked argument `tuple[str, *Ts]` is not assignable to varargs type `tuple[float, *Ts]`
k.suffix(*xs, y) # E: Unpacked argument `tuple[*Ts, str]` is not assignable to varargs type `tuple[*Ts, float]`
k.both(x, *xs, y) # E: Unpacked argument `tuple[int, *Ts, str]` is not assignable to varargs type `tuple[float, *Ts, float]`
"#,
);

testcase!(
test_type_var_tuple_callable_resolves_to_empty,
r#"
Expand Down
Loading