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

Code before:

        self._event_dispatcher = self.connection_kwargs.get("event_dispatcher", None)
        if self._event_dispatcher is None:
            self._event_dispatcher = EventDispatcher()

        # a lock to protect the critical section in _checkpid().
        # this lock is acquired when the process id changes, such as
        # after a fork. during this time, multiple threads in the child
        # process could attempt to acquire this lock. the first thread
        # to acquire the lock will reset the data structures and lock
        # object of this pool. subsequent threads acquiring this lock
        # will notice the first thread already did the work and simply
        # release the lock.
        self._fork_lock = threading.Lock()
        self._lock = threading.Lock()
        self.reset()

    def __repr__(self) -> str:
        arg = ""
        if "host" in self.connection_kwargs:
            arg = f"{self.connection_kwargs['host']}:{self.connection_kwargs['port']}"
        elif "path" in self.connection_kwargs:
            arg = self.connection_kwargs["path"]

        return (
            f"<{type(self).__module__}.{type(self).__name__}"
            f"({self.connection_class.__name__}<{arg}>)>"
        )

    def get_protocol(self):
        """
        Returns:
            The RESP protocol version, or ``None`` if the protocol is not specified,
            in which case the server default will be used.
        """
        return self.connection_kwargs.get("protocol", None)

    def reset(self) -> None:
        self._created_connections = 0
        self._available_connections = []
        self._in_use_connections = set()

        # this must be the last operation in this method. while reset() is
        # called when holding _fork_lock, other threads in this process
        # can call _checkpid() which compares self.pid and os.getpid() without
        # holding any lock (for performance reasons). keeping this assignment
        # as the last operation ensures that those other threads will also
        # notice a pid difference and block waiting for the first thread to
        # release _fork_lock. when each of these threads eventually acquire
        # _fork_lock, they will notice that another thread already called
        # reset() and they will immediately release _fork_lock and continue on.

Review comment (suggestion):
I suggest something like the following:
`connection_kwargs_repr = ",".join(
            [f"{k}={v}" for k, v in self.connection_kwargs.items()]
        )
        return (
            f"<{self.__class__.__module__}.{self.__class__.__name__}"
            f"({self.connection_class.__module__}.{self.connection_class.__name__}"
            f"({connection_kwargs_repr})))>"
        )`

With this representation, it will be obvious what the Connection class is and from which module(it can be custom).
We will also be able to see all the provided kwargs when a new connection is instantiated.

Although this covers most of the cases, with the above code, the default args set to the connection are still missing and it would require fixing some unit tests.

Code after:

        self._event_dispatcher = self.connection_kwargs.get("event_dispatcher", None)
        if self._event_dispatcher is None:
            self._event_dispatcher = EventDispatcher()

        # a lock to protect the critical section in _checkpid().
        # this lock is acquired when the process id changes, such as
        # after a fork. during this time, multiple threads in the child
        # process could attempt to acquire this lock. the first thread
        # to acquire the lock will reset the data structures and lock
        # object of this pool. subsequent threads acquiring this lock
        # will notice the first thread already did the work and simply
        # release the lock.

        self._fork_lock = threading.RLock()

        if self.cache is None:
            self._lock = threading.RLock()
        else:
            # TODO: To avoid breaking changes during the bug fix, we have to keep non-reentrant lock.
            # TODO: Remove this before next major version (7.0.0)
            self._lock = threading.Lock()

        self.reset()

    def __repr__(self) -> str:
        conn_kwargs = ",".join([f"{k}={v}" for k, v in self.connection_kwargs.items()])
        return (
            f"<{self.__class__.__module__}.{self.__class__.__name__}"
            f"(<{self.connection_class.__module__}.{self.connection_class.__name__}"
            f"({conn_kwargs})>)>"
        )

    def get_protocol(self):
        """
        Returns:
            The RESP protocol version, or ``None`` if the protocol is not specified,
            in which case the server default will be used.
        """
        return self.connection_kwargs.get("protocol", None)

    def reset(self) -> None:
        self._created_connections = 0
        self._available_connections = []
        self._in_use_connections = set()

        # this must be the last operation in this method. while reset() is
        # called when holding _fork_lock, other threads in this process
        # can call _checkpid() which compares self.pid and os.getpid() without
        # holding any lock (for performance reasons). keeping this assignment
        # as the last operation ensures that those other threads will also