Skip to content

Commit

Permalink
Added deadcode linter for python and fixed relevant errors
Browse files Browse the repository at this point in the history
  • Loading branch information
barshaul committed Oct 5, 2023
1 parent 443da99 commit 43a3137
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 41 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 &
Expand Down
4 changes: 2 additions & 2 deletions examples/python/client_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
1 change: 1 addition & 0 deletions python/dev_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ flake8 == 5.0
isort == 5.10
mypy == 1.2
mypy-protobuf == 3.5
deadcode == 2.1
2 changes: 1 addition & 1 deletion python/python/pybushka/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
3 changes: 2 additions & 1 deletion python/python/pybushka/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions python/python/pybushka/protobuf_codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
6 changes: 0 additions & 6 deletions python/python/pybushka/redis_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
7 changes: 0 additions & 7 deletions python/python/pybushka/utils.py

This file was deleted.

8 changes: 5 additions & 3 deletions python/python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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`",
)
Expand All @@ -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.
Expand Down
1 change: 0 additions & 1 deletion python/python/tests/test_proto_coded.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 7 additions & 10 deletions python/python/tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions python/python/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion utils/cluster_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down

0 comments on commit 43a3137

Please sign in to comment.