diff --git a/aio_pika/__init__.py b/aio_pika/__init__.py index 516eb51e..e18002a4 100644 --- a/aio_pika/__init__.py +++ b/aio_pika/__init__.py @@ -13,12 +13,8 @@ from .robust_queue import RobustQueue -try: - from importlib.metadata import Distribution - __version__ = Distribution.from_name("aio-pika").version -except ImportError: - import pkg_resources - __version__ = pkg_resources.get_distribution("aio-pika").version +from importlib.metadata import Distribution +__version__ = Distribution.from_name("aio-pika").version __all__ = ( diff --git a/aio_pika/abc.py b/aio_pika/abc.py index 1f27022b..95873337 100644 --- a/aio_pika/abc.py +++ b/aio_pika/abc.py @@ -1,6 +1,5 @@ import asyncio import dataclasses -import sys from abc import ABC, abstractmethod from dataclasses import dataclass from datetime import datetime, timedelta @@ -9,16 +8,10 @@ from types import TracebackType from typing import ( Any, AsyncContextManager, AsyncIterable, Awaitable, Callable, Dict, - Generator, Iterator, Mapping, Optional, Tuple, Type, TypeVar, Union, - overload, + Generator, Iterator, Literal, Mapping, Optional, Tuple, Type, TypedDict, + TypeVar, Union, overload, ) - -if sys.version_info >= (3, 8): - from typing import Literal, TypedDict -else: - from typing_extensions import Literal, TypedDict - import aiormq.abc from aiormq.abc import ExceptionType from pamqp.common import Arguments, FieldValue diff --git a/aio_pika/queue.py b/aio_pika/queue.py index 88ad4b20..fca449c9 100644 --- a/aio_pika/queue.py +++ b/aio_pika/queue.py @@ -1,10 +1,9 @@ import asyncio -import sys from asyncio import Future from functools import partial from types import TracebackType from typing import ( - Any, Awaitable, Callable, Optional, Type, cast, overload, + Any, Awaitable, Callable, Literal, Optional, Type, cast, overload, ) import aiormq @@ -23,12 +22,6 @@ from .tools import CallbackCollection, create_task, ensure_awaitable -if sys.version_info >= (3, 8): - from typing import Literal -else: - from typing_extensions import Literal - - log = get_logger(__name__) @@ -546,7 +539,6 @@ def __aiter__(self) -> "AbstractQueueIterator": async def __aenter__(self) -> "AbstractQueueIterator": if not hasattr(self, "_consumer_tag"): await self.consume() - return self async def __aexit__( diff --git a/aio_pika/robust_connection.py b/aio_pika/robust_connection.py index c1a28368..091f7bc4 100644 --- a/aio_pika/robust_connection.py +++ b/aio_pika/robust_connection.py @@ -103,10 +103,6 @@ async def _on_connected(self) -> None: except Exception: log.exception("Failed to reopen channel") raise - except asyncio.CancelledError: - # In python 3.7 asyncio.CancelledError inherited - # from Exception and this needed for catch it first - raise except Exception as e: closing = self.loop.create_future() closing.set_exception(e) diff --git a/aio_pika/tools.py b/aio_pika/tools.py index daec83ea..890a794b 100644 --- a/aio_pika/tools.py +++ b/aio_pika/tools.py @@ -19,25 +19,12 @@ def iscoroutinepartial(fn: Callable[..., Any]) -> bool: """ - Function returns True if function is a partial instance of coroutine. - See additional information here_. - - :param fn: Function - :return: bool - - .. _here: https://goo.gl/C0S4sQ - + Use Python 3.8's inspect.iscoroutinefunction() instead """ - - while True: - parent = fn - - fn = getattr(parent, "func", None) # type: ignore - - if fn is None: - break - - return asyncio.iscoroutinefunction(parent) + warnings.warn( + "Use inspect.iscoroutinefunction() instead.", DeprecationWarning + ) + return asyncio.iscoroutinefunction(fn) def _task_done(future: asyncio.Future) -> None: @@ -57,8 +44,8 @@ def create_task( ) -> Awaitable[T]: loop = loop or asyncio.get_event_loop() - if iscoroutinepartial(func): - task = loop.create_task(func(*args, **kwargs)) # type: ignore + if inspect.iscoroutinefunction(func): + task = loop.create_task(func(*args, **kwargs)) task.add_done_callback(_task_done) return task @@ -260,7 +247,7 @@ def ensure_awaitable( if inspect.iscoroutinefunction(func): return func - if inspect.isfunction(func) and not iscoroutinepartial(func): + if inspect.isfunction(func): warnings.warn( f"You probably registering the non-coroutine function {func!r}. " "This is deprecated and will be removed in future releases. " diff --git a/docs/source/conf.py b/docs/source/conf.py index 3417ebf4..92a0e9d5 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -23,12 +23,8 @@ # noinspection PyUnresolvedReferences -try: - from importlib.metadata import Distribution - __version__ = Distribution.from_name("aio-pika").version -except ImportError: - import pkg_resources - __version__ = pkg_resources.get_distribution("aio-pika").version +from importlib.metadata import Distribution +__version__ = Distribution.from_name("aio-pika").version sys.path.insert(0, os.path.abspath(os.path.dirname("__file__")))