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
11 changes: 0 additions & 11 deletions conformance/third_party/conformance.exp
Original file line number Diff line number Diff line change
Expand Up @@ -1800,17 +1800,6 @@
"stop_column": 16,
"stop_line": 66
},
{
"code": -2,
"column": 12,
"concise_description": "Returned type `Literal[True]` is not assignable to declared return type `None`",
"description": "Returned type `Literal[True]` is not assignable to declared return type `None`",
"line": 71,
"name": "bad-return",
"severity": "error",
"stop_column": 16,
"stop_line": 71
},
{
"code": -2,
"column": 11,
Expand Down
3 changes: 2 additions & 1 deletion pyrefly/lib/alt/unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,10 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> {
let send_ty = self
.resolve_var_opt(ty, send_ty)
.unwrap_or_else(|| self.heap.mk_none());
// Iterator and Iterable annotations do not constrain the generator's return value.
let return_ty = self
.resolve_var_opt(ty, return_ty)
.unwrap_or_else(|| self.heap.mk_none());
.unwrap_or_else(|| self.heap.mk_any_implicit());
Some((yield_ty, send_ty, return_ty))
} else {
None
Expand Down
30 changes: 30 additions & 0 deletions pyrefly/lib/test/yields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,36 @@ def g() -> Iterator[list[int]] | Iterator[list[str]]:
"#,
);

testcase!(
test_iterator_annotation_with_return_value,
r#"
from collections.abc import Generator, Iterable, Iterator
from typing import assert_type

def gen(x: int) -> Iterator[int] | int:
yield from range(4)
return 5

assert_type(gen(0), Iterator[int] | int)

def iterator() -> Iterator[int]:
yield 1
return "done"

def iterable() -> Iterable[int]:
yield 1
return "done"

def bad_yield() -> Iterator[int] | int:
yield "oops" # E: Yielded type `Literal['oops']` is not assignable to declared yield type `int`
return 5

def explicit_return_type() -> Generator[int, None, None] | int:
yield 1
return 5 # E: Returned type `Literal[5]` is not assignable to declared return type `None`
"#,
);

testcase!(
test_return_is_incompatible_with_generator,
r#"
Expand Down
Loading