Skip to content

Commit

Permalink
yatq adapted for python 3.11
Browse files Browse the repository at this point in the history
yatq adapted for python 3.11
  • Loading branch information
walnutflat authored Jun 21, 2023
2 parents f875f21 + e7b87ea commit 879d35e
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 34 deletions.
53 changes: 48 additions & 5 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,52 @@ on:
branches: [ master ]

jobs:
test:
test_with_aioredis:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8, 3.9]
aioredis-version: [1.2.0, 2.0.0]
python-version: [ '3.7', '3.8', '3.9', '3.10']
aioredis-version: [ 1.2.0, 2.0.0 ]
exclude:
- python-version: '3.10'
aioredis-version: 1.2.0

steps:
- uses: actions/checkout@v2

- name: Install redis
run: sudo apt-get install redis-server

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install common dependencies
run: |
python -m pip install --upgrade pip setuptools
pip install -r requirements-test.txt
- name: Install aioredis
run: |
pip install "aioredis==${{ matrix.aioredis-version }}"
- name: Install yatq
run: |
pip install .
- name: Test with pytest
run: |
pytest
test_with_redis:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.11']
redis-version: [4.5.5]

steps:
- uses: actions/checkout@v2
Expand All @@ -26,10 +65,14 @@ jobs:
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
- name: Install common dependencies
run: |
python -m pip install --upgrade pip setuptools
pip install -r requirements-test.txt "aioredis==${{ matrix.aioredis-version }}"
pip install -r requirements-test.txt
- name: Install redis
run: |
pip install "redis==${{ matrix.redis-version }}"
- name: Install yatq
run: |
Expand Down
5 changes: 3 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[metadata]
name = yatq
version = 0.0.5
version = 0.0.6

[options]
packages = find:
install_requires =
aioredis >= 1.2.0
aioredis >= 1.2.0;python_version<'3.11'
redis >= 4.5.5;python_version>='3.11'

[options.package_data]
* = py.typed
Expand Down
12 changes: 9 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
from typing import Any
from uuid import uuid4

import aioredis
from yatq.py_version import AIOREDIS_USE

if AIOREDIS_USE:
import aioredis
else: # pragma: no cover
from redis import asyncio as aioredis

import pytest

from yatq.dto import TaskState
from yatq.queue import Queue

if aioredis.__version__ >= "2.0":
if not AIOREDIS_USE or aioredis.__version__ >= "2.0":

async def create_redis_connection(redis_uri: str):
return aioredis.from_url(redis_uri)

async def zadd_single(client: aioredis.Redis, set_name: str, key: str, value: Any):
await client.zadd(set_name, {key: value})

else:
else: # pragma: no cover

async def create_redis_connection(redis_uri: str):
return await aioredis.create_redis(redis_uri)
Expand Down
1 change: 0 additions & 1 deletion tests/test_task_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,6 @@ async def test_task_retry_every_x_policy(task_queue, queue_checker, freezer):
await queue_checker.assert_metric_requeued(3)



@pytest.mark.asyncio
async def test_task_retry_forced(task_queue, queue_checker, freezer):
task_1 = await task_queue.add_task(
Expand Down
12 changes: 11 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tox]
isolated_build = True
envlist = py{37,38,39},aioredis_compat,coverage,mypy,black
envlist = py{37,38,39,310,311},aioredis_compat,coverage,mypy,black

[testenv]
deps =
Expand All @@ -21,6 +21,16 @@ extras =
commands =
pytest tests

[testenv:redis]
basepython = python3.11
deps =
-rrequirements-test.txt
redis == 4.5.5
extras =
setuptools
commands =
pytest tests

[coverage:run]
branch = True

Expand Down
7 changes: 6 additions & 1 deletion yatq/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
from string import Template
from typing import Any, Dict

from aioredis import Redis
from yatq.py_version import AIOREDIS_USE

if AIOREDIS_USE:
from aioredis import Redis
else:
from redis.asyncio import Redis # type: ignore # pragma: no cover

from yatq.redis_compat import NoScriptError, eval_sha # type: ignore

Expand Down
3 changes: 3 additions & 0 deletions yatq/py_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import sys

AIOREDIS_USE = sys.version_info[0:2] < (3, 11)
7 changes: 6 additions & 1 deletion yatq/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
from typing import Any, Dict, Optional, Union
from uuid import uuid4

from aioredis import Redis
from yatq.py_version import AIOREDIS_USE

if AIOREDIS_USE:
from aioredis import Redis
else:
from redis.asyncio import Redis # type: ignore # pragma: no cover

from .defaults import (
DEFAULT_QUEUE_NAME,
Expand Down
45 changes: 28 additions & 17 deletions yatq/redis_compat.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
# type: ignore
from typing import Any, Tuple
from yatq.py_version import AIOREDIS_USE

import aioredis
if AIOREDIS_USE:
import aioredis

if aioredis.__version__ >= "2.0": # pragma: no cover
from aioredis.exceptions import NoScriptError
if aioredis.__version__ >= "2.0":
from aioredis.exceptions import NoScriptError

async def eval_sha(
client: aioredis.Redis, digest: str, args: Tuple[Any]
): # pragma: no cover
return await client.evalsha(digest, 0, *args)
async def eval_sha(
client: aioredis.Redis, digest: str, args: Tuple[Any]
): # pragma: no cover
return await client.evalsha(digest, 0, *args)

else: # pragma: no cover
from aioredis.errors import ReplyError
else: # pragma: no cover
from aioredis.errors import ReplyError

class NoScriptError(ReplyError):
...
class NoScriptError(ReplyError):
...

async def eval_sha(
client: aioredis.Redis, digest: str, args: Tuple[Any]
): # pragma: no cover
try:
return await client.evalsha(digest, keys=[], args=list(args))
except ReplyError as error:
if error.args[0].startswith("NOSCRIPT"):
raise NoScriptError(error.args[0])
raise

else: # pragma: no cover
from redis import asyncio as aioredis
from redis.exceptions import NoScriptError

async def eval_sha(
client: aioredis.Redis, digest: str, args: Tuple[Any]
): # pragma: no cover
try:
return await client.evalsha(digest, keys=[], args=list(args))
except ReplyError as error:
if error.args[0].startswith("NOSCRIPT"):
raise NoScriptError(error.args[0])
raise
return await client.execute_command("EVALSHA", digest, 0, *args)
13 changes: 11 additions & 2 deletions yatq/worker/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
import traceback
from typing import Dict, List, Optional, Set, Type, cast

import aioredis
from yatq.py_version import AIOREDIS_USE

if AIOREDIS_USE:
import aioredis
else: # pragma: no cover
from redis import asyncio as aioredis # type: ignore


from yatq.defaults import DEFAULT_MAX_JOBS, DEFAULT_QUEUE_NAMESPACE
from yatq.dto import TaskWrapper
Expand Down Expand Up @@ -61,7 +67,10 @@ async def _periodic_poll(self) -> None:

async def _wait_poll(self) -> None:
_, pending = await asyncio.wait(
{self._poll_event.wait(), self._stop_event.wait()},
{
asyncio.create_task(self._poll_event.wait()),
asyncio.create_task(self._stop_event.wait()),
},
return_when=asyncio.FIRST_COMPLETED,
)

Expand Down
7 changes: 6 additions & 1 deletion yatq/worker/worker_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
from typing import Awaitable, Callable, Dict, Optional, Tuple, Type
from abc import ABC, abstractmethod

import aioredis
from yatq.py_version import AIOREDIS_USE

if AIOREDIS_USE:
import aioredis
else: # pragma: no cover
from redis import asyncio as aioredis # type: ignore

from yatq.worker.factory.base import BaseJobFactory
from yatq.worker.factory.simple import SimpleJobFactory
Expand Down

0 comments on commit 879d35e

Please sign in to comment.