Skip to content

Stubgen applies Generator type to function with nested generator #11893

@ska-kialo

Description

@ska-kialo

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:
    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.

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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions