Skip to content

Commit e4229b9

Browse files
committed
Return None more consistently, see also #150
1 parent 75243de commit e4229b9

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

crates/zuban_python/src/file/inference.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -780,10 +780,13 @@ impl<'db, 'file> Inference<'db, 'file, '_> {
780780
}
781781
if let Some(return_type) = other.return_type {
782782
Inferred::from_type(return_type)
783-
} else if result_context.expect_not_none() {
784-
from.add_issue(i_s, IssueKind::DoesNotReturnAValue("Function".into()));
785-
Inferred::new_any_from_error()
786783
} else {
784+
if result_context.expect_not_none() {
785+
from.add_issue(i_s, IssueKind::DoesNotReturnAValue("Function".into()));
786+
if i_s.db.project.settings.mypy_compatible {
787+
return Inferred::new_any_from_error();
788+
}
789+
}
787790
Inferred::new_none()
788791
}
789792
} else {
@@ -4778,10 +4781,11 @@ pub fn await_(
47784781
}
47794782
if expect_not_none && matches!(t, Type::None) {
47804783
from.add_issue(i_s, IssueKind::DoesNotReturnAValue("Function".into()));
4781-
Inferred::new_any_from_error()
4782-
} else {
4783-
Inferred::from_type(t)
4784+
if i_s.db.project.settings.mypy_compatible {
4785+
return Inferred::new_any_from_error();
4786+
}
47844787
}
4788+
Inferred::from_type(t)
47854789
}
47864790

47874791
fn targets_len_infos(targets: TargetIterator) -> (usize, TupleLenInfos) {

crates/zuban_python/src/name.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use std::borrow::Cow;
22

33
use lsp_types::SymbolKind;
44
use parsa_python_cst::{
5-
AssignmentContent, ClassDef, DefiningStmt, FunctionDef, Name as CSTName, NameParent, NodeIndex,
6-
PrimaryTargetOrAtom, Scope, Target, TypeLike,
5+
ClassDef, DefiningStmt, FunctionDef, Name as CSTName, NameParent, NodeIndex, Scope, TypeLike,
76
};
87
use vfs::NormalizedPath;
98

crates/zuban_python/src/type_helpers/callable.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ impl<'a> Callable<'a> {
7474
.into(),
7575
),
7676
);
77-
return Inferred::new_any_from_error();
77+
if i_s.db.project.settings.mypy_compatible {
78+
return Inferred::new_any_from_error();
79+
}
7880
}
7981
self.execute_for_custom_return_type(
8082
i_s,

crates/zuban_python/tests/mypylike/tests/functions.test

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,27 @@ def four(*args: int) -> int: ...
409409
[case function_no_return_should_not_result_in_any_no_mypy_compatible]
410410
# flags: --no-mypy-compatible
411411
# From GH #150, see also the discussion there, Mypy returns Any, Pyright None
412-
from typing import assert_type
412+
from typing import assert_type, Callable, Generator, Iterator
413413

414414
def foo() -> None: ...
415415

416+
def bar(c: Callable[[], None]):
417+
val = c() # E: Function does not return a value (it only ever returns None)
418+
assert_type(val, None)
419+
420+
async def asyn(depth: int) -> None:
421+
if depth > 0:
422+
val = await asyn(depth - 1) # E: Function does not return a value (it only ever returns None)
423+
assert_type(val, None)
424+
425+
def generator():
426+
def gen() -> Iterator[int]:
427+
yield 1
428+
return None
429+
430+
val = yield from gen() # E: Function does not return a value (it only ever returns None)
431+
assert_type(val, None)
432+
433+
416434
val = foo() # E: "foo" does not return a value (it only ever returns None)
417435
assert_type(val, None)

0 commit comments

Comments
 (0)