From f37dd1faa2046a2ebb88c098eb03e56a95534ec6 Mon Sep 17 00:00:00 2001 From: Anton Agestam Date: Wed, 9 Oct 2019 20:12:28 +0200 Subject: [PATCH] fix various failing strict type checks --- collectfast/management/commands/collectstatic.py | 3 ++- collectfast/strategies/base.py | 2 +- .../tests/strategies/test_caching_hash_strategy.py | 4 +++- collectfast/tests/strategies/test_hash_strategy.py | 3 ++- collectfast/tests/utils.py | 14 ++++++++++---- lint-requirements.txt | 2 +- setup.cfg | 2 +- 7 files changed, 20 insertions(+), 10 deletions(-) diff --git a/collectfast/management/commands/collectstatic.py b/collectfast/management/commands/collectstatic.py index 456c410..040b4bd 100644 --- a/collectfast/management/commands/collectstatic.py +++ b/collectfast/management/commands/collectstatic.py @@ -31,7 +31,7 @@ def __init__(self, *args, **kwargs): @staticmethod def _load_strategy(): - # type: () -> Type[Strategy] + # type: () -> Type[Strategy[Storage]] strategy_str = getattr(django_settings, "COLLECTFAST_STRATEGY", None) if strategy_str is not None: return load_strategy(strategy_str) @@ -78,6 +78,7 @@ def collect(self): return ret def handle(self, *args, **options): + # type: (Any, Any) -> str """Override handle to suppress summary output.""" super().handle(**options) return "{} static file{} copied.".format( diff --git a/collectfast/strategies/base.py b/collectfast/strategies/base.py index f75848f..3b0ca0e 100644 --- a/collectfast/strategies/base.py +++ b/collectfast/strategies/base.py @@ -138,7 +138,7 @@ def get_gzipped_local_file_hash(self, uncompressed_file_hash, path, contents): return str(file_hash) -def load_strategy(klass: Union[str, type, object]) -> Type[Strategy]: +def load_strategy(klass: Union[str, type, object]) -> Type[Strategy[Storage]]: if isinstance(klass, str): klass = locate(klass) if not isinstance(klass, type) or not issubclass(klass, Strategy): diff --git a/collectfast/tests/strategies/test_caching_hash_strategy.py b/collectfast/tests/strategies/test_caching_hash_strategy.py index 016ae54..c15eacd 100644 --- a/collectfast/tests/strategies/test_caching_hash_strategy.py +++ b/collectfast/tests/strategies/test_caching_hash_strategy.py @@ -12,11 +12,13 @@ hash_characters = string.ascii_letters + string.digits -class Strategy(CachingHashStrategy): +class Strategy(CachingHashStrategy[FileSystemStorage]): def __init__(self): + # type: () -> None super().__init__(FileSystemStorage()) def get_remote_file_hash(self, prefixed_path): + # type: (str) -> None pass diff --git a/collectfast/tests/strategies/test_hash_strategy.py b/collectfast/tests/strategies/test_hash_strategy.py index e375d4f..cfdbb4e 100644 --- a/collectfast/tests/strategies/test_hash_strategy.py +++ b/collectfast/tests/strategies/test_hash_strategy.py @@ -10,12 +10,13 @@ from collectfast.tests.utils import test -class Strategy(HashStrategy): +class Strategy(HashStrategy[FileSystemStorage]): def __init__(self): # type: () -> None super().__init__(FileSystemStorage()) def get_remote_file_hash(self, prefixed_path): + # type: (str) -> None pass diff --git a/collectfast/tests/utils.py b/collectfast/tests/utils.py index 95715ab..2cb804a 100644 --- a/collectfast/tests/utils.py +++ b/collectfast/tests/utils.py @@ -6,6 +6,8 @@ import uuid from typing import Any from typing import Callable +from typing import cast +from typing import Type from typing import TypeVar from django.conf import settings as django_settings @@ -14,11 +16,12 @@ from collectfast import settings -F = TypeVar("F", bound=Callable) +F = TypeVar("F", bound=Callable[..., Any]) static_dir = pathlib.Path(django_settings.STATICFILES_DIRS[0]) # type: Final def test(func): + # type: (F) -> Type[unittest.TestCase] """ Creates a class that inherits from `unittest.TestCase` with the decorated function as a method. Create tests like this: @@ -34,8 +37,9 @@ def test(func): def test_many(**mutations): - # type: (Callable[[F], F]) -> Callable[[F], F] + # type: (Callable[[F], F]) -> Callable[[F], Type[unittest.TestCase]] def test(func): + # type: (F) -> Type[unittest.TestCase] """ Creates a class that inherits from `unittest.TestCase` with the decorated function as a method. Create tests like this: @@ -77,6 +81,7 @@ def clean_static_dir(): def override_setting(name, value): # type: (str, Any) -> Callable[[F], F] def decorator(fn): + # type: (F) -> F @functools.wraps(fn) def wrapper(*args, **kwargs): original = getattr(settings, name) @@ -86,7 +91,7 @@ def wrapper(*args, **kwargs): finally: setattr(settings, name, original) - return wrapper + return cast(F, wrapper) return decorator @@ -94,6 +99,7 @@ def wrapper(*args, **kwargs): def override_storage_attr(name, value): # type: (str, Any) -> Callable[[F], F] def decorator(fn): + # type: (F) -> F @functools.wraps(fn) def wrapper(*args, **kwargs): storage = import_string(django_settings.STATICFILES_STORAGE) @@ -104,6 +110,6 @@ def wrapper(*args, **kwargs): finally: setattr(storage, name, original) - return wrapper + return cast(F, wrapper) return decorator diff --git a/lint-requirements.txt b/lint-requirements.txt index a0577a3..62dacf6 100644 --- a/lint-requirements.txt +++ b/lint-requirements.txt @@ -3,4 +3,4 @@ flake8-bugbear black sorti mypy>=0.730 -git+https://github.com/typeddjango/django-stubs.git@b939bc96b7ff0645da19c2ff960ac9bd0a50bee7#egg=django_stubs +django-stubs>=1.2.0 diff --git a/setup.cfg b/setup.cfg index 00b398f..ebe4b25 100644 --- a/setup.cfg +++ b/setup.cfg @@ -6,7 +6,7 @@ max-line-length = 88 # https://github.com/PyCQA/pyflakes/issues/475 # see this discussion as to why we're ignoring E722 # https://github.com/PyCQA/pycodestyle/issues/703 -extend-ignore = F403 E265 E722 F821 +extend-ignore = E722 F821 [mypy] python_version = 3.4