python/cpython · error · TypeError

task factory must be a callable or None

Error message

task factory must be a callable or None

What it means

Error "task factory must be a callable or None" thrown in python/cpython.

Source

Thrown at Lib/asyncio/base_events.py:497

            return task
        finally:
            # gh-128552: prevent a refcycle of
            # task.exception().__traceback__->BaseEventLoop.create_task->task
            del task

    def set_task_factory(self, factory):
        """Set a task factory that will be used by loop.create_task().

        If factory is None the default task factory will be set.

        If factory is a callable, it should have a signature matching
        '(loop, coro, **kwargs)', where 'loop' will be a reference to the
        active event loop, 'coro' will be a coroutine object, and **kwargs
        will be arbitrary keyword arguments that should be passed on to
        Task.  The callable must return a Task.
        """
        if factory is not None and not callable(factory):
            raise TypeError('task factory must be a callable or None')
        self._task_factory = factory

    def get_task_factory(self):
        """Return a task factory, or None if the default one is in use."""
        return self._task_factory

    def _make_socket_transport(self, sock, protocol, waiter=None, *,
                               extra=None, server=None):
        """Create socket transport."""
        raise NotImplementedError

    def _make_ssl_transport(
            self, rawsock, protocol, sslcontext, waiter=None,
            *, server_side=False, server_hostname=None,
            extra=None, server=None,
            ssl_handshake_timeout=None,
            ssl_shutdown_timeout=None,
            call_connection_made=True,

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. Pass a callable (e.g. a function or lambda) to loop.set_task_factory(), or None to reset it.
  2. Ensure the factory signature is factory(loop, coro) and returns a Task.

When it happens

Trigger: Raised when loop.set_task_factory() is given a value that is neither callable nor None.

Common situations: See trigger scenarios.


AI-assisted analysis of python/cpython@bc6749cc3b (2026-08-14). Data as JSON: /api/errors/a6b9fff8b724c2d4. Report an issue: GitHub.