Skip to content

Commit

Permalink
stubgen: infer None return type for functions without return statement (
Browse files Browse the repository at this point in the history
#4467)

Fixes #4181.
  • Loading branch information
hoefling authored and ilevkivskyi committed Jan 13, 2018
1 parent 468dff2 commit 6fce774
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 53 deletions.
17 changes: 15 additions & 2 deletions mypy/stubgen.py
Expand Up @@ -59,7 +59,7 @@
Expression, IntExpr, UnaryExpr, StrExpr, BytesExpr, NameExpr, FloatExpr, MemberExpr, TupleExpr,
ListExpr, ComparisonExpr, CallExpr, IndexExpr, EllipsisExpr,
ClassDef, MypyFile, Decorator, AssignmentStmt,
IfStmt, ImportAll, ImportFrom, Import, FuncDef, FuncBase, TempNode,
IfStmt, ReturnStmt, ImportAll, ImportFrom, Import, FuncDef, FuncBase, TempNode,
ARG_POS, ARG_STAR, ARG_STAR2, ARG_NAMED, ARG_NAMED_OPT,
)
from mypy.stubgenc import parse_all_signatures, find_unique_signatures, generate_stub_for_c_module
Expand Down Expand Up @@ -475,7 +475,7 @@ def visit_func_def(self, o: FuncDef) -> None:
retname = None
if isinstance(o.type, CallableType):
retname = self.print_annotation(o.type.ret_type)
elif o.name() == '__init__':
elif o.name() == '__init__' or not has_return_statement(o):
retname = 'None'
retfield = ''
if retname is not None:
Expand Down Expand Up @@ -814,6 +814,19 @@ def visit_assignment_stmt(self, o: AssignmentStmt) -> None:
return results


def has_return_statement(fdef: FuncBase) -> bool:
class ReturnSeeker(mypy.traverser.TraverserVisitor):
def __init__(self) -> None:
self.found = False

def visit_return_stmt(self, o: ReturnStmt) -> None:
self.found = True

seeker = ReturnSeeker()
fdef.accept(seeker)
return seeker.found


def get_qualified_name(o: Expression) -> str:
if isinstance(o, NameExpr):
return o.name
Expand Down

0 comments on commit 6fce774

Please sign in to comment.