Repository: redis/redis-py
File: redis/asyncio/connection.py (Python)

Code before:

    def deregister_connect_callback(self, callback):
        """
        De-register a previously registered callback.  It will no-longer receive
        notifications on connection events.  Calling this is not required when the
        listener goes away, since the callbacks are kept as weak methods.
        """
        try:
            self._connect_callbacks.remove(weakref.WeakMethod(callback))
        except ValueError:
            pass

    def set_parser(self, parser_class: Type[BaseParser]) -> None:
        """
        Creates a new instance of parser_class with socket size:
        _socket_read_size and assigns it to the parser for the connection
        :param parser_class: The required parser class
        """
        self._parser = parser_class(socket_read_size=self._socket_read_size)

    async def connect(self):
        """Connects to the Redis server if not already connected"""
        # try once the socket connect with the handshake, retry the whole
        # connect/handshake flow based on retry policy
        await self.retry.call_with_retry(
            lambda: self.connect_check_health(retry_socket_connect=False),
            lambda error: self.disconnect(),
        )

    async def connect_check_health(
        self, check_health: bool = True, retry_socket_connect: bool = True
    ):
        if self.is_connected:
            return
        try:
            if retry_socket_connect:
                await self.retry.call_with_retry(
                    lambda: self._connect(), lambda error: self.disconnect()
                )
            else:
                await self._connect()
        except asyncio.CancelledError:
            raise  # in 3.7 and earlier, this is an Exception, not BaseException
        except (socket.timeout, asyncio.TimeoutError):
            raise TimeoutError("Timeout connecting to server")
        except OSError as e:
            raise ConnectionError(self._error_message(e))
        except Exception as exc:
            raise ConnectionError(exc) from exc

        try:

Review comment (suggestion):
The `check_health` parameter is not passed to `connect_check_health` in the async version, while it is explicitly set to `True` in the synchronous version (line 850 in redis/connection.py). For consistency and to ensure the same behavior, this should be: `lambda: self.connect_check_health(check_health=True, retry_socket_connect=False)`
```suggestion
            lambda: self.connect_check_health(check_health=True, retry_socket_connect=False),
```

Code after:

    def deregister_connect_callback(self, callback):
        """
        De-register a previously registered callback.  It will no-longer receive
        notifications on connection events.  Calling this is not required when the
        listener goes away, since the callbacks are kept as weak methods.
        """
        try:
            self._connect_callbacks.remove(weakref.WeakMethod(callback))
        except ValueError:
            pass

    def set_parser(self, parser_class: Type[BaseParser]) -> None:
        """
        Creates a new instance of parser_class with socket size:
        _socket_read_size and assigns it to the parser for the connection
        :param parser_class: The required parser class
        """
        self._parser = parser_class(socket_read_size=self._socket_read_size)

    async def connect(self):
        """Connects to the Redis server if not already connected"""
        # try once the socket connect with the handshake, retry the whole
        # connect/handshake flow based on retry policy
        await self.retry.call_with_retry(
            lambda: self.connect_check_health(
                check_health=True, retry_socket_connect=False
            ),
            lambda error: self.disconnect(),
        )

    async def connect_check_health(
        self, check_health: bool = True, retry_socket_connect: bool = True
    ):
        if self.is_connected:
            return
        try:
            if retry_socket_connect:
                await self.retry.call_with_retry(
                    lambda: self._connect(), lambda error: self.disconnect()
                )
            else:
                await self._connect()
        except asyncio.CancelledError:
            raise  # in 3.7 and earlier, this is an Exception, not BaseException
        except (socket.timeout, asyncio.TimeoutError):
            raise TimeoutError("Timeout connecting to server")
        except OSError as e:
            raise ConnectionError(self._error_message(e))
        except Exception as exc:
            raise ConnectionError(exc) from exc