Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stubgen: infer None return type for functions without return statement #4467

Merged
merged 3 commits into from
Jan 13, 2018
Merged
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
17 changes: 15 additions & 2 deletions mypy/stubgen.py
Original file line number Diff line number Diff line change
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
Loading