Skip to content

Commit d8d70cc

Browse files
committed
Fix not None return types, fixes #124
1 parent e4229b9 commit d8d70cc

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

crates/zuban_python/src/type_/operations.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,18 @@ impl Type {
673673
execute_type_of_type(i_s, args, result_context, on_type_error, cls.as_ref())
674674
}
675675
Type::Union(union) => Inferred::gather_simplified_union(i_s, |gather| {
676+
let result_context = if result_context.expect_not_none()
677+
&& union.iter().any(|t| match t {
678+
// If there's any callable with a return type that is not None we should
679+
// not complain with the lint that a None return does not make sense,
680+
// because it can be some other type.
681+
Type::Callable(c) => c.return_type != Type::None,
682+
_ => false,
683+
}) {
684+
&mut ResultContext::ExpectUnused
685+
} else {
686+
result_context
687+
};
676688
for entry in union.iter() {
677689
gather(entry.execute(i_s, None, args, result_context, on_type_error))
678690
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,3 +433,30 @@ def generator():
433433

434434
val = foo() # E: "foo" does not return a value (it only ever returns None)
435435
assert_type(val, None)
436+
437+
[case function_does_not_return_value_mypy_compatible]
438+
# flags: --mypy-compatible
439+
from collections.abc import Callable
440+
441+
def get(fn: Callable[[], None] | Callable[[], str]) -> None:
442+
ret = fn()
443+
reveal_type(ret) # N: Revealed type is "None | str"
444+
445+
def get2(fn: Callable[[float], None] | Callable[[int], None]) -> None:
446+
ret = fn(1) # E: Function does not return a value (it only ever returns None) \
447+
# E: Function does not return a value (it only ever returns None)
448+
reveal_type(ret) # N: Revealed type is "Any"
449+
450+
[case function_does_not_return_value_no_mypy_compatible]
451+
# flags: --no-mypy-compatible
452+
# From GH #124
453+
from collections.abc import Callable
454+
455+
def get(fn: Callable[[], None] | Callable[[], str]) -> None:
456+
ret = fn()
457+
reveal_type(ret) # N: Revealed type is "None | str"
458+
459+
def get2(fn: Callable[[float], None] | Callable[[int], None]) -> None:
460+
ret = fn(1) # E: Function does not return a value (it only ever returns None) \
461+
# E: Function does not return a value (it only ever returns None)
462+
reveal_type(ret) # N: Revealed type is "None"

0 commit comments

Comments
 (0)