Skip to content

_asyncio._get_running_loop can return None#15837

Merged
JelleZijlstra merged 1 commit into
python:mainfrom
jonathandung:patch-4
May 27, 2026
Merged

_asyncio._get_running_loop can return None#15837
JelleZijlstra merged 1 commit into
python:mainfrom
jonathandung:patch-4

Conversation

@jonathandung
Copy link
Copy Markdown
Contributor

I recently found out the type checker I was using marked a branch where asyncio.events._get_running_loop returns None as unreachable. However, this simple snippet will show otherwise:

$ python -c "print(__import__('asyncio')._get_running_loop())"
None

None is returned when there is no running loop, as per the documentation:

$ python -m pydoc asyncio._get_running_loop
Help on built-in function _get_running_loop in asyncio:                                                                                                                        

asyncio._get_running_loop = _get_running_loop()
    Return the running event loop or None.

    This is a low-level function intended to be used by event loops.
    This function is thread-specific.

In the stubs, asyncio.events reexports _asyncio._get_running_loop, and the asyncio top level pulls that in. This thus affects all three locations where the symbol is available. Typing the return type as a union AbstractEventLoop | None fixes this.

@github-actions
Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@JelleZijlstra JelleZijlstra merged commit 1d3abc4 into python:main May 27, 2026
58 checks passed
@jonathandung jonathandung deleted the patch-4 branch May 27, 2026 13:29
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.

2 participants