Skip to content

Commit 6fce774

Browse files
hoeflingilevkivskyi
authored andcommitted
stubgen: infer None return type for functions without return statement (#4467)
Fixes #4181.
1 parent 468dff2 commit 6fce774

File tree

2 files changed

+86
-53
lines changed

2 files changed

+86
-53
lines changed

mypy/stubgen.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
Expression, IntExpr, UnaryExpr, StrExpr, BytesExpr, NameExpr, FloatExpr, MemberExpr, TupleExpr,
6060
ListExpr, ComparisonExpr, CallExpr, IndexExpr, EllipsisExpr,
6161
ClassDef, MypyFile, Decorator, AssignmentStmt,
62-
IfStmt, ImportAll, ImportFrom, Import, FuncDef, FuncBase, TempNode,
62+
IfStmt, ReturnStmt, ImportAll, ImportFrom, Import, FuncDef, FuncBase, TempNode,
6363
ARG_POS, ARG_STAR, ARG_STAR2, ARG_NAMED, ARG_NAMED_OPT,
6464
)
6565
from mypy.stubgenc import parse_all_signatures, find_unique_signatures, generate_stub_for_c_module
@@ -475,7 +475,7 @@ def visit_func_def(self, o: FuncDef) -> None:
475475
retname = None
476476
if isinstance(o.type, CallableType):
477477
retname = self.print_annotation(o.type.ret_type)
478-
elif o.name() == '__init__':
478+
elif o.name() == '__init__' or not has_return_statement(o):
479479
retname = 'None'
480480
retfield = ''
481481
if retname is not None:
@@ -814,6 +814,19 @@ def visit_assignment_stmt(self, o: AssignmentStmt) -> None:
814814
return results
815815

816816

817+
def has_return_statement(fdef: FuncBase) -> bool:
818+
class ReturnSeeker(mypy.traverser.TraverserVisitor):
819+
def __init__(self) -> None:
820+
self.found = False
821+
822+
def visit_return_stmt(self, o: ReturnStmt) -> None:
823+
self.found = True
824+
825+
seeker = ReturnSeeker()
826+
fdef.accept(seeker)
827+
return seeker.found
828+
829+
817830
def get_qualified_name(o: Expression) -> str:
818831
if isinstance(o, NameExpr):
819832
return o.name

0 commit comments

Comments
 (0)