diff --git a/kombu/__init__.py b/kombu/__init__.py index c435645e..d3ba15ea 100644 --- a/kombu/__init__.py +++ b/kombu/__init__.py @@ -4,6 +4,7 @@ import os import re import sys from collections import namedtuple +from typing import Any, List, cast __version__ = '5.2.4' __author__ = 'Ask Solem' @@ -19,8 +20,8 @@ version_info_t = namedtuple('version_info_t', ( # bumpversion can only search for {current_version} # so we have to parse the version here. -_temp = re.match( - r'(\d+)\.(\d+).(\d+)(.+)?', __version__).groups() +_temp = cast(re.Match, re.match( + r'(\d+)\.(\d+).(\d+)(.+)?', __version__)).groups() VERSION = version_info = version_info_t( int(_temp[0]), int(_temp[1]), int(_temp[2]), _temp[3] or '', '') del(_temp) @@ -61,15 +62,15 @@ all_by_module = { } object_origins = {} -for module, items in all_by_module.items(): +for _module, items in all_by_module.items(): for item in items: - object_origins[item] = module + object_origins[item] = _module class module(ModuleType): """Customized Python module.""" - def __getattr__(self, name): + def __getattr__(self, name: str) -> Any: if name in object_origins: module = __import__(object_origins[name], None, None, [name]) for extra_name in all_by_module[module.__name__]: @@ -77,7 +78,7 @@ class module(ModuleType): return getattr(module, name) return ModuleType.__getattribute__(self, name) - def __dir__(self): + def __dir__(self) -> List[str]: result = list(new_module.__all__) result.extend(('__file__', '__path__', '__doc__', '__all__', '__docformat__', '__name__', '__path__', 'VERSION', diff --git a/kombu/asynchronous/aws/__init__.py b/kombu/asynchronous/aws/__init__.py index d8423c23..a105cca2 100644 --- a/kombu/asynchronous/aws/__init__.py +++ b/kombu/asynchronous/aws/__init__.py @@ -1,4 +1,13 @@ -def connect_sqs(aws_access_key_id=None, aws_secret_access_key=None, **kwargs): +from typing import Any, Optional + +from kombu.asynchronous.aws.sqs.connection import AsyncSQSConnection + + +def connect_sqs( + aws_access_key_id: Optional[str] = None, + aws_secret_access_key: Optional[str] = None, + **kwargs: Any +) -> AsyncSQSConnection: """Return async connection to Amazon SQS.""" from .sqs.connection import AsyncSQSConnection return AsyncSQSConnection( diff --git a/kombu/asynchronous/http/__init__.py b/kombu/asynchronous/http/__init__.py index 1c45ebca..0dc4247d 100644 --- a/kombu/asynchronous/http/__init__.py +++ b/kombu/asynchronous/http/__init__.py @@ -1,17 +1,22 @@ -from kombu.asynchronous import get_event_loop +from typing import TYPE_CHECKING, Optional -from .base import Headers, Request, Response +from kombu.asynchronous import get_event_loop +from kombu.asynchronous.http.base import Headers, Request, Response +from kombu.asynchronous.hub import Hub + +if TYPE_CHECKING: + from kombu.asynchronous.http.curl import CurlClient __all__ = ('Client', 'Headers', 'Response', 'Request') -def Client(hub=None, **kwargs): +def Client(hub: Optional[Hub] = None, **kwargs: int) -> "CurlClient": """Create new HTTP client.""" from .curl import CurlClient return CurlClient(hub, **kwargs) -def get_client(hub=None, **kwargs): +def get_client(hub: Optional[Hub] = None, **kwargs: int) -> "CurlClient": """Get or create HTTP client bound to the current event loop.""" hub = hub or get_event_loop() try: diff --git a/kombu/asynchronous/http/curl.py b/kombu/asynchronous/http/curl.py index bd45c893..8c601956 100644 --- a/kombu/asynchronous/http/curl.py +++ b/kombu/asynchronous/http/curl.py @@ -4,8 +4,9 @@ from collections import deque from functools import partial from io import BytesIO from time import time +from typing import Optional -from kombu.asynchronous.hub import READ, WRITE, get_event_loop +from kombu.asynchronous.hub import READ, WRITE, Hub, get_event_loop from kombu.exceptions import HttpError from kombu.utils.encoding import bytes_to_str @@ -36,7 +37,7 @@ class CurlClient(BaseClient): Curl = Curl - def __init__(self, hub=None, max_clients=10): + def __init__(self, hub: Optional[Hub] = None, max_clients: int = 10): if pycurl is None: raise ImportError('The curl client requires the pycurl library.') hub = hub or get_event_loop() diff --git a/kombu/asynchronous/hub.py b/kombu/asynchronous/hub.py index 2d479db7..73c4a7ff 100644 --- a/kombu/asynchronous/hub.py +++ b/kombu/asynchronous/hub.py @@ -6,6 +6,7 @@ from contextlib import contextmanager from queue import Empty from time import sleep from types import GeneratorType as generator +from typing import Optional from vine import Thenable, promise @@ -19,7 +20,7 @@ from .timer import Timer __all__ = ('Hub', 'get_event_loop', 'set_event_loop') logger = get_logger(__name__) -_current_loop = None +_current_loop: Optional["Hub"] = None W_UNKNOWN_EVENT = """\ Received unknown event %r for fd %r, please contact support!\ @@ -39,12 +40,12 @@ def _dummy_context(*args, **kwargs): yield -def get_event_loop(): +def get_event_loop() -> Optional["Hub"]: """Get current event loop object.""" return _current_loop -def set_event_loop(loop): +def set_event_loop(loop: Optional["Hub"]) -> Optional["Hub"]: """Set the current event loop object.""" global _current_loop _current_loop = loop diff --git a/kombu/transport/__init__.py b/kombu/transport/__init__.py index 5fb5047b..f51b5bbc 100644 --- a/kombu/transport/__init__.py +++ b/kombu/transport/__init__.py @@ -1,10 +1,12 @@ """Built-in transports.""" +from typing import Optional + from kombu.utils.compat import _detect_environment from kombu.utils.imports import symbol_by_name -def supports_librabbitmq(): +def supports_librabbitmq() -> Optional[bool]: """Return true if :pypi:`librabbitmq` can be used.""" if _detect_environment() == 'default': try: @@ -13,6 +15,7 @@ def supports_librabbitmq(): pass else: # pragma: no cover return True + return None TRANSPORT_ALIASES = { @@ -44,7 +47,7 @@ TRANSPORT_ALIASES = { _transport_cache = {} -def resolve_transport(transport=None): +def resolve_transport(transport: Optional[str] = None) -> Optional[str]: """Get transport by name. Arguments: @@ -71,7 +74,7 @@ def resolve_transport(transport=None): return transport -def get_transport_cls(transport=None): +def get_transport_cls(transport: Optional[str] = None) -> Optional[str]: """Get transport class by name. The transport string is the full path to a transport class, e.g.:: diff --git a/kombu/utils/text.py b/kombu/utils/text.py index b930daaf..8cc11f34 100644 --- a/kombu/utils/text.py +++ b/kombu/utils/text.py @@ -3,7 +3,7 @@ from difflib import SequenceMatcher -from typing import Iterator, Optional, Sequence, Tuple, Union +from typing import Iterable, Iterator, Optional, Tuple, Union from kombu import version_info_t @@ -17,7 +17,7 @@ def escape_regex(p, white=''): for c in p) -def fmatch_iter(needle: str, haystack: Sequence[str], min_ratio: float = 0.6) -> Iterator[Tuple[float, str]]: +def fmatch_iter(needle: str, haystack: Iterable[str], min_ratio: float = 0.6) -> Iterator[Tuple[float, str]]: """Fuzzy match: iteratively. Yields: @@ -29,7 +29,7 @@ def fmatch_iter(needle: str, haystack: Sequence[str], min_ratio: float = 0.6) -> yield ratio, key -def fmatch_best(needle: str, haystack: Sequence[str], min_ratio: float = 0.6) -> Optional[str]: +def fmatch_best(needle: str, haystack: Iterable[str], min_ratio: float = 0.6) -> Optional[str]: """Fuzzy match - Find best match (scalar).""" try: return sorted( diff --git a/setup.cfg b/setup.cfg index 80d4da1d..ff949215 100644 --- a/setup.cfg +++ b/setup.cfg @@ -15,7 +15,6 @@ extend-ignore = W504, N806, N802, N801, N803 [mypy] warn_unused_configs = True strict = False -warn_return_any = True follow_imports = skip show_error_codes = True disallow_untyped_defs = True @@ -25,6 +24,14 @@ files = kombu/utils/uuid.py, t/unit/utils/test_uuid.py, kombu/utils/text.py, + kombu/__init__.py, + kombu/asynchronous/__init__.py, + kombu/asynchronous/aws/__init__.py, + kombu/asynchronous/aws/sqs/__init__.py, + kombu/asynchronous/http/__init__.py, + kombu/transport/__init__.py, + kombu/transport/virtual/__init__.py, + kombu/utils/__init__.py, kombu/asynchronous/semaphore.py