Skip to content

Commit 1871f0d

Browse files
rchen152meta-codesync[bot]
authored andcommitted
Add tests showing two bugs in tuple_infer
Summary: I noticed that tuple_infer skips using vars as hints to avoid premature pinning. Add tests showing some problems this causes. Reviewed By: grievejia Differential Revision: D91639156 fbshipit-source-id: 4891d9cb1f2584f5406de95546a68472c4378610
1 parent da88083 commit 1871f0d

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

pyrefly/lib/alt/expr.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,9 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
655655
}
656656
if !nontuples.is_empty() {
657657
// The non-tuple options may contain a type like Sequence[T] that provides an additional default hint.
658-
// Filter out top-level Vars: they don't provide any hints, and we don't want to pin them.
658+
// TODO: we filter out top-level Vars to prevent premature pinning
659+
// (https://github.com/facebook/pyrefly/issues/105), but this also prevents us from picking up hints
660+
// from Quantified restrictions. See test::contextual::test_sequence_hint_in_typevar_bound.
659661
let nontuple_hint = self.unions(
660662
nontuples
661663
.into_iter()

pyrefly/lib/test/contextual.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,3 +660,29 @@ x3: tuple[TD1] | tuple[TD2] = ({"y": "a"},)
660660
x4: tuple[TD1] | tuple[TD2, ...] = ({"x": 0}, {"y": "a"}) # E: `tuple[TD1, TD2]` is not assignable to `tuple[TD1] | tuple[TD2, ...]`
661661
"#,
662662
);
663+
664+
testcase!(
665+
bug = "`Sequence[TD]` hint should be used when typing `({'x': 0},)`",
666+
test_sequence_hint_in_typevar_bound,
667+
r#"
668+
from typing import Sequence, TypedDict
669+
class TD(TypedDict):
670+
x: int
671+
def f[T: Sequence[TD]](x: T) -> T:
672+
return x
673+
f(({"x": 0},)) # E: `tuple[dict[str, int]]` is not assignable to upper bound `Sequence[TD]`
674+
"#,
675+
);
676+
677+
testcase!(
678+
bug = "`TD` should be used as a hint when typing `{'x': 0}`",
679+
test_typed_dict_hint_in_typevar_bound,
680+
r#"
681+
from typing import TypedDict
682+
class TD(TypedDict):
683+
x: int
684+
def f[T: TD](x: tuple[T, ...]) -> T:
685+
return x[0]
686+
f(({"x": 0},)) # E: `dict[str, int]` is not assignable to upper bound `TD`
687+
"#,
688+
);

0 commit comments

Comments
 (0)