redis/redis-py · error · ValueError

Cannot enable maintenance notifications for connection objec

Error message

Cannot enable maintenance notifications for connection object that doesn't have a host attribute.

What it means

Raised as ValueError in _enable_maintenance_notifications when getattr(self,'host',None) is None. The CLIENT MAINT_NOTIFICATIONS ON command needs an endpoint type resolved from the connection's host, so a connection without a host attribute (e.g. some mock/test doubles or unix-socket-only paths) cannot enable notifications.

Source

Thrown at redis/asyncio/connection.py:401

            and self.maint_notifications_config
            and self.maint_notifications_config.enabled
            and self._maint_notifications_connection_handler
            and host is not None
        ):
            await self._enable_maintenance_notifications(
                maint_notifications_config=self.maint_notifications_config,
                check_health=check_health,
            )

    async def _enable_maintenance_notifications(
        self,
        maint_notifications_config: MaintNotificationsConfig,
        check_health: bool = True,
    ) -> None:
        try:
            host = getattr(self, "host", None)
            if host is None:
                raise ValueError(
                    "Cannot enable maintenance notifications for connection"
                    " object that doesn't have a host attribute."
                )

            endpoint_type = maint_notifications_config.get_endpoint_type(host, self)
            await self.send_command(
                "CLIENT",
                "MAINT_NOTIFICATIONS",
                "ON",
                "moving-endpoint-type",
                endpoint_type.value,
                check_health=check_health,
            )
            response = await self.read_response()
            if not response or str_if_bytes(response) != "OK":
                raise ResponseError(
                    "The server doesn't support maintenance notifications"
                )

View on GitHub (pinned to da03cdc7e8)

Solutions

  1. Ensure self.host is set on the connection (provide host= when constructing, or set it in your subclass before connect).
  2. Disable maintenance notifications for connections that legitimately have no host (tests, in-memory transports).
  3. If using unix sockets or unusual transports, configure them so host resolves to a usable endpoint identifier.

Example fix

// before
class TestConn(AsyncConnection):
    def __init__(self, *a, **kw):
        super().__init__(*a, host=None, **kw)  # later raises [92]
// after
class TestConn(AsyncConnection):
    def __init__(self, *a, **kw):
        super().__init__(*a, host='127.0.0.1', **kw)
Defensive patterns

Strategy: validation

Validate before calling

if cfg.enabled and getattr(conn, 'host', None) is None:
    raise ValueError('Connection needs a host to enable maintenance notifications')

Try / catch

try:
    await conn.connect()
except ValueError as e:
    if "doesn't have a host attribute" in str(e):
        conn.host = '127.0.0.1'; await conn.connect()

Prevention

When it happens

Trigger: Constructing a Connection subclass that doesn't set self.host (or sets it to None) while maintenance notifications are enabled and protocol=3, then triggering the on_connect health-check path that calls _enable_maintenance_notifications.

Common situations: Using a Connection subclass for testing, or a custom transport (e.g. in-memory, unix socket configured differently) that never assigns host; passing a Connection into a context that expects maintenance notifications without a real host.

Related errors


AI-assisted analysis of redis/redis-py@da03cdc7e8 (2026-08-04). Data as JSON: /data/errors/55c04028f08ba389.json. Report an issue: GitHub.