Skip to content

Conversation

@x42005e1f
Copy link
Contributor

This is a simple PR that adds None as a possible type for GeneratorType.gi_frame, CoroutineType.cr_frame, and AsyncGeneratorType.ag_frame: the property value becomes None when the object completes (e.g. closes). It is valid for all supported versions of Python (and even older ones).

Closes #15218.

@github-actions
Copy link
Contributor

github-actions bot commented Jan 5, 2026

Diff from mypy_primer, showing the effect of this PR on open source code:

trio (https://github.com/python-trio/trio)
+ src/trio/_util.py:301: error: Item "None" of "FrameType | None" has no attribute "f_globals"  [union-attr]
+ src/trio/_core/_tests/test_asyncgen.py:309: error: Item "None" of "FrameType | None" has no attribute "f_locals"  [union-attr]
+ src/trio/_core/_tests/test_asyncgen.py:313: error: Item "None" of "FrameType | None" has no attribute "f_locals"  [union-attr]

@x42005e1f
Copy link
Contributor Author

x42005e1f commented Jan 5, 2026

The claim that it is valid for all supported versions of Python can also be verified via the inspect.get*state() source code:

# Python ≥3.2 (but the behavior is present on ≥2.5 — PEP 342)
# https://github.com/python/cpython/issues/42099#issuecomment-1094010374
def getgeneratorstate(generator):
    ...
    if generator.gi_frame is None:
        return GEN_CLOSED
    ...
# Python ≥3.5 (PEP 492)
def getcoroutinestate(coroutine):
    ...
    if coroutine.cr_frame is None:
        return CORO_CLOSED
    ...
# Python ≥3.12 (but the behavior is present on ≥3.6 — PEP 525)
def getasyncgenstate(agen):
    ...
    if agen.ag_frame is None:
        return AGEN_CLOSED
    ...

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@srittau srittau merged commit 934b047 into python:main Jan 6, 2026
63 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

types.GeneratorType.gi_frame should be FrameType | None

2 participants