{"record":{"id":"34ae83561d1d90c1","repo":"python/cpython","slug":"loop-must-be-an-instance-of-abstracteventloop-or-n","errorCode":null,"errorMessage":"loop must be an instance of AbstractEventLoop or None, not '{type(loop).__name__}'","messagePattern":"loop must be an instance of AbstractEventLoop or None, not '(.+?)'","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Lib/asyncio/events.py","lineNumber":755,"sourceCode":"\n    If there is no running event loop set, the function will return\n    the loop set by ``set_event_loop()``, or raise a RuntimeError if\n    no loop has been set.\n    \"\"\"\n    # NOTE: this function is implemented in C (see _asynciomodule.c)\n    current_loop = _get_running_loop()\n    if current_loop is not None:\n        return current_loop\n    return _get_event_loop()\n\n\ndef set_event_loop(loop):\n    \"\"\"Set the event loop for the current thread to loop.\n\n    If loop is None, the current event loop is unset.\n    \"\"\"\n    if loop is not None and not isinstance(loop, AbstractEventLoop):\n        raise TypeError(f\"loop must be an instance of AbstractEventLoop or None, not '{type(loop).__name__}'\")\n    _local._loop = loop\n\n\ndef new_event_loop():\n    \"\"\"Create and return a new event loop object.\"\"\"\n    if sys.platform == 'win32':\n        from .windows_events import EventLoop\n    else:\n        from .unix_events import EventLoop\n    return EventLoop()\n\n\n# Alias pure-Python implementations for testing purposes.\n_py__get_running_loop = _get_running_loop\n_py__set_running_loop = _set_running_loop\n_py_get_running_loop = get_running_loop\n_py_get_event_loop = get_event_loop\n","sourceCodeStart":737,"sourceCodeEnd":773,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/asyncio/events.py#L737-L773","documentation":"asyncio.set_event_loop() validates its argument: it must be an AbstractEventLoop instance or None. Passing anything else (a factory, a coroutine, a mock, a loop policy) raises this TypeError immediately, guarding the thread-local loop slot from corrupt state.","triggerScenarios":"Calling set_event_loop(new_event_loop) with missing parentheses, passing the EventLoop class instead of an instance, passing a concurrent.futures executor, or a Mock object in tests without spec.","commonSituations":"Typos in test setup; copying example code that stored a loop factory; mocking asyncio and forgetting spec=asyncio.AbstractEventLoop; passing the result of uvloop.Loop (class) instead of uvloop.new_event_loop().","solutions":["Instantiate the loop: asyncio.set_event_loop(asyncio.new_event_loop())","To clear the thread's loop, pass None explicitly","In tests, use unittest.mock patches with spec, or pytest-asyncio fixtures instead of manual set_event_loop"],"exampleFix":"# before\nasyncio.set_event_loop(asyncio.new_event_loop)  # passed the function\n\n# after\nasyncio.set_event_loop(asyncio.new_event_loop())  # passed an instance","handlingStrategy":"type-guard","validationCode":"import asyncio\nassert isinstance(loop, (asyncio.AbstractEventLoop, type(None)))","typeGuard":"def is_loop_or_none(obj) -> bool:\n    return obj is None or isinstance(obj, asyncio.AbstractEventLoop)","tryCatchPattern":null,"preventionTips":["Always pass new_event_loop() result, not the function","Lint for set_event_loop(asyncio.new_event_loop) (missing parens)","Use spec-ed mocks in tests touching loop APIs"],"tags":["asyncio","event-loop","typeerror"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}