diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index d35095293a..aa22321f04 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -51,7 +51,7 @@ jobs: working-directory: ./python run: | python -m pip install --upgrade pip - pip install flake8 isort black mypy-protobuf + pip install flake8 isort black mypy-protobuf deadcode - name: Lint with isort working-directory: ./python @@ -71,6 +71,11 @@ jobs: run: | black --target-version py36 --check --diff . + - name: Lint with deadcode + working-directory: . + run: | + deadcode . --ignore-definitions=InfoSection,Level,*protobuf*,*pytest* + - name: Start redis server working-directory: ./python run: redis-server & diff --git a/examples/python/client_example.py b/examples/python/client_example.py index 75eacf72f2..dfda315536 100644 --- a/examples/python/client_example.py +++ b/examples/python/client_example.py @@ -73,9 +73,9 @@ async def test_cluster_client(host: str = "localhost", port: int = 6379): async def main(): set_console_logger(LogLevel.DEBUG) - # set_file_logger(LogLevel.DEBUG) + set_file_logger(LogLevel.DEBUG) await test_standalone_client() - # await test_cluster_client() + await test_cluster_client() if __name__ == "__main__": diff --git a/python/dev_requirements.txt b/python/dev_requirements.txt index d870a9bb4f..4fe25fea17 100644 --- a/python/dev_requirements.txt +++ b/python/dev_requirements.txt @@ -3,3 +3,4 @@ flake8 == 5.0 isort == 5.10 mypy == 1.2 mypy-protobuf == 3.5 +deadcode == 2.1 diff --git a/python/python/pybushka/async_commands/transaction.py b/python/python/pybushka/async_commands/transaction.py index a342b1cfc0..323773ad12 100644 --- a/python/python/pybushka/async_commands/transaction.py +++ b/python/python/pybushka/async_commands/transaction.py @@ -32,7 +32,7 @@ def append_command(self, request_type: RequestType.ValueType, args: List[str]): finally: self.lock.release() - def dispose(self): + def clear(self): with self.lock: self.commands.clear() diff --git a/python/python/pybushka/config.py b/python/python/pybushka/config.py index f4708cf170..99676aae10 100644 --- a/python/python/pybushka/config.py +++ b/python/python/pybushka/config.py @@ -68,7 +68,8 @@ def __init__( Args: password (str): The password that will be passed to the cluster's Access Control Layer. - username (Optional[str]): The username that will be passed to the cluster's Access Control Layer. If not supplied, "default" will be used. + username (Optional[str]): The username that will be passed to the cluster's Access Control Layer. + If not supplied, "default" will be used. """ self.password = password self.username = username diff --git a/python/python/pybushka/protobuf_codec.py b/python/python/pybushka/protobuf_codec.py index 645cbb9370..4a06685359 100644 --- a/python/python/pybushka/protobuf_codec.py +++ b/python/python/pybushka/protobuf_codec.py @@ -49,7 +49,7 @@ def _varint_encoder(): local_int2byte = struct.Struct(">B").pack - def encode_varint(write, value, unused_deterministic=None): + def encode_varint(write, value): bits = value & 0x7F value >>= 7 while value: @@ -62,13 +62,11 @@ def encode_varint(write, value, unused_deterministic=None): @classmethod def _varint_bytes(cls, value: int) -> bytes: - """Encode the given integer as a varint and return the bytes. - TODO: Improve performance - """ + """Encode the given integer as a varint and return the bytes.""" pieces: List[bytes] = [] func = cls._varint_encoder() - func(pieces.append, value, True) + func(pieces.append, value) return b"".join(pieces) @classmethod diff --git a/python/python/pybushka/redis_client.py b/python/python/pybushka/redis_client.py index 08731b0ff0..a8fe16190b 100644 --- a/python/python/pybushka/redis_client.py +++ b/python/python/pybushka/redis_client.py @@ -26,14 +26,12 @@ def __init__(self, config: ClientConfiguration): To create a new client, use the `create` classmethod """ self.config: ClientConfiguration = config - self._write_buffer: bytearray = bytearray(1024) self._available_futures: dict[int, asyncio.Future] = {} self._available_callbackIndexes: set[int] = set() self._buffered_requests: List[TRequest] = list() self._writer_lock = threading.Lock() self.socket_path: Optional[str] = None self._reader_task: Optional[asyncio.Task] = None - self._done_init = None @classmethod async def create(cls, config: Optional[ClientConfiguration] = None) -> Self: @@ -70,10 +68,6 @@ def init_callback(socket_path: Optional[str], err: Optional[str]): await self._set_connection_configurations() return self - async def _wait_for_init_complete(self) -> None: - while not self._done_init: - await asyncio.sleep(0.1) - async def _create_uds_connection(self) -> None: try: # Open an UDS connection diff --git a/python/python/pybushka/utils.py b/python/python/pybushka/utils.py deleted file mode 100644 index b9dfab4ec5..0000000000 --- a/python/python/pybushka/utils.py +++ /dev/null @@ -1,7 +0,0 @@ -def to_url(host, port=6379, user="", password="", tls_enabled=False) -> str: - protocol = "rediss" if tls_enabled else "redis" - auth = "" - if user or password: - auth = f"{user}{':' if user else ''}" - auth += f"{password}{'@' if password else ''}" - return f"{protocol}://{auth}{host}:{port}" diff --git a/python/python/tests/conftest.py b/python/python/tests/conftest.py index cb605d0335..4664822dfa 100644 --- a/python/python/tests/conftest.py +++ b/python/python/tests/conftest.py @@ -13,6 +13,8 @@ from pybushka.redis_client import RedisClient, RedisClusterClient, TRedisClient from tests.utils.cluster import RedisCluster +DEFAULT_HOST = "localhost" +DEFAULT_PORT = 6379 DEFAULT_TEST_LOG_LEVEL = logLevel.WARN Logger.set_logger_config(DEFAULT_TEST_LOG_LEVEL) @@ -21,14 +23,14 @@ def pytest_addoption(parser): parser.addoption( "--host", - default=default_host, + default=DEFAULT_HOST, action="store", help="Redis host endpoint, defaults to `%(default)s`", ) parser.addoption( "--port", - default=default_port, + default=DEFAULT_PORT, action="store", help="Redis port, defaults to `%(default)s`", ) @@ -49,7 +51,7 @@ def pytest_addoption(parser): @pytest.fixture(autouse=True, scope="session") -def call_before_all_tests(request): +def call_before_all_pytests(request): """ Called after the Session object has been created and before performing collection and entering the run test loop. diff --git a/python/python/tests/test_proto_coded.py b/python/python/tests/test_proto_coded.py index e69595a338..3c05b6ecb9 100644 --- a/python/python/tests/test_proto_coded.py +++ b/python/python/tests/test_proto_coded.py @@ -32,7 +32,6 @@ def test_encode_decode_delimited(self): def test_decode_partial_message_fails(self): response = Response() response.callback_idx = 1 - response.constant_response = 0 b_arr = bytearray() ProtobufCodec.encode_delimited(b_arr, response) b_arr_view = memoryview(b_arr) diff --git a/python/python/tests/test_transaction.py b/python/python/tests/test_transaction.py index 4a4aeacf6e..6ba6781715 100644 --- a/python/python/tests/test_transaction.py +++ b/python/python/tests/test_transaction.py @@ -8,20 +8,10 @@ Transaction, ) from pybushka.constants import OK, TResult -from pybushka.protobuf.redis_request_pb2 import RequestType from pybushka.redis_client import RedisClient, RedisClusterClient, TRedisClient from tests.test_async_client import get_random_string -def transaction_test_helper( - transaction: BaseTransaction, - expected_request_type: RequestType, - expected_args: List[str], -): - assert transaction.commands[-1][0] == expected_request_type - assert transaction.commands[-1][1] == expected_args - - def transaction_test( transaction: Union[Transaction, ClusterTransaction], keyslot: str ) -> List[TResult]: @@ -172,3 +162,10 @@ async def test_standalone_transaction(self, redis_client: RedisClient): assert "# Memory" in result[0] assert result[1:6] == [OK, OK, value, OK, None] assert result[6:] == expected + + def test_transaction_clear(self): + transaction = Transaction() + transaction.info() + transaction.select(1) + transaction.clear() + assert len(transaction.commands) == 0 diff --git a/python/python/tests/test_utils.py b/python/python/tests/test_utils.py index 1e4b565d56..be5c743e99 100644 --- a/python/python/tests/test_utils.py +++ b/python/python/tests/test_utils.py @@ -5,8 +5,8 @@ class TestLogger: def test_init_logger(self): # The logger is already configured in the conftest file, so calling init again shouldn't modify the log level - Logger.init(Level.DEBUG) - assert Logger.logger_level != Level.DEBUG.value + Logger.init(Level.ERROR) + assert Logger.logger_level == DEFAULT_TEST_LOG_LEVEL.value def test_set_logger_config(self): Logger.set_logger_config(Level.INFO) diff --git a/submodules/redis-rs b/submodules/redis-rs index ca6b9cb57a..ad7dc114cf 160000 --- a/submodules/redis-rs +++ b/submodules/redis-rs @@ -1 +1 @@ -Subproject commit ca6b9cb57a78d9164febe3878eff68043ab5bed0 +Subproject commit ad7dc114cf9ffa45a3d327610fb5873eebed5d7c diff --git a/utils/cluster_manager.py b/utils/cluster_manager.py index 8add4f808e..fca60d8cb0 100644 --- a/utils/cluster_manager.py +++ b/utils/cluster_manager.py @@ -142,7 +142,7 @@ def make_key(name: str, size: int): stderr=subprocess.PIPE, text=True, ) - redis_key_output, err = p.communicate(timeout=10) + _redis_key_output, err = p.communicate(timeout=10) if p.returncode != 0: raise Exception(f"Failed to read Redis key. Executed: {str(p.args)}:\n{err}")