Skip to content

Commit

Permalink
Initial mypy setup (#560)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamsorcerer committed Mar 14, 2022
1 parent a274196 commit 02782c2
Show file tree
Hide file tree
Showing 26 changed files with 91 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ jobs:
env:
COLOR: 'yes'
run: |
pytest tests
pytest tests --cov-report xml --cov-report html
python -m coverage xml
- name: Run functional tests
run: bash examples/run_all.sh
Expand Down
30 changes: 30 additions & 0 deletions .mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[mypy]
files = aiocache, examples, tests
#check_untyped_defs = True
follow_imports_for_stubs = True
#disallow_any_decorated = True
disallow_any_generics = True
disallow_incomplete_defs = 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-tests.*]
disallow_any_decorated = False
disallow_untyped_calls = False
disallow_untyped_defs = False


[mypy-msgpack.*]
ignore_missing_imports = True
4 changes: 3 additions & 1 deletion aiocache/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import logging
from typing import Dict, Type

from ._version import __version__
from .backends.memory import SimpleMemoryCache
from .base import BaseCache

logger = logging.getLogger(__name__)

AIOCACHE_CACHES = {SimpleMemoryCache.NAME: SimpleMemoryCache}
AIOCACHE_CACHES: Dict[str, Type[BaseCache]] = {SimpleMemoryCache.NAME: SimpleMemoryCache}

try:
import aioredis
Expand Down
5 changes: 3 additions & 2 deletions aiocache/backends/memory.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
from typing import Dict

from aiocache.base import BaseCache
from aiocache.serializers import NullSerializer
Expand All @@ -9,8 +10,8 @@ class SimpleMemoryBackend:
Wrapper around dict operations to use it as a cache backend
"""

_cache = {}
_handlers = {}
_cache: Dict[str, object] = {}
_handlers: Dict[str, asyncio.TimerHandle] = {}

def __init__(self, **kwargs):
super().__init__(**kwargs)
Expand Down
2 changes: 0 additions & 2 deletions aiocache/backends/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ class RedisBackend:
" end"
)

pools = {}

def __init__(
self,
endpoint="127.0.0.1",
Expand Down
5 changes: 4 additions & 1 deletion aiocache/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import os
import time
from typing import Callable, Set

from aiocache import serializers

Expand All @@ -14,7 +15,7 @@

class API:

CMDS = set()
CMDS: Set[Callable[..., object]] = set()

@classmethod
def register(cls, func):
Expand Down Expand Up @@ -103,6 +104,8 @@ class BaseCache:
the backend. It can be overriden in the specific calls.
"""

NAME: str

def __init__(
self, serializer=None, plugins=None, namespace=None, key_builder=None, timeout=5, ttl=None
):
Expand Down
7 changes: 4 additions & 3 deletions aiocache/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import urllib
import warnings
from copy import deepcopy
from typing import Dict

from aiocache import AIOCACHE_CACHES
from aiocache.base import BaseCache
Expand Down Expand Up @@ -126,7 +127,7 @@ def from_url(cls, url):

class CacheHandler:

_config = {
_config: Dict[str, Dict[str, object]] = {
"default": {
"cache": "aiocache.SimpleMemoryCache",
"serializer": {"class": "aiocache.serializers.StringSerializer"},
Expand All @@ -136,7 +137,7 @@ class CacheHandler:
def __init__(self):
self._caches = {}

def add(self, alias: str, config: dict) -> None:
def add(self, alias: str, config: Dict[str, object]) -> None:
"""
Add a cache to the current config. If the key already exists, it
will overwrite it::
Expand All @@ -153,7 +154,7 @@ def add(self, alias: str, config: dict) -> None:
"""
self._config[alias] = config

def get(self, alias: str):
def get(self, alias: str) -> object:
"""
Retrieve cache identified by alias. Will return always the same instance
Expand Down
6 changes: 3 additions & 3 deletions aiocache/lock.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
import uuid
from typing import Any, Union
from typing import Any, Dict, Union

from aiocache.base import BaseCache

Expand Down Expand Up @@ -60,7 +60,7 @@ class RedLock:
result of ``super_expensive_function``.
"""

_EVENTS = {}
_EVENTS: Dict[str, asyncio.Event] = {}

def __init__(self, client: BaseCache, key: str, lease: Union[int, float]):
self.client = client
Expand Down Expand Up @@ -149,7 +149,7 @@ async def _acquire(self):
async def __aexit__(self, exc_type, exc_value, traceback):
pass

async def cas(self, value: Any, **kwargs) -> bool:
async def cas(self, value: Any, **kwargs: Any) -> bool:
"""
Checks and sets the specified value for the locked key. If the value has changed
since the lock was created, it will raise an :class:`aiocache.lock.OptimisticLockError`
Expand Down
Empty file added aiocache/py.typed
Empty file.
11 changes: 7 additions & 4 deletions aiocache/serializers/serializers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import logging
import pickle # noqa: S403
from typing import Any, Optional

logger = logging.getLogger(__name__)

try:
import ujson as json
except ImportError:
logger.debug("ujson module not found, using json")
import json
import json # type: ignore[no-redef]

try:
import msgpack
Expand All @@ -21,16 +22,18 @@

class BaseSerializer:

DEFAULT_ENCODING = "utf-8"
DEFAULT_ENCODING: Optional[str] = "utf-8"

def __init__(self, *args, encoding=_NOT_SET, **kwargs):
self.encoding = self.DEFAULT_ENCODING if encoding is _NOT_SET else encoding
super().__init__(*args, **kwargs)

def dumps(self, value):
# TODO(PY38): Positional-only
def dumps(self, value: Any) -> str:
raise NotImplementedError("dumps method must be implemented")

def loads(self, value):
# TODO(PY38): Positional-only
def loads(self, value: str) -> Any:
raise NotImplementedError("loads method must be implemented")


Expand Down
2 changes: 1 addition & 1 deletion examples/marshmallow_serializer_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __eq__(self, obj):
return self.__dict__ == obj.__dict__


class MarshmallowSerializer(Schema, BaseSerializer):
class MarshmallowSerializer(Schema, BaseSerializer): # type: ignore[misc]
int_type = fields.Integer()
str_type = fields.String()
dict_type = fields.Dict()
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ pytest==7.0.1
pytest-asyncio==0.18.2
pytest-cov==3.0.0
pytest-mock==3.7
types-ujson==4.2.1
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ universal=1
max-line-length=100

[tool:pytest]
addopts = --cov=aiocache --cov-report xml --cov-report html --cov-report term
addopts = --cov=aiocache --cov-report term
asyncio_mode = auto
junit_suite_name = aiohttp_test_suite
#filterwarnings=
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@
"memcached": ["aiomcache>=0.5.2"],
"msgpack": ["msgpack>=0.5.5"],
},
include_package_data=True,
)
6 changes: 0 additions & 6 deletions tests/acceptance/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest

from aiocache import Cache, caches
from aiocache.backends.redis import RedisBackend


def pytest_configure():
Expand All @@ -27,11 +26,6 @@ def reset_caches():
)


@pytest.fixture(autouse=True)
def reset_redis_pools():
RedisBackend.pools = {}


@pytest.fixture
def redis_cache(event_loop):
cache = Cache(Cache.REDIS, namespace="test")
Expand Down
4 changes: 3 additions & 1 deletion tests/acceptance/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import pytest

from aiocache import MemcachedCache, RedisCache, SimpleMemoryCache
from aiocache.backends.memcached import MemcachedCache
from aiocache.backends.memory import SimpleMemoryCache
from aiocache.backends.redis import RedisCache
from aiocache.base import _Conn


Expand Down
5 changes: 4 additions & 1 deletion tests/acceptance/test_factory.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import pytest

from aiocache import Cache, MemcachedCache, RedisCache, SimpleMemoryCache
from aiocache import Cache
from aiocache.backends.memcached import MemcachedCache
from aiocache.backends.memory import SimpleMemoryCache
from aiocache.backends.redis import RedisCache


class TestCache:
Expand Down
7 changes: 2 additions & 5 deletions tests/acceptance/test_serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pickle
import random

import pytest
Expand All @@ -6,11 +7,7 @@
try:
import ujson as json
except ImportError:
import json
try:
import cPickle as pickle
except ImportError:
import pickle
import json # type: ignore[no-redef]

from aiocache.serializers import (
BaseSerializer,
Expand Down
5 changes: 2 additions & 3 deletions tests/ut/backends/test_memcached.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import aiomcache
import pytest

from aiocache import MemcachedCache
from aiocache.backends.memcached import MemcachedBackend
from aiocache.backends.memcached import MemcachedBackend, MemcachedCache
from aiocache.base import BaseCache
from aiocache.serializers import JsonSerializer

Expand Down Expand Up @@ -272,7 +271,7 @@ def test_parse_uri_path(self):

@pytest.mark.parametrize(
"namespace, expected",
([None, "test" + pytest.KEY], ["", pytest.KEY], ["my_ns", "my_ns" + pytest.KEY]),
([None, "test" + pytest.KEY], ["", pytest.KEY], ["my_ns", "my_ns" + pytest.KEY]), # type: ignore[attr-defined] # noqa: B950
)
def test_build_key_bytes(self, set_test_namespace, memcached_cache, namespace, expected):
assert memcached_cache.build_key(pytest.KEY, namespace=namespace) == expected.encode()
Expand Down
3 changes: 1 addition & 2 deletions tests/ut/backends/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

import pytest

from aiocache import SimpleMemoryCache
from aiocache.backends.memory import SimpleMemoryBackend
from aiocache.backends.memory import SimpleMemoryBackend, SimpleMemoryCache
from aiocache.base import BaseCache
from aiocache.serializers import NullSerializer

Expand Down
5 changes: 2 additions & 3 deletions tests/ut/backends/test_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import aioredis
import pytest

from aiocache import RedisCache
from aiocache.backends.redis import RedisBackend, conn
from aiocache.backends.redis import RedisBackend, RedisCache, conn
from aiocache.base import BaseCache
from aiocache.serializers import JsonSerializer

pytest.skip("aioredis code is broken", allow_module_level=True)


@pytest.fixture
@pytest.fixture # type: ignore[unreachable]
def redis_connection():
return create_autospec(aioredis.RedisConnection)

Expand Down
4 changes: 3 additions & 1 deletion tests/ut/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

import pytest

from aiocache import MemcachedCache, RedisCache, caches
from aiocache import caches
from aiocache.backends.memcached import MemcachedCache
from aiocache.backends.redis import RedisCache
from aiocache.base import API, BaseCache
from aiocache.plugins import BasePlugin
from aiocache.serializers import BaseSerializer
Expand Down
2 changes: 1 addition & 1 deletion tests/ut/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def set_test_namespace(self, base_cache):

@pytest.mark.parametrize(
"namespace, expected",
([None, "test" + pytest.KEY], ["", pytest.KEY], ["my_ns", "my_ns" + pytest.KEY]),
([None, "test" + pytest.KEY], ["", pytest.KEY], ["my_ns", "my_ns" + pytest.KEY]), # type: ignore[attr-defined] # noqa: B950
)
def test_build_key(self, set_test_namespace, base_cache, namespace, expected):
assert base_cache.build_key(pytest.KEY, namespace=namespace) == expected
Expand Down
3 changes: 2 additions & 1 deletion tests/ut/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

import pytest

from aiocache import SimpleMemoryCache, cached, cached_stampede, multi_cached
from aiocache import cached, cached_stampede, multi_cached
from aiocache.backends.memory import SimpleMemoryCache
from aiocache.base import BaseCache, SENTINEL
from aiocache.decorators import _get_args_dict
from aiocache.lock import RedLock
Expand Down
Loading

0 comments on commit 02782c2

Please sign in to comment.