Skip to content

Commit

Permalink
stubgen: Do not consider nested generators (#12463)
Browse files Browse the repository at this point in the history
Since yields in nested functions will not yield outside of the
considered function, they can be ignored.
  • Loading branch information
shoracek authored Mar 27, 2022
1 parent c755928 commit 1480344
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
25 changes: 13 additions & 12 deletions mypy/traverser.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,20 @@ def has_return_statement(fdef: FuncBase) -> bool:
return seeker.found


class YieldSeeker(TraverserVisitor):
class FuncCollectorBase(TraverserVisitor):
def __init__(self) -> None:
self.inside_func = False

def visit_func_def(self, defn: FuncDef) -> None:
if not self.inside_func:
self.inside_func = True
super().visit_func_def(defn)
self.inside_func = False


class YieldSeeker(FuncCollectorBase):
def __init__(self) -> None:
super().__init__()
self.found = False

def visit_yield_expr(self, o: YieldExpr) -> None:
Expand All @@ -382,17 +394,6 @@ def has_yield_expression(fdef: FuncBase) -> bool:
return seeker.found


class FuncCollectorBase(TraverserVisitor):
def __init__(self) -> None:
self.inside_func = False

def visit_func_def(self, defn: FuncDef) -> None:
if not self.inside_func:
self.inside_func = True
super().visit_func_def(defn)
self.inside_func = False


class ReturnCollector(FuncCollectorBase):
def __init__(self) -> None:
super().__init__()
Expand Down
9 changes: 9 additions & 0 deletions test-data/unit/stubgen.test
Original file line number Diff line number Diff line change
Expand Up @@ -2619,3 +2619,12 @@ class PT(Protocol[T, T2]):
def func(*, non_default_kwarg: bool, default_kwarg: bool = True): ...
[out]
def func(*, non_default_kwarg: bool, default_kwarg: bool = ...): ...

[case testNestedGenerator]
def f():
def g():
yield 0

return 0
[out]
def f(): ...

0 comments on commit 1480344

Please sign in to comment.