@@ -846,3 +846,49 @@ or of potential subclasses.
846846 # In the context of the subclass Bar, the Self return type promises
847847 # that the return value will be Bar
848848 reveal_type(Bar.constructor()) # note: Revealed type is "Bar"
849+
850+ Missing ``yield `` statement in generator functions
851+ --------------------------------------------------
852+
853+ Python uses the ``yield `` statement to determine whether a function is a
854+ generator function (https://peps.python.org/pep-0255/#specification-yield).
855+
856+ .. code-block :: python
857+
858+ from collections.abc import Generator, AsyncGenerator
859+
860+ async def async_gen () -> AsyncGenerator[int ]:
861+ yield 1
862+
863+ def gen () -> Generator[int ]:
864+ yield 1
865+
866+ async def async_func () -> int :
867+ return 1
868+
869+ def func () -> int :
870+ return 1
871+
872+ reveal_type(async_gen()) # AsyncGenerator[int, None]
873+ reveal_type(gen()) # Generator[int, None, None]
874+ reveal_type(async_func()) # Coroutine[Any, Any, int]
875+ reveal_type(func()) # int
876+
877+ Functions containing ``yield `` statements will be regarded as Generator functions
878+ by Python, and their return types are different from other functions.
879+ Therefore, when using ``mypy `` to check types, we need to declare the return types
880+ of such functions as Generator (or AsyncGenerator when the function is async).
881+
882+ A common mistake is that we declared the return type of a function as
883+ Generator (AsyncGenerator) but did not use the ``yield `` statement in the function.
884+ ``mypy `` will consider that the return type (no return, or normal types) of this function
885+ does not meet the expectation (generator type).
886+
887+ .. code-block :: python
888+
889+ from collections.abc import Generator
890+
891+ def gen () -> Generator[int ]:
892+ ... # error: Missing return statement
893+
894+ This kind of mistake is common in unfinished functions (the function body is ``... ``).
0 commit comments