๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ typing.html
typing โ€” Support for type hints
The argument list must be a list of types, a ParamSpec, Concatenate, or an ellipsis (...). The return type must be a single type. If a literal ellipsis ... is given as the argument list, it indicates that a callable with any arbitrary parameter list would be acceptable: def concat(x: str, y: str) -> str: return x + y x: Callable[..., str] x = str # OK x = concat # Also OK ยท Callable cannot express complex signatures such as functions that take a variadic number of arguments, overloaded functions, or functions that have keyword-only parameters.
๐ŸŒ
Real Python
realpython.com โ€บ ref โ€บ keywords โ€บ type
type | Python Keywords โ€“ Real Python
Hereโ€™s a quick example demonstrating how to use the type keyword to define a type alias for either a list or set: ... In this guide, you'll look at Python type checking. Traditionally, types have been handled by the Python interpreter in a flexible but implicit way.
๐ŸŒ
Mypy
mypy.readthedocs.io โ€บ en โ€บ stable โ€บ cheat_sheet_py3.html
Type hints cheat sheet - mypy 1.19.1 documentation
# For most types, just use the name of the type in the annotation # Note that mypy can usually infer the type of a variable from its value, # so technically these annotations are redundant x: int = 1 x: float = 1.0 x: bool = True x: str = "test" x: bytes = b"test" # For collections on Python 3.9+, the type of the collection item is in brackets x: list[int] = [1] x: set[int] = {6, 7} # For mappings, we need the types of both keys and values x: dict[str, float] = {"field": 2.0} # Python 3.9+ # For tuples of fixed size, we specify the types of all the elements x: tuple[int, str, float] = (3, "yes
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ types.html
types โ€” Dynamic type creation and names for built-in types
Source code: Lib/types.py This module defines utility functions to assist in dynamic creation of new types. It also defines names for some object types that are used by the standard Python interpre...
๐ŸŒ
Python
peps.python.org โ€บ pep-0695
PEP 695 โ€“ Type Parameter Syntax | peps.python.org
A type checker # should generate an error in this case because this method uses the # new syntax for type parameters, and all type parameters associated # with the method must be explicitly declared. In this case, ``K`` # is not declared by "method2", nor is it supplied by a new-style # type parameter defined in an outer scope. def method2[M](self, a: M, b: K) -> M | K: ... This PEP introduces a new soft keyword type.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_generators.asp
Python Generators
Python Overview Python Built-in ... Bootcamp Python Certificate Python Training ... Generators are functions that can pause and resume their execution....
๐ŸŒ
FastAPI
fastapi.tiangolo.com โ€บ python-types
Python Types Intro - FastAPI
By declaring types for your variables, editors and tools can give you better support. This is just a quick tutorial / refresher about Python type hints. It covers only the minimum necessary to use them with FastAPI...
Top answer
1 of 5
216

You need to import the typing module. As per the documentation:

The return type of generator functions can be annotated by the generic type Generator[yield_type, send_type, return_type] provided by typing.py module

Try this way instead:

from typing import Generator

def generate() -> Generator[int, None, None]:
    for i in range(10):
        yield i

The above will have the desired result:

l = [i for i in generate()]

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

As pointed out in the comments, you might not use the last version of PyCharm. Try switching to version 2016.3.2 and you might be fine. Unfortunately, this is a well-known bug, as per Ashwini Chaudhary's comment.

More, the reported issue (for the last version of PyCharm) was submitted in December 2016. They probably fixed it and pushed the modifications into the same version.

2 of 5
194

This isn't a direct answer to the question, but I think it is a better solution.

I'm using the typing specification below, using Iterator[int] instead of Generator. The validation is OK. I think it is a lot clearer. It better describes the code intention and is recommended by the Python documentation.

from typing import Iterator

def generate() -> Iterator[int]:
    for i in range(10):
        yield i

It would also allow future refactorings if you change your Generator for a list or other iterable.

I'm using Visual Studio Code with Pylance for typing validation. PyCharm mypy should have the same behavior.

If you are using Python 3.10 or later, change the import command above to:

from collections.abc import Iterator
๐ŸŒ
Skypage
skywork.ai โ€บ skypage โ€บ en โ€บ mastering-python-generator-hinting โ€บ 2016775811354783744
Mastering Python Generator Type Hinting: A Deep Dive with Skywork AI (2026)
The syntax is clear and maps directly to the three channels we just discussed: from collections.abc import Generator # Generator[YieldType, SendType, ReturnType] def my_generator() -> Generator[int, str, bool]: sent_value = yield 1 print(f"Received: {sent_value}") # sent_value will be a 'str' yield 2 return True # The final return value is a 'bool' This annotation tells any type checker (like Mypy or Pyright) that my_generator produces integers, expects strings to be sent into it, and will ultimately return a boolean. If your generator doesn't accept sent values or have an explicit return, you can use None for those type parameters. The three core components of a Python generator's type signature.
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_ref_keywords.asp
Python Keywords
Python Functions Python Arguments Python *args / **kwargs Python Scope Python Decorators Python Lambda Python Recursion Python Generators Code Challenge Python Range
๐ŸŒ
Real Python
realpython.com โ€บ python-keywords
Python Keywords: An Introduction โ€“ Real Python
February 12, 2025 - Pythonโ€™s yield keyword is kind of like the return keyword in that it specifies what gets returned from a function. However, when a function has a yield statement, what gets returned is a generator.
๐ŸŒ
Python
peps.python.org โ€บ pep-0484
PEP 484 โ€“ Type Hints | peps.python.org
Some functions are designed to take their arguments only positionally, and expect their callers never to use the argumentโ€™s name to provide that argument by keyword. All arguments with names beginning with __ are assumed to be positional-only, except if their names also end with __: def quux(__x: int, __y__: int = 0) -> None: ... quux(3, __y__=1) # This call is fine. quux(__x=3) # This call is an error. The return type of generator functions can be annotated by the generic type Generator[yield_type, send_type, return_type] provided by typing.py module:
๐ŸŒ
Python
docs.python.org โ€บ 3.9 โ€บ library โ€บ typing.html
typing โ€” Support for type hints โ€” Python 3.9.24 documentation
A generator can be annotated by the generic type Generator[YieldType, SendType, ReturnType]. For example:
๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
Use type keyword for defining NewTypes in Non-Generic types - Ideas - Discussions on Python.org
February 7, 2024 - Now type aliases for Non-generic types are almost useless, like the one showed in the PEP 484 Url = str def retry(url: Url, retry_count: int) -> None: ... With the modern syntax, we would have: type Url = str def retry(url: Url, retry_count: int) -> None: ... Which wonโ€™t do anything in static analysis: test: str = 'not_an_url' test1 = retry(test) # Doesn't give a static warning test = 'not_an_url' test2 = retry(test) # Doesn't give a warning, but may be nice based on --strict test: Url ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_func_type.asp
Python type() Function
Python Functions Python Arguments Python *args / **kwargs Python Scope Python Decorators Python Lambda Python Recursion Python Generators Code Challenge Python Range
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ generators-in-python
Generators in Python - GeeksforGeeks
A generator function is a special type of function that returns an iterator object. Instead of using return to send back a single value, generator functions use yield to produce a series of results over time.
Published ย  December 12, 2025
๐ŸŒ
Studytonight
studytonight.com โ€บ python โ€บ python-generators
Python Generators and Generator Expressions | Studytonight
The magic recipe to convert a simple function into a generator function is the yield keyword. We can have a single or multiple yield statements to return some data from the generator where each time the generator is called the yield statement stores the state of the local variables and yields ...