diff --git a/mypy/traverser.py b/mypy/traverser.py
--- a/mypy/traverser.py
+++ b/mypy/traverser.py
@@ -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:
@@ -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__()

diff --git a/test-data/unit/stubgen.test b/test-data/unit/stubgen.test
--- a/test-data/unit/stubgen.test
+++ b/test-data/unit/stubgen.test
@@ -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(): ...

Stubgen applies Generator type to function with nested generator
**Bug Report**

When stubgen encounters a function with a nested generator-function, that generator type is used for the return type of the function, even if the function itself does not return a generator.

**To Reproduce**

1. Create a file `generator.py`:
   ```py
   def some_function():
       def generator():
           yield "a"

       return 5
   ```
2. Run `stubgen generator.py`.

**Expected Behavior**
The function in the generated type stub has a "regular" return type.
```py
from collections.abc import Generator
from typing import Any

def some_function() -> Any: ...  # or int
```

**Actual Behavior**
The stub function has a `Generator[None, None, Any]` type.
```py
from collections.abc import Generator
from typing import Any

def some_function() -> Generator[None, None, Any]: ...
```

**Your Environment**

- Mypy version used: 0.930
- Mypy command-line flags: `stubgen generator.py`
- Mypy configuration options from `mypy.ini` (and other config files): -
- Python version used: 3.9.9
- Operating system and version: macOS 12.1



mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testPreserveVarAnnotation mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testEmptyFile mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testMultipleVariable mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testSpecialInternalVar mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testPreserveVarAnnotationWithoutQuotes mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testClassVariable mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testSingleFunction mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testBaseClass mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testVarArgs mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testInitTypeAnnotationPreserved mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testTwoFunctions mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testDecoratedFunction mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testKwVarArgs mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testSelfAssignment mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testDefaultArgInt mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testMultipleAssignment mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testVarArgsWithKwVarArgs mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testSelfAndClassBodyAssignment mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testDefaultArgNone mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testClass mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testMultipleAssignmentAnnotated mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testEmptyClass mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testDefaultArgBool mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testVariable mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testMultipleAssignment2 mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testSkipPrivateFunction mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testDefaultArgStr mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testAnnotatedVariable mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testKeywordOnlyArg mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testIncludePrivateFunction mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testDefaultArgBytes mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testAnnotatedVariableGeneric mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testSkipPrivateMethod mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testProperty mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testDefaultArgFloat mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testIncludePrivateMethod mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testStaticMethod mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testSkipPrivateVar mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testAnnotatedVariableOldSyntax mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testAnnotatedVariableNone mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testClassMethod mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testDefaultArgOther mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testPreserveFunctionAnnotation mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testAnnotatedVariableNoneOldSyntax mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testIncludePrivateVar mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testIfMainCheck mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testNoSpacesBetweenEmptyClasses mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testPreserveFunctionAnnotationWithArgs mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testImportStar mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testExceptionBaseClasses mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testIncludePrivateProperty mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testNamedtupleBaseClass mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testIncludeFromImportIfInAll_import mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testOmitSomeSpecialMethods mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testSkipPrivateStaticAndClassMethod mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testNamedtupleAltSyntaxFieldsTuples mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testIncludePrivateStaticAndClassMethod mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testDynamicNamedTuple mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testNamedtuple mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testOmitDefsNotInAll_import mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testArbitraryBaseClass mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testEmptyNamedtuple mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testArbitraryBaseClass2 mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testNamedtupleAltSyntax mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testExportModule_import mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testNamedtupleAltSyntaxUsingComma mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testUnqualifiedArbitraryBaseClassWithNoDef mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testExportModule2_import mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testNamedtupleAltSyntaxUsingMultipleCommas mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testUnqualifiedArbitraryBaseClass mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testNamedtupleWithUnderscore mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testExportModuleAs_import mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testUnqualifiedArbitraryBaseClassWithImportAs mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testObjectBaseClass mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testEmptyLines mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testGenericClass mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testNestedClass mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testAsyncAwait_fast_parser mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testExportModuleInPackage_import mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testExportViaRelativeImport mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testInferOptionalOnlyFunc mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testExportViaRelativePackageImport mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testAnnotationImportsFrom mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testExportPackageOfAModule_import mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testNoExportViaRelativeImport mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testAnnotationImports mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testRelativeImportAndBase mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testAnnotationImports2 mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testRelativeImportAll mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testAnnotationFwRefs mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testDuplicateDef mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testTypeVarPreserved mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testCommentForUndefinedName_import mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testTypeVarNamedArgsPreserved mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testTypeVarArgsPreserved mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testIgnoreSlots mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testTypeAliasPreserved mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testGeneratorAlreadyDefined mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testSkipPrivateProperty mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testDeepTypeAliasPreserved mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testCallable mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testAliasOnlyToplevel mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testDeepGenericTypeAliasPreserved mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testAwaitDef mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testAliasExceptions mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testEllipsisAliasPreserved mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testFunctionNoReturnInfersReturnNone mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testCoroutineImportAsyncio mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testCallableAliasPreserved mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testFunctionReturnNoReturnType mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testCoroutineImportAsyncioCoroutines mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testAliasPullsImport mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testCoroutineImportAsyncioCoroutinesSub mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testFunctionEllipsisInfersReturnNone mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testFunctionYields mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testCoroutineImportTypes mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testCoroutineFromTypesImportCoroutineAsC mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testFunctionYieldsNone mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testCoroutineFromAsyncioImportCoroutine mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testCoroutineImportAsyncioAsA mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testCoroutineFromAsyncioCoroutinesImportCoroutine mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testCoroutineImportAsyncioCoroutinesAsC mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testCoroutineFromTypesImportCoroutine mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testCoroutineImportAsyncioCoroutinesSubAsA mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testCoroutineFromAsyncioImportCoroutineAsC mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testCoroutineImportTypesAsT mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testCoroutineFromAsyncioCoroutinesImportCoroutineAsC mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testCoroutineSpecialCase_import mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testOmitDefsNotInAll_semanal mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testVarDefsNotInAll_import mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testIncludeClassNotInAll_import mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testAllAndClass_import mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testSkipMultiplePrivateDefs mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testIncludeMultiplePrivateDefs mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testFinalInvalid_semanal mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testCrossModuleClass_semanal mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testNestedClass_semanal mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testABCMeta_semanal mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testInvalidNumberOfArgsInAnnotation mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testFunctionPartiallyAnnotated mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testExplicitAnyArg mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testExplicitReturnedAny mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testPlacementOfDecorators mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testMisplacedTypeComment mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testNoFunctionNested_semanal mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testCrossModuleFunction_semanal mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testAbstractMethodNameExpr mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testAbstractMethodMemberExpr mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testAbstractMethodMemberExpr2 mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testFunctionAlias_semanal mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testPrivateAliasesExcluded_semanal mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testAbstractProperty3_semanal mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testClassWithNameIncompleteOrOptional mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testExportedNameImported mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testVendoredSix mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testVendoredPackage mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testTestFiles mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testTestFiles_import mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testVerboseFlag mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testImportedModuleExits_import mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testImportedModuleHardExits_import mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testImportedModuleHardExits2_import mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testConditionalImportAll_semanal mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testImportedModuleHardExits3_import mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testImportedModuleHardExits4_import mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testExportInternalImportsByDefault mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testNoExportOfInternalImportsIfAll_import mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testExportInternalImportsByDefaultFromUnderscorePackage mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testDisableExportOfInternalImports mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testExportInternalImportsByDefaultUsingRelativeImport mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testExportInternalImportsByDefaultSkipPrivate mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testExportInternalImportsByDefaultIncludePrivate mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testHideDunderModuleAttributes mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testHideDunderModuleAttributesWithAll_import mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testModuleAlias_semanal mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testPrivateAliasesIncluded_semanal mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testCannotCalculateMRO_semanal mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testAttrsClass_semanal mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testNamedTupleInClass mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testImport_overwrites_directWithAlias_from mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testImport_overwrites_directWithAlias_fromWithAlias mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testImports_overwrites_direct_from mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testImports_overwrites_direct_fromWithAlias mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testImports_overwrites_from_directWithAlias mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testImports_overwrites_fromWithAlias_direct mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testImports_direct mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testOverload_fromTypingImport mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testOverload_importTyping mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testOverload_importTypingAs mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testBadAliasNested_semanal mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testImports_directImportsWithAlias mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testFinalWrapped_semanal mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testImports_directImportsMixed mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testUnreachableCode_semanal mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testProtocol_semanal mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testNonDefaultKeywordOnlyArgAfterAsterisk mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testAbstractProperty1_semanal mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testAbstractProperty2_semanal
mypy/test/teststubgen.py::StubgenPythonSuite::stubgen.test::testNestedGenerator