Skip to content

types.GeneratorType.gi_frame should be FrameType | None #15218

@x42005e1f

Description

@x42005e1f

typeshed/stdlib/types.pyi

Lines 402 to 403 in 7fde970

@property
def gi_frame(self) -> FrameType: ...

This issue is similar to #14780. The gi_frame property becomes None when the generator iterator completes:

>>> def generator_function():
...     return
...     yield
>>> gen = generator_function()
>>> gen.gi_frame
<frame at 0x7f2ebe641620, file '<stdin>', line 1, code generator_function>
>>> next(gen)  # consume
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
>>> gen.gi_frame is None
True

The same applies to AsyncGeneratorType.ag_frame:

>>> async def asyncgen_function():
...     return
...     yield
>>> asyncgen = asyncgen_function()
>>> asyncgen.ag_frame
<frame at 0x7f37cf7d5c60, file '<stdin>', line 1, code asyncgen_function>
>>> [*asyncgen.asend(None).__await__()]  # consume
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopAsyncIteration
>>> asyncgen.ag_frame is None
True

Notably, this was mentioned in both the linked issue and python/cpython#112428 (Python 3.12.1), to which it refers, but was ignored in the PR (#14802), meaning the issue was closed prematurely (why?). Moreover, none of the *_frame properties actually depend on the Python version. 3.12.0 introduced a regression, as mentioned in python/cpython#111058. The above examples (and a similar one for coroutine objects) can be easily reproduced even on Python 3.6:

Python 3.6.15 (default, Apr 25 2022, 01:55:53) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> async def coroutine_function():
...     pass
>>> coro = coroutine_function()
>>> coro.cr_frame
<frame object at 0x7fe26e728a08>
>>> coro.send(None)  # consume
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
>>> coro.cr_frame is None
True

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions