Repository: cunla/fakeredis-py

Issue:
AttributeError: 'FakeSocket' object has no attribute '_type'
**Describe the bug**
When calling `scan_iter` with `_type` as argument, it throws AttributeError.

**To Reproduce**

Minimal reproducible example:
```py
from fakeredis import FakeRedis
r = FakeRedis()
r.zadd("key", {"a": 2})
for _ in r.scan_iter(match="key", _type="ZSET"):
    pass
```
Trace:
```
Traceback (most recent call last):
  File "/mre/file.py", line 5, in <module>
    for _ in r.scan_iter(match="key", _type="ZSET"):
  File "/mre/.venv/lib/python3.10/site-packages/redis/commands/core.py", line 2939, in scan_iter
    cursor, data = self.scan(
  File "/mre/.venv/lib/python3.10/site-packages/redis/commands/core.py", line 2914, in scan
    return self.execute_command("SCAN", *pieces, **kwargs)
  File "/mre/.venv/lib/python3.10/site-packages/redis/client.py", line 1258, in execute_command
    return conn.retry.call_with_retry(
  File "/mre/.venv/lib/python3.10/site-packages/redis/retry.py", line 46, in call_with_retry
    return do()
  File "/mre/.venv/lib/python3.10/site-packages/redis/client.py", line 1259, in <lambda>
    lambda: self._send_command_parse_response(
  File "/mre/.venv/lib/python3.10/site-packages/redis/client.py", line 1234, in _send_command_parse_response
    conn.send_command(*args)
  File "/mre/.venv/lib/python3.10/site-packages/redis/connection.py", line 787, in send_command
    self.send_packed_command(
  File "/mre/.venv/lib/python3.10/site-packages/redis/connection.py", line 769, in send_packed_command
    self._sock.sendall(item)
  File "/mre/.venv/lib/python3.10/site-packages/fakeredis/_basefakesocket.py", line 212, in sendall
    self._parser.send(data)
  File "/mre/.venv/lib/python3.10/site-packages/fakeredis/_basefakesocket.py", line 123, in _parse_commands
    self._process_command(fields)
  File "/mre/.venv/lib/python3.10/site-packages/fakeredis/_basefakesocket.py", line 240, in _process_command
    result = self._run_command(func, sig, cmd_arguments, False)
  File "/mre/.venv/lib/python3.10/site-packages/fakeredis/_basefakesocket.py", line 144, in _run_command
    result = func(*args)
  File "/mre/.venv/lib/python3.10/site-packages/fakeredis/commands_mixins/generic_mixin.py", line 206, in scan
    return self._scan(list(self._db), cursor, *args)
  File "/mre/.venv/lib/python3.10/site-packages/fakeredis/_basefakesocket.py", line 304, in _scan
    if match_key(compare_val) and match_type(compare_val):
  File "/mre/.venv/lib/python3.10/site-packages/fakeredis/_basefakesocket.py", line 298, in match_type
    return casematch(self._type(self._db[key]).value, _type)
AttributeError: 'FakeSocket' object has no attribute '_type'. Did you mean: 'type'?
```

**Expected behavior**
Filter the results for the expected `_type` without throwing errors.

**Desktop (please complete the following information):**
 - OS: macOS Monterey
 - python version: 3.10.8, but also reproduced it on 3.9.15
 - redis-py version: 4.4.0
 - fakeredis-py version: 2.3.0
 - full requirements.txt? Only `fakeredis`


Discussion:
github-actions[bot]: <!-- add-pr-comment:coverage -->

Coverage report:
```
Name                                              Stmts   Miss Branch BrPart  Cover
-----------------------------------------------------------------------------------
fakeredis/__init__.py                                 6      2      0      0    67%
fakeredis/_basefakesocket.py                        225      9     96     10    94%
fakeredis/_commands.py                              295      8    110      5    96%
fakeredis/_fakesocket.py                             17      0      0      0   100%
fakeredis/_helpers.py                               168      4     61      3    97%
fakeredis/_msgs.py                                   57      0      0      0   100%
fakeredis/_server.py                                122      6     42      5    93%
fakeredis/_zset.py                                   64      0     16      0   100%
fakeredis/aioredis.py                               155      8     43      7    92%
fakeredis/commands_mixins/__init__.py                 0      0      0      0   100%
fakeredis/commands_mixins/bitmap_mixin.py            44      1      8      1    96%
fakeredis/commands_mixins/connection_mixin.py        21      0      4      0   100%
fakeredis/commands_mixins/generic_mixin.py          244     17    110     10    91%
fakeredis/commands_mixins/hash_mixin.py              82      1     16      1    98%
fakeredis/commands_mixins/list_mixin.py             176      5     76      5    96%
fakeredis/commands_mixins/pubsub_mixin.py            81      0     36      6    95%
fakeredis/commands_mixins/scripting_mixin.py        147      3     70      4    97%
fakeredis/commands_mixins/server_mixin.py            49      0     14      0   100%
fakeredis/commands_mixins/set_mixin.py              139     12     58      6    88%
fakeredis/commands_mixins/sortedset_mixin.py        326      7    137      6    97%
fakeredis/commands_mixins/string_mixin.py           191      4     80      4    97%
fakeredis/commands_mixins/transactions_mixin.py      71      2     20      1    97%
fakeredis/stack/__init__.py                           7      3      0      0    57%
fakeredis/stack/_json_mixin.py                      189      7     85      3    96%
-----------------------------------------------------------------------------------
TOTAL                                              2876     99   1082     77    95%
```

Patch:
diff --git a/fakeredis/_basefakesocket.py b/fakeredis/_basefakesocket.py
index 9929587..37259b4 100644
--- a/fakeredis/_basefakesocket.py
+++ b/fakeredis/_basefakesocket.py
@@ -7,7 +7,8 @@ from typing import List
 import redis
 
 from . import _msgs as msgs
-from ._commands import (Int, Float, SUPPORTED_COMMANDS, COMMANDS_WITH_SUB)
+from ._commands import (
+    Int, Float, SUPPORTED_COMMANDS, COMMANDS_WITH_SUB, key_value_type)
 from ._helpers import (
     SimpleError, valid_response_type, SimpleString, NoResponse, casematch,
     compile_pattern, QUEUED, encode_command)
@@ -295,7 +296,7 @@ class BaseFakeSocket:
 
         def match_type(key):
             if _type is not None:
-                return casematch(self._type(self._db[key]).value, _type)
+                return casematch(key_value_type(self._db[key]).value, _type)
             return True
 
         if pattern is not None or _type is not None:
diff --git a/fakeredis/_commands.py b/fakeredis/_commands.py
index cf751e1..00f2b06 100644
--- a/fakeredis/_commands.py
+++ b/fakeredis/_commands.py
@@ -7,7 +7,8 @@ import math
 import re
 
 from . import _msgs as msgs
-from ._helpers import null_terminate, SimpleError
+from ._helpers import null_terminate, SimpleError, SimpleString
+from ._zset import ZSet
 
 MAX_STRING_SIZE = 512 * 1024 * 1024
 SUPPORTED_COMMANDS = dict()  # Dictionary of supported commands name => Signature
@@ -414,3 +415,20 @@ def fix_range_string(start, end, length):
         end = max(0, end + length)
     end = min(end, length - 1)
     return start, end + 1
+
+
+def key_value_type(key):
+    if key.value is None:
+        return SimpleString(b'none')
+    elif isinstance(key.value, bytes):
+        return SimpleString(b'string')
+    elif isinstance(key.value, list):
+        return SimpleString(b'list')
+    elif isinstance(key.value, set):
+        return SimpleString(b'set')
+    elif isinstance(key.value, ZSet):
+        return SimpleString(b'zset')
+    elif isinstance(key.value, dict):
+        return SimpleString(b'hash')
+    else:
+        assert False  # pragma: nocover
diff --git a/fakeredis/commands_mixins/generic_mixin.py b/fakeredis/commands_mixins/generic_mixin.py
index 8517dae..e04cee8 100644
--- a/fakeredis/commands_mixins/generic_mixin.py
+++ b/fakeredis/commands_mixins/generic_mixin.py
@@ -3,8 +3,10 @@ import pickle
 from random import random
 
 from fakeredis import _msgs as msgs
-from fakeredis._commands import command, Key, Int, DbIndex, BeforeAny, CommandItem, SortFloat, delete_keys
-from fakeredis._helpers import compile_pattern, SimpleError, OK, casematch, SimpleString
+from fakeredis._commands import (
+    command, Key, Int, DbIndex, BeforeAny, CommandItem, SortFloat,
+    delete_keys, key_value_type, )
+from fakeredis._helpers import compile_pattern, SimpleError, OK, casematch
 from fakeredis._zset import ZSet
 
 
@@ -37,23 +39,6 @@ class GenericCommandsMixin:
                 return None
             return item.value
 
-    @staticmethod
-    def _key_value_type(key):
-        if key.value is None:
-            return SimpleString(b'none')
-        elif isinstance(key.value, bytes):
-            return SimpleString(b'string')
-        elif isinstance(key.value, list):
-            return SimpleString(b'list')
-        elif isinstance(key.value, set):
-            return SimpleString(b'set')
-        elif isinstance(key.value, ZSet):
-            return SimpleString(b'zset')
-        elif isinstance(key.value, dict):
-            return SimpleString(b'hash')
-        else:
-            assert False  # pragma: nocover
-
     def _expireat(self, key, timestamp, *args):
         nx = False
         xx = False
@@ -308,7 +293,7 @@ class GenericCommandsMixin:
 
     @command((Key(),))
     def type(self, key):
-        return self._key_value_type(key)
+        return key_value_type(key)
 
     @command((Key(),), (Key(),), name='unlink')
     def unlink(self, *keys):


Test patch:
diff --git a/test/test_mixins/test_generic_commands.py b/test/test_mixins/test_generic_commands.py
index 4987519..cf7a143 100644
--- a/test/test_mixins/test_generic_commands.py
+++ b/test/test_mixins/test_generic_commands.py
@@ -657,6 +657,7 @@ def test_scan_iter_single_page(r):
     assert set(r.scan_iter(match="foo*")) == {b'foo1', b'foo2'}
     assert set(r.scan_iter()) == {b'foo1', b'foo2'}
     assert set(r.scan_iter(match="")) == set()
+    assert set(r.scan_iter(match="foo1", _type="string")) == {b'foo1', }
 
 
 def test_scan_iter_multiple_pages(r):