Skip to content

[stdlib][types] fix *_frame type (may be None) - #15219

Merged
srittau merged 1 commit into
python:mainfrom
x42005e1f:main
Jan 6, 2026
Merged

[stdlib][types] fix *_frame type (may be None)#15219
srittau merged 1 commit into
python:mainfrom
x42005e1f:main

Conversation

@x42005e1f

Copy link
Copy Markdown
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
Copy Markdown
Contributor

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

x42005e1f commented Jan 5, 2026

Copy link
Copy Markdown
ContributorAuthor

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-1094010374defgetgeneratorstate(generator):
...
ifgenerator.gi_frameisNone:
returnGEN_CLOSED
...
# Python ≥3.5 (PEP 492)defgetcoroutinestate(coroutine):
...
ifcoroutine.cr_frameisNone:
returnCORO_CLOSED
...
# Python ≥3.12 (but the behavior is present on ≥3.6 — PEP 525)defgetasyncgenstate(agen):
...
ifagen.ag_frameisNone:
returnAGEN_CLOSED
...

@srittausrittau left a comment

Copy link
Copy Markdown
Collaborator

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:mainJan 6, 2026
63 checks passed
@cdce8pcdce8p mentioned this pull request Jan 17, 2026
Sign up for freeto 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

@x42005e1f@srittau