Repository: redis/redis-py
Pull request: Fix bug with partial Hiredis availability
### Pull Request check-list

_Please make sure to review and check all of these items:_

- [x] Do tests and lints pass with this change?
- [x] Do the CI tests pass with this change (enable it first in your forked repo and wait for the github action build to finish)?
- [x] Is the new or changed code fully tested?
- [x] Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
- [x] Is there an example added to the examples folder (if applicable)?
- [ ] Was the change added to CHANGES file?

_NOTE: these things are not required to open a PR and can be done
afterwards / while the PR is open._

### Description of change

Closes #3392 


Problem statement:
Seeking guidance for `hiredis` dependency on `redis-py`
I have a question regarding the `hiredis` dependency in `redis-py`. 
Is hiredis a required dependency for using the package, or is it optional? 

I have an implicit dependency on `redis-py`  and I've encountered an issue with a locked version of `hiredis` in my `requirements.txt`

Since `redis-py` updates automatically as it is implicit, it can lead to a version mismatch where `redis-py` is updated, but `hiredis` remains locked to an older version, causing an unsynchronized state.

Could you provide guidance on how to handle this situation and avoid any conflicts arising from the version lock?

Hints from issue discussion:
I noticed that in the recent `redis-py` update (5.1.0), the author forgot to mention the hiredis dependency, although it is mentioned [here](https://github.com/redis/redis-py/pull/3324#issue-2419053541:~:text=Upgrade%20the%20required%20hiredis%2Dpy%20version%20to%20be%20at%20least%203.0.0): 

Including such a change in the release log would be quite helpful for other developers.
So I request to add this information on release notes of 5.1.0 :) 
Hi @ramchandra-st,

it's possible to use `redis-py` without `hiredis`, but if `hiredis` is installed, then `redis-py` will use it. This is documented in the README here on Github.

Which version of `hiredis` are you using?

Regards,
David  
Hey guys, i created this issue https://github.com/redis/redis-py/issues/3393 about it, i just realised that this is talking about this same issue, should i close it?
@helissonomc Yes, please. Can you add any relevant detail here please.
With version 5.1.0 I started to face the error message:
` NameError: name 'hiredis' is not defined`
The error was introduced by the code inside redis/utils.py
```
try:
    import hiredis  # noqa

    # Only support Hiredis >= 3.0:
    HIREDIS_AVAILABLE = int(hiredis.__version__.split(".")[0]) >= 3
    HIREDIS_PACK_AVAILABLE = hasattr(hiredis, "pack_command")
except ImportError:
    HIREDIS_AVAILABLE = False
    HIREDIS_PACK_AVAILABLE = False
   ```
If the hiredis version is lower than 3 (which mine is) the HIREDIS_AVAILABLE is going to be false, therefore it will keep giving error inside the redis/connection.py file, because the `import hiredis` will not be executed, but the code still relies on this import.
Yeah, problem presented by @helissonomc is same as what I want to highlight here.

Thanks folks for engagement here :) 
OK, seems to be a bug that was introduced by trying to enforce hiredis >=3. We will take a look and fix it. Thanks for the detailed report.  
Hi,

this bug could be reproduced with `redis-py` 5.1.0 and `hiredis-py` 2.4.0. The library `redis-py` should fall back to its internal response parser if the `hiredis-py` has a lower version than 3.0. There are two boolean variables checked and `HIREDIS_PACK_AVAILABLE` can be true if any version of `hiredis-py` is installed, which causes the bug. This will be fixed in a maintenance version. We apologize for the inconvenience.

Regards,
David

Commit message: Codestyl fixes

Patch to review:
diff --git a/.github/workflows/integration.yaml b/.github/workflows/integration.yaml
index b10edf2fb4..ed969d11f1 100644
--- a/.github/workflows/integration.yaml
+++ b/.github/workflows/integration.yaml
@@ -90,6 +90,21 @@ jobs:
           invoke ${{matrix.test-type}}-tests
           ls -1
 
+      - name: Run tests against hiredis < 3.0.0
+        if: ${{ matrix.connection-type == 'hiredis' && matrix.python-version == '3.12'}}
+        run: |
+          pip uninstall hiredis -y
+          pip install -U setuptools wheel
+          pip install -r requirements.txt
+          pip install -r dev_requirements.txt
+          if [ "${{matrix.connection-type}}" == "hiredis" ]; then
+           pip install "hiredis<3.0.0"
+          fi
+          invoke devenv
+          sleep 10 # time to settle
+          invoke ${{matrix.test-type}}-tests
+          ls -1
+
       - name: Upload test results and profiling data
         uses: actions/upload-artifact@v4
         with:
@@ -145,6 +160,24 @@ jobs:
             invoke ${{matrix.test-type}}-tests --protocol=3
           fi
 
+      - name: Run tests against hiredis < 3.0.0
+        if: ${{ matrix.connection-type == 'hiredis' && matrix.python-version == '3.12'}}
+        run: |
+          pip uninstall hiredis -y
+          pip install -U setuptools wheel
+          pip install -r requirements.txt
+          pip install -r dev_requirements.txt
+          if [ "${{matrix.connection-type}}" == "hiredis" ]; then
+            pip install "hiredis<3.0.0"
+          fi
+          invoke devenv
+          sleep 10 # time to settle
+          if [ "${{matrix.event-loop}}" == "uvloop" ]; then
+            invoke ${{matrix.test-type}}-tests --uvloop --protocol=3
+          else
+            invoke ${{matrix.test-type}}-tests --protocol=3
+          fi
+
       - name: Upload test results and profiling data
         uses: actions/upload-artifact@v4
         with:
diff --git a/redis/utils.py b/redis/utils.py
index b4e9afb054..5632bb18a7 100644
--- a/redis/utils.py
+++ b/redis/utils.py
@@ -8,7 +8,13 @@
 
     # Only support Hiredis >= 3.0:
     HIREDIS_AVAILABLE = int(hiredis.__version__.split(".")[0]) >= 3
+    if not HIREDIS_AVAILABLE:
+        raise ImportError("hiredis package should be >= 3.0.0")
     HIREDIS_PACK_AVAILABLE = hasattr(hiredis, "pack_command")
+    if not HIREDIS_PACK_AVAILABLE:
+        raise ImportError(
+            "pack_command is not available in the current version of hiredis."
+        )
 except ImportError:
     HIREDIS_AVAILABLE = False
     HIREDIS_PACK_AVAILABLE = False


Code review comments:
diff_hunk: @@ -8,7 +8,13 @@
 
     # Only support Hiredis >= 3.0:
     HIREDIS_AVAILABLE = int(hiredis.__version__.split(".")[0]) >= 3
+    if not HIREDIS_AVAILABLE:
+        raise ImportError("hiredis package should be >= 3.0.0")
     HIREDIS_PACK_AVAILABLE = hasattr(hiredis, "pack_command")
path: redis/utils.py
text: @user1:
Can it actually happen that `hiredis-py` doesn't have the `pack` command if the  version is  >=3? If not, let's remove the redundant check.

@author:
Check was removed