Skip to content

Commit

Permalink
Merge original typing from types-redis, adapt it for valkey.
Browse files Browse the repository at this point in the history
Signed-off-by: Raphaël Vinot <[email protected]>
  • Loading branch information
Rafiot committed Dec 16, 2024
1 parent d5bb6bf commit f271a43
Show file tree
Hide file tree
Showing 71 changed files with 17,592 additions and 484 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Python application

on:
push:
branches: [ types ]
pull_request:
branches: [ types ]

jobs:
build:

runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v4

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

- name: Install package
run: |
pip install mypy cryptography pyopenssl requests
pip install types-setuptools
pip install types-cachetools
pip install -r dev_requirements.txt
pip install .[libvalkey]
- name: Run MyPy
run: |
mypy --exclude build .
83 changes: 61 additions & 22 deletions .mypy.ini
Original file line number Diff line number Diff line change
@@ -1,24 +1,63 @@
[mypy]
#, docs/examples, tests
files = valkey
check_untyped_defs = True
follow_imports_for_stubs asyncio.= True
#disallow_any_decorated = True
disallow_subclassing_any = True
#disallow_untyped_calls = True
disallow_untyped_decorators = True
#disallow_untyped_defs = True
implicit_reexport = False
no_implicit_optional = True
show_error_codes = True
strict_equality = True
warn_incomplete_stub = True
warn_redundant_casts = True
warn_unreachable = True
warn_unused_ignores = True
disallow_any_unimported = True
#warn_return_any = True

[mypy-valkey.asyncio.lock]
# TODO: Remove once locks has been rewritten
strict = True
show_error_context = True
pretty = True
exclude = docs|build

# These next few are various gradations of forcing use of type annotations
disallow_untyped_calls = False
disallow_incomplete_defs = False
disallow_untyped_defs = False

# This one can be tricky to get passing if you use a lot of untyped libraries
warn_return_any = False

[mypy-valkey._parsers.*]
ignore_errors = True

[mypy-valkey._cache]
ignore_errors = True

[mypy-tests.*]
ignore_errors = True
[mypy-tests.test_bloom]
ignore_errors = False
[mypy-tests.test_asyncio.test_bloom]
ignore_errors = False
[mypy-tests.test_cache]
ignore_errors = False
[mypy-tests.test_asyncio.test_cache]
ignore_errors = False
[mypy-tests.test_commands]
ignore_errors = False
[mypy-tests.test_asyncio.test_commands]
ignore_errors = False
#[mypy-tests.test_cluster]
#ignore_errors = False
#[mypy-tests.test_asyncio.test_cluster]
#ignore_errors = False
#[mypy-tests.test_connection_pool]
#ignore_errors = False
#[mypy-tests.test_asyncio.test_connection_pool]
#ignore_errors = False
#[mypy-tests.test_connection]
#ignore_errors = False
#[mypy-tests.test_asyncio.test_connection]
#ignore_errors = False
[mypy-tests.test_pipeline]
ignore_errors = False
[mypy-tests.test_asyncio.test_pipeline]
ignore_errors = False
[mypy-tests.test_pubsub]
ignore_errors = False
[mypy-tests.test_asyncio.test_pubsub]
ignore_errors = False

[mypy-benchmarks.*]
ignore_errors = True

[mypy-whitelist]
ignore_errors = True

[mypy-tasks]
ignore_errors = True
2 changes: 2 additions & 0 deletions tests/test_asyncio/compat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import asyncio
from unittest import mock

__all__ = ["mock", "aclosing", "create_task"]

try:
mock.AsyncMock
except AttributeError:
Expand Down
52 changes: 26 additions & 26 deletions tests/test_asyncio/test_bloom.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def intlist(obj):
return [int(v) for v in obj]


async def test_create(decoded_r: valkey.Valkey):
async def test_create(decoded_r: valkey.Valkey[str]):
"""Test CREATE/RESERVE calls"""
assert await decoded_r.bf().create("bloom", 0.01, 1000)
assert await decoded_r.bf().create("bloom_e", 0.01, 1000, expansion=1)
Expand All @@ -31,11 +31,11 @@ async def test_create(decoded_r: valkey.Valkey):


@pytest.mark.experimental
async def test_tdigest_create(decoded_r: valkey.Valkey):
async def test_tdigest_create(decoded_r: valkey.Valkey[str]):
assert await decoded_r.tdigest().create("tDigest", 100)


async def test_bf_add(decoded_r: valkey.Valkey):
async def test_bf_add(decoded_r: valkey.Valkey[str]):
assert await decoded_r.bf().create("bloom", 0.01, 1000)
assert 1 == await decoded_r.bf().add("bloom", "foo")
assert 0 == await decoded_r.bf().add("bloom", "foo")
Expand All @@ -47,7 +47,7 @@ async def test_bf_add(decoded_r: valkey.Valkey):
assert [1, 0] == intlist(await decoded_r.bf().mexists("bloom", "foo", "noexist"))


async def test_bf_insert(decoded_r: valkey.Valkey):
async def test_bf_insert(decoded_r: valkey.Valkey[str]):
assert await decoded_r.bf().create("bloom", 0.01, 1000)
assert [1] == intlist(await decoded_r.bf().insert("bloom", ["foo"]))
assert [0, 1] == intlist(await decoded_r.bf().insert("bloom", ["foo", "bar"]))
Expand Down Expand Up @@ -77,7 +77,7 @@ async def test_bf_insert(decoded_r: valkey.Valkey):
)


async def test_bf_scandump_and_loadchunk(decoded_r: valkey.Valkey):
async def test_bf_scandump_and_loadchunk(decoded_r: valkey.Valkey[str]):
# Store a filter
await decoded_r.bf().create("myBloom", "0.0001", "1000")

Expand Down Expand Up @@ -124,7 +124,7 @@ async def do_verify():
await decoded_r.bf().create("myBloom", "0.0001", "10000000")


async def test_bf_info(decoded_r: valkey.Valkey):
async def test_bf_info(decoded_r: valkey.Valkey[str]):
expansion = 4
# Store a filter
await decoded_r.bf().create("nonscaling", "0.0001", "1000", noScale=True)
Expand Down Expand Up @@ -155,7 +155,7 @@ async def test_bf_info(decoded_r: valkey.Valkey):
assert True


async def test_bf_card(decoded_r: valkey.Valkey):
async def test_bf_card(decoded_r: valkey.Valkey[str]):
# return 0 if the key does not exist
assert await decoded_r.bf().card("not_exist") == 0

Expand All @@ -169,7 +169,7 @@ async def test_bf_card(decoded_r: valkey.Valkey):
await decoded_r.bf().card("setKey")


async def test_cf_add_and_insert(decoded_r: valkey.Valkey):
async def test_cf_add_and_insert(decoded_r: valkey.Valkey[str]):
assert await decoded_r.cf().create("cuckoo", 1000)
assert await decoded_r.cf().add("cuckoo", "filter")
assert not await decoded_r.cf().addnx("cuckoo", "filter")
Expand All @@ -194,7 +194,7 @@ async def test_cf_add_and_insert(decoded_r: valkey.Valkey):
)


async def test_cf_exists_and_del(decoded_r: valkey.Valkey):
async def test_cf_exists_and_del(decoded_r: valkey.Valkey[str]):
assert await decoded_r.cf().create("cuckoo", 1000)
assert await decoded_r.cf().add("cuckoo", "filter")
assert await decoded_r.cf().exists("cuckoo", "filter")
Expand All @@ -205,7 +205,7 @@ async def test_cf_exists_and_del(decoded_r: valkey.Valkey):
assert 0 == await decoded_r.cf().count("cuckoo", "filter")


async def test_cms(decoded_r: valkey.Valkey):
async def test_cms(decoded_r: valkey.Valkey[str]):
assert await decoded_r.cms().initbydim("dim", 1000, 5)
assert await decoded_r.cms().initbyprob("prob", 0.01, 0.01)
assert await decoded_r.cms().incrby("dim", ["foo"], [5])
Expand All @@ -221,7 +221,7 @@ async def test_cms(decoded_r: valkey.Valkey):


@pytest.mark.onlynoncluster
async def test_cms_merge(decoded_r: valkey.Valkey):
async def test_cms_merge(decoded_r: valkey.Valkey[str]):
assert await decoded_r.cms().initbydim("A", 1000, 5)
assert await decoded_r.cms().initbydim("B", 1000, 5)
assert await decoded_r.cms().initbydim("C", 1000, 5)
Expand All @@ -237,7 +237,7 @@ async def test_cms_merge(decoded_r: valkey.Valkey):
assert [16, 15, 21] == await decoded_r.cms().query("C", "foo", "bar", "baz")


async def test_topk(decoded_r: valkey.Valkey):
async def test_topk(decoded_r: valkey.Valkey[str]):
# test list with empty buckets
assert await decoded_r.topk().reserve("topk", 3, 50, 4, 0.9)
assert [
Expand Down Expand Up @@ -317,7 +317,7 @@ async def test_topk(decoded_r: valkey.Valkey):
assert 0.9 == round(float(info["decay"]), 1)


async def test_topk_incrby(decoded_r: valkey.Valkey):
async def test_topk_incrby(decoded_r: valkey.Valkey[str]):
await decoded_r.flushdb()
assert await decoded_r.topk().reserve("topk", 3, 10, 3, 1)
assert [None, None, None] == await decoded_r.topk().incrby(
Expand All @@ -332,7 +332,7 @@ async def test_topk_incrby(decoded_r: valkey.Valkey):


@pytest.mark.experimental
async def test_tdigest_reset(decoded_r: valkey.Valkey):
async def test_tdigest_reset(decoded_r: valkey.Valkey[str]):
assert await decoded_r.tdigest().create("tDigest", 10)
# reset on empty histogram
assert await decoded_r.tdigest().reset("tDigest")
Expand All @@ -348,7 +348,7 @@ async def test_tdigest_reset(decoded_r: valkey.Valkey):


@pytest.mark.onlynoncluster
async def test_tdigest_merge(decoded_r: valkey.Valkey):
async def test_tdigest_merge(decoded_r: valkey.Valkey[str]):
assert await decoded_r.tdigest().create("to-tDigest", 10)
assert await decoded_r.tdigest().create("from-tDigest", 10)
# insert data-points into sketch
Expand All @@ -375,7 +375,7 @@ async def test_tdigest_merge(decoded_r: valkey.Valkey):


@pytest.mark.experimental
async def test_tdigest_min_and_max(decoded_r: valkey.Valkey):
async def test_tdigest_min_and_max(decoded_r: valkey.Valkey[str]):
assert await decoded_r.tdigest().create("tDigest", 100)
# insert data-points into sketch
assert await decoded_r.tdigest().add("tDigest", [1, 2, 3])
Expand All @@ -385,8 +385,8 @@ async def test_tdigest_min_and_max(decoded_r: valkey.Valkey):


@pytest.mark.experimental
@skip_ifmodversion_lt("2.4.0", "bf")
async def test_tdigest_quantile(decoded_r: valkey.Valkey):
@skip_ifmodversion_lt("2.4.0", "bf") # type: ignore[misc]
async def test_tdigest_quantile(decoded_r: valkey.Valkey[str]):
assert await decoded_r.tdigest().create("tDigest", 500)
# insert data-points into sketch
assert await decoded_r.tdigest().add(
Expand All @@ -413,7 +413,7 @@ async def test_tdigest_quantile(decoded_r: valkey.Valkey):


@pytest.mark.experimental
async def test_tdigest_cdf(decoded_r: valkey.Valkey):
async def test_tdigest_cdf(decoded_r: valkey.Valkey[str]):
assert await decoded_r.tdigest().create("tDigest", 100)
# insert data-points into sketch
assert await decoded_r.tdigest().add("tDigest", list(range(1, 10)))
Expand All @@ -424,8 +424,8 @@ async def test_tdigest_cdf(decoded_r: valkey.Valkey):


@pytest.mark.experimental
@skip_ifmodversion_lt("2.4.0", "bf")
async def test_tdigest_trimmed_mean(decoded_r: valkey.Valkey):
@skip_ifmodversion_lt("2.4.0", "bf") # type: ignore[misc]
async def test_tdigest_trimmed_mean(decoded_r: valkey.Valkey[str]):
assert await decoded_r.tdigest().create("tDigest", 100)
# insert data-points into sketch
assert await decoded_r.tdigest().add("tDigest", list(range(1, 10)))
Expand All @@ -434,7 +434,7 @@ async def test_tdigest_trimmed_mean(decoded_r: valkey.Valkey):


@pytest.mark.experimental
async def test_tdigest_rank(decoded_r: valkey.Valkey):
async def test_tdigest_rank(decoded_r: valkey.Valkey[str]):
assert await decoded_r.tdigest().create("t-digest", 500)
assert await decoded_r.tdigest().add("t-digest", list(range(0, 20)))
assert -1 == (await decoded_r.tdigest().rank("t-digest", -1))[0]
Expand All @@ -444,7 +444,7 @@ async def test_tdigest_rank(decoded_r: valkey.Valkey):


@pytest.mark.experimental
async def test_tdigest_revrank(decoded_r: valkey.Valkey):
async def test_tdigest_revrank(decoded_r: valkey.Valkey[str]):
assert await decoded_r.tdigest().create("t-digest", 500)
assert await decoded_r.tdigest().add("t-digest", list(range(0, 20)))
assert -1 == (await decoded_r.tdigest().revrank("t-digest", 20))[0]
Expand All @@ -453,7 +453,7 @@ async def test_tdigest_revrank(decoded_r: valkey.Valkey):


@pytest.mark.experimental
async def test_tdigest_byrank(decoded_r: valkey.Valkey):
async def test_tdigest_byrank(decoded_r: valkey.Valkey[str]):
assert await decoded_r.tdigest().create("t-digest", 500)
assert await decoded_r.tdigest().add("t-digest", list(range(1, 11)))
assert 1 == (await decoded_r.tdigest().byrank("t-digest", 0))[0]
Expand All @@ -464,7 +464,7 @@ async def test_tdigest_byrank(decoded_r: valkey.Valkey):


@pytest.mark.experimental
async def test_tdigest_byrevrank(decoded_r: valkey.Valkey):
async def test_tdigest_byrevrank(decoded_r: valkey.Valkey[str]):
assert await decoded_r.tdigest().create("t-digest", 500)
assert await decoded_r.tdigest().add("t-digest", list(range(1, 11)))
assert 10 == (await decoded_r.tdigest().byrevrank("t-digest", 0))[0]
Expand All @@ -474,7 +474,7 @@ async def test_tdigest_byrevrank(decoded_r: valkey.Valkey):
(await decoded_r.tdigest().byrevrank("t-digest", -1))[0]


# # async def test_pipeline(decoded_r: valkey.Valkey):
# # async def test_pipeline(decoded_r: valkey.Valkey[str]):
# pipeline = await decoded_r.bf().pipeline()
# assert not await decoded_r.bf().execute_command("get pipeline")
#
Expand Down
Loading

0 comments on commit f271a43

Please sign in to comment.