Repository: redis/redis-py

Issue:
'UnixDomainSocketConnection' object has no attribute '_command_packer'
**Version**: v4.5.0

**Platform**: Debian Bullseye using Python v3.9.2

**Description**: 
The following code which used to work on up to and including v4.4.2 now crashes with the stack trace below on v4.5.0.

```python
redis_conn = redis.Redis(
        unix_socket_path="/var/run/redis/redis-server.sock",
        decode_responses=True)
redis_conn.ping()
```

```
Traceback (most recent call last):
  File "<redacted>", line 142, in main
    redis_conn.ping()
  File "/usr/local/lib/python3.9/dist-packages/redis/commands/core.py", line 1194, in ping
    return self.execute_command("PING", **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/redis/client.py", line 1258, in execute_command
    return conn.retry.call_with_retry(
  File "/usr/local/lib/python3.9/dist-packages/redis/retry.py", line 46, in call_with_retry
    return do()
  File "/usr/local/lib/python3.9/dist-packages/redis/client.py", line 1259, in <lambda>
    lambda: self._send_command_parse_response(
  File "/usr/local/lib/python3.9/dist-packages/redis/client.py", line 1234, in _send_command_parse_response
    conn.send_command(*args)
  File "/usr/local/lib/python3.9/dist-packages/redis/connection.py", line 916, in send_command
    self._command_packer.pack(*args),
AttributeError: 'UnixDomainSocketConnection' object has no attribute '_command_packer'
```


Discussion:
also seeing this in our apps, had to pin to 4.4.2 to prevent our django app from crashing anytime it attempted to use the cache
I guess this is the issue:

```python
class Connection:
    def __init__(self, ...):
        ...
        self._command_packer = self._construct_command_packer(command_packer)

class SSLConnection(Connection):
    def __init__(self, ...):
        ...
        super().__init__(...)

class UnixDomainSocketConnection(Connection):
    def __init__(self, ...):
         # does not call _construct_command_packer nor super().__init__()
```
Seems like the bug was introduced just before the 4.5.0 release: https://github.com/redis/redis-py/pull/2570

@prokazov Could you take a look please?

Strange that this does not get picked up by the tests.
Looks like there is no synchronous tests that run any command on SSLConnection/UnixDomainSocketConnection.
Actually `SSLConnection` does call `super().__init__` so that one should be fine.
The fact that `UnixDomainSocketConnection.__init__ ` does not call `super().__init__` is probably worth fixing. I see lots of copy&paste so seems doable? Perhaps introduce an `AbstractConnection` with abstract methods `_connect`, `repr_pieces` and `_error_message`?
`Connection` has `host` and `port` while `UnixDomainSocketConnection` has `path`.

`Connection` has in addition `socket_connect_timeout`, `socket_keepalive `, `socket_keepalive_options` and `socket_type`.

Lastly there is `if retry or retry_on_error:` vs. `if retry_on_error:`. This discrepancy is probably also a mistake https://github.com/redis/redis-py/pull/2377 @barshaul ?

The rest is identical.

Resolving patch:
diff --git a/redis/connection.py b/redis/connection.py
--- a/redis/connection.py
+++ b/redis/connection.py
@@ -1153,6 +1153,7 @@ def __init__(
         retry=None,
         redis_connect_func=None,
         credential_provider: Optional[CredentialProvider] = None,
+        command_packer=None,
     ):
         """
         Initialize a new UnixDomainSocketConnection.
@@ -1202,6 +1203,7 @@ def __init__(
         self.set_parser(parser_class)
         self._connect_callbacks = []
         self._buffer_cutoff = 6000
+        self._command_packer = self._construct_command_packer(command_packer)
 
     def repr_pieces(self):
         pieces = [("path", self.path), ("db", self.db)]


Test patch:
diff --git a/tests/test_connection.py b/tests/test_connection.py
--- a/tests/test_connection.py
+++ b/tests/test_connection.py
@@ -7,7 +7,13 @@
 
 import redis
 from redis.backoff import NoBackoff
-from redis.connection import Connection, HiredisParser, PythonParser
+from redis.connection import (
+    Connection,
+    HiredisParser,
+    PythonParser,
+    SSLConnection,
+    UnixDomainSocketConnection,
+)
 from redis.exceptions import ConnectionError, InvalidResponse, TimeoutError
 from redis.retry import Retry
 from redis.utils import HIREDIS_AVAILABLE
@@ -163,3 +169,39 @@ def test_connection_parse_response_resume(r: redis.Redis, parser_class):
         pytest.fail("didn't receive a response")
     assert response
     assert i > 0
+
+
+@pytest.mark.onlynoncluster
+@pytest.mark.parametrize(
+    "Class",
+    [
+        Connection,
+        SSLConnection,
+        UnixDomainSocketConnection,
+    ],
+)
+def test_pack_command(Class):
+    """
+    This test verifies that the pack_command works
+    on all supported connections. #2581
+    """
+    cmd = (
+        "HSET",
+        "foo",
+        "key",
+        "value1",
+        b"key_b",
+        b"bytes str",
+        b"key_i",
+        67,
+        "key_f",
+        3.14159265359,
+    )
+    expected = (
+        b"*10\r\n$4\r\nHSET\r\n$3\r\nfoo\r\n$3\r\nkey\r\n$6\r\nvalue1\r\n"
+        b"$5\r\nkey_b\r\n$9\r\nbytes str\r\n$5\r\nkey_i\r\n$2\r\n67\r\n$5"
+        b"\r\nkey_f\r\n$13\r\n3.14159265359\r\n"
+    )
+
+    actual = Class().pack_command(*cmd)[0]
+    assert actual == expected, f"actual = {actual}, expected = {expected}"