Skip to content
Closed
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
21 changes: 21 additions & 0 deletions pyrefly/lib/alt/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2926,6 +2926,27 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
self.expr(expr, hint.as_ref().map(|t| (t, tcc)), errors)
}
} else {
// Bare return (no value) implicitly returns None
// Check if None is compatible with the declared return type
if let Some(ref ret_type) = hint {
// For generators, extract the return type (3rd type parameter)
let expected_type = if x.is_generator {
hint.and_then(|ty| self.decompose_generator(&ty).map(|(_, _, r)| r))
} else if matches!(hint, Some(Type::TypeGuard(_) | Type::TypeIs(_))) {
// TypeGuard functions should return bool
Some(Type::ClassType(self.stdlib.bool().clone()))
} else {
Some(ret_type.clone())
};

// Check None against expected return type
if let Some(expected) = expected_type {
let tcc: &dyn Fn() -> TypeCheckContext = &|| {
TypeCheckContext::of_kind(TypeCheckKind::ExplicitFunctionReturn)
};
self.check_type(&Type::None, &expected, x.range, errors, tcc);
}
}
Type::None
}
}
Expand Down
1 change: 1 addition & 0 deletions pyrefly/lib/binding/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,7 @@ pub struct ReturnExplicit {
pub expr: Option<Box<Expr>>,
pub is_generator: bool,
pub is_async: bool,
pub range: TextRange,
}

#[derive(Clone, Debug)]
Expand Down
1 change: 1 addition & 0 deletions pyrefly/lib/binding/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ impl<'a> BindingsBuilder<'a> {
expr: x.value,
is_generator,
is_async,
range: x.range,
}),
)
})
Expand Down
27 changes: 27 additions & 0 deletions pyrefly/lib/test/returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,30 @@ def f(x: str): pass
return f(1) # E: Invalid `return` outside of a function # E: `Literal[1]` is not assignable to parameter `x` with type `str`
"#,
);

testcase!(
test_bare_return_with_non_none_type,
r#"
def test() -> int:
return # E: Returned type `None` is not assignable to declared return type `int`
"#,
);

testcase!(
test_bare_return_with_none_type,
r#"
def test() -> None:
return # Should work - None is assignable to None
"#,
);

testcase!(
test_bare_return_in_generator,
r#"
from typing import Generator

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