Skip to content

feat: add unused-asyncio-task error code for asyncio.create_task without reference (#14710) - #22147

Open
DawnofGenX wants to merge 3 commits into
python:masterfrom
DawnofGenX:unused-asyncio-task
Open

DawnofGenX wants to merge 3 commits into
python:masterfrom
DawnofGenX:unused-asyncio-task

Conversation

@DawnofGenX

Copy link
Copy Markdown

Summary

Adds a new opt-in error code unused-asyncio-task that warns when asyncio.create_task() or asyncio.ensure_future() is called without holding a reference to the returned task. This is a known Python gotcha where the task can be garbage collected before it completes.

Changes

  • mypy/errorcodes.py: Added UNUSED_ASYNCIO_TASK error code (disabled by default)
  • mypy/checker.py: Added check in type_requires_usage() for asyncio.create_task and asyncio.ensure_future calls
  • test-data/unit/check-async-await.test: Added two test cases covering both error and no-error scenarios

Test Results

  • testAsyncioCreateTaskWithoutReference: PASSED
  • testAsyncioCreateTaskWithReference: PASSED
  • All 79 async tests: PASSED

Fixes #14710

@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

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.

Add rule to detect potential bug caused by unheld references to asyncio.create_task

1 participant