Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
Drop shims for Python 3.6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
cburgdorf committed Apr 27, 2020
1 parent 7003142 commit e600f60
Show file tree
Hide file tree
Showing 36 changed files with 94 additions and 109 deletions.
17 changes: 16 additions & 1 deletion p2p/_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import collections
from typing import Hashable, Sequence, Tuple, TypeVar
from typing import Hashable, Sequence, Tuple, TypeVar, AsyncGenerator, Any

import rlp

Expand Down Expand Up @@ -93,3 +93,18 @@ def duplicates(elements: Sequence[TValue]) -> Tuple[TValue, ...]:
collections.Counter(elements).items()
if count > 1
)


TCo = TypeVar('TCo')
TContra = TypeVar('TContra')


class aclosing:
def __init__(self, aiter: AsyncGenerator[TCo, TContra]) -> None:
self._aiter = aiter

async def __aenter__(self) -> AsyncGenerator[TCo, TContra]:
return self._aiter

async def __aexit__(self, *args: Any) -> None:
await self._aiter.aclose()
5 changes: 2 additions & 3 deletions p2p/behaviors.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import contextlib
from typing import (
AsyncIterator,
)

from async_generator import asynccontextmanager

from eth_utils import ValidationError

from p2p.abc import (
Expand Down Expand Up @@ -36,7 +35,7 @@ def should_apply_to(self, connection: 'ConnectionAPI') -> bool:
# mypy bug: https://github.com/python/mypy/issues/708
return self.qualifier(connection, self.logic) # type: ignore

@asynccontextmanager
@contextlib.asynccontextmanager
async def apply(self, connection: ConnectionAPI) -> AsyncIterator[None]:
if self._applied_to is not None:
raise ValidationError(
Expand Down
4 changes: 1 addition & 3 deletions p2p/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
TypeVar,
)

from async_generator import aclosing

import trio

import eth_utils.toolz
Expand Down Expand Up @@ -82,7 +80,7 @@
)
from p2p.kademlia import (
Address, Node, check_relayed_addr, create_stub_enr, sort_by_distance, KademliaRoutingTable)
from p2p._utils import get_logger
from p2p._utils import get_logger, aclosing
from p2p import trio_utils

# V4 handler are async methods that take a Node, payload and msg_hash as arguments.
Expand Down
5 changes: 2 additions & 3 deletions p2p/discv5/message_dispatcher.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import logging
import random
from types import (
Expand All @@ -14,8 +15,6 @@
TypeVar,
)

from async_generator import asynccontextmanager

import trio
from trio.abc import (
ReceiveChannel,
Expand Down Expand Up @@ -289,7 +288,7 @@ async def get_endpoint_from_node_db(self, receiver_node_id: NodeID) -> Endpoint:

return Endpoint(ip_address, udp_port)

@asynccontextmanager
@contextlib.asynccontextmanager
async def request_response_subscription(self,
receiver_node_id: NodeID,
message: BaseMessage,
Expand Down
4 changes: 2 additions & 2 deletions p2p/exchange/exchange.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from functools import partial
import contextlib
from typing import (
AsyncIterator,
Callable,
Type,
)

from async_generator import asynccontextmanager
from async_service import background_asyncio_service

from p2p.abc import ConnectionAPI
Expand All @@ -25,7 +25,7 @@ class BaseExchange(ExchangeAPI[TRequestCommand, TResponseCommand, TResult]):
def __init__(self) -> None:
self.tracker = self.tracker_class()

@asynccontextmanager
@contextlib.asynccontextmanager
async def run_exchange(self, connection: ConnectionAPI) -> AsyncIterator[None]:
protocol = connection.get_protocol_for_command_type(self.get_request_cmd_type())

Expand Down
5 changes: 2 additions & 3 deletions p2p/exchange/logic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import contextlib
from typing import Any, AsyncIterator

from async_generator import asynccontextmanager

from p2p.abc import ConnectionAPI, LogicAPI
from p2p.exceptions import UnknownProtocol
from p2p.logic import BaseLogic
Expand Down Expand Up @@ -29,7 +28,7 @@ def qualifier(self, connection: ConnectionAPI, logic: LogicAPI) -> bool:
else:
return protocol.supports_command(self.exchange.get_response_cmd_type())

@asynccontextmanager
@contextlib.asynccontextmanager
async def apply(self, connection: ConnectionAPI) -> AsyncIterator[None]:
async with self.exchange.run_exchange(connection):
yield
10 changes: 4 additions & 6 deletions p2p/logic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from abc import abstractmethod
import contextlib
from typing import (
cast,
AsyncIterator,
Expand All @@ -7,9 +8,6 @@
Type,
)

from async_generator import asynccontextmanager
from async_exit_stack import AsyncExitStack

from p2p.abc import (
BehaviorAPI,
ConnectionAPI,
Expand Down Expand Up @@ -49,7 +47,7 @@ class CommandHandler(BaseLogic, Generic[TCommand]):
def qualifier(self) -> QualifierFn: # type: ignore
return HasCommand(self.command_type)

@asynccontextmanager
@contextlib.asynccontextmanager
async def apply(self, connection: ConnectionAPI) -> AsyncIterator[None]:
self.connection = connection

Expand All @@ -76,11 +74,11 @@ class Application(BaseLogic):
def add_child_behavior(self, behavior: BehaviorAPI) -> None:
self._behaviors += (behavior,)

@asynccontextmanager
@contextlib.asynccontextmanager
async def apply(self, connection: ConnectionAPI) -> AsyncIterator[None]:
self.connection = connection

async with AsyncExitStack() as stack:
async with contextlib.AsyncExitStack() as stack:
# First apply all the child behaviors
for behavior in self._behaviors:
if behavior.should_apply_to(connection):
Expand Down
5 changes: 2 additions & 3 deletions p2p/multiplexer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import collections
import contextlib
import time
from typing import (
Any,
Expand All @@ -15,8 +16,6 @@

from cached_property import cached_property

from async_generator import asynccontextmanager

from eth_utils import ValidationError
from eth_utils.toolz import cons
import rlp
Expand Down Expand Up @@ -291,7 +290,7 @@ async def _stream_protocol_messages(self,
#
# Message reading and streaming API
#
@asynccontextmanager
@contextlib.asynccontextmanager
async def multiplex(self) -> AsyncIterator[None]:
"""
API for running the background task that feeds individual protocol
Expand Down
5 changes: 2 additions & 3 deletions p2p/p2p_api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import asyncio
import contextlib
from typing import Any, AsyncIterator, cast

from async_generator import asynccontextmanager

from cached_property import cached_property

from async_service import (
Expand Down Expand Up @@ -80,7 +79,7 @@ class DisconnectIfIdle(BaseLogic):
def __init__(self, idle_timeout: float) -> None:
self.idle_timeout = idle_timeout

@asynccontextmanager
@contextlib.asynccontextmanager
async def apply(self, connection: ConnectionAPI) -> AsyncIterator[None]:
service = PingAndDisconnectIfIdle(connection, self.idle_timeout)
async with background_asyncio_service(service):
Expand Down
4 changes: 1 addition & 3 deletions p2p/peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
TYPE_CHECKING,
)

from async_exit_stack import AsyncExitStack

from async_service import Service

from lahja import EndpointAPI
Expand Down Expand Up @@ -261,7 +259,7 @@ async def run(self) -> None:
self._start_time = time.monotonic()
self.connection.add_command_handler(Disconnect, cast(HandlerFn, self._handle_disconnect))
try:
async with AsyncExitStack() as stack:
async with contextlib.AsyncExitStack() as stack:
await stack.enter_async_context(P2PAPI().as_behavior().apply(self.connection))
self.p2p_api = self.connection.get_logic('p2p', P2PAPI)

Expand Down
5 changes: 2 additions & 3 deletions p2p/resource_lock.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import asyncio
from collections import defaultdict
from collections.abc import Hashable
import contextlib
from typing import AsyncIterator, DefaultDict, Dict, Generic, TypeVar

from async_generator import asynccontextmanager


TResource = TypeVar('TResource', bound=Hashable)

Expand All @@ -20,7 +19,7 @@ def __init__(self) -> None:
self._locks = {}
self._reference_counts = defaultdict(int)

@asynccontextmanager
@contextlib.asynccontextmanager
async def lock(self, resource: TResource) -> AsyncIterator[None]:
if resource not in self._locks:
self._locks[resource] = asyncio.Lock()
Expand Down
4 changes: 2 additions & 2 deletions p2p/service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from abc import abstractmethod
import asyncio
import concurrent
import contextlib
import functools
import time
from typing import (
Expand All @@ -15,7 +16,6 @@
from weakref import WeakSet

from async_service import Service, ServiceAPI
from async_generator import asynccontextmanager

from cancel_token import CancelToken, OperationCancelled
from eth_utils import (
Expand Down Expand Up @@ -423,7 +423,7 @@ async def _cleanup(self) -> None:
TService = TypeVar('TService', bound=AsyncioServiceAPI)


@asynccontextmanager
@contextlib.asynccontextmanager
async def run_service(service: TService) -> AsyncIterator[TService]:
task = asyncio.ensure_future(service.run())
await service.events.started.wait()
Expand Down
5 changes: 2 additions & 3 deletions p2p/tools/factories/connection.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import asyncio
import contextlib
from typing import AsyncIterator, Tuple

from async_generator import asynccontextmanager

from async_service import background_asyncio_service

from eth_keys import keys
Expand All @@ -20,7 +19,7 @@
from .transport import MemoryTransportPairFactory


@asynccontextmanager
@contextlib.asynccontextmanager
async def ConnectionPairFactory(*,
alice_handshakers: Tuple[HandshakerAPI[ProtocolAPI], ...] = (),
bob_handshakers: Tuple[HandshakerAPI[ProtocolAPI], ...] = (),
Expand Down
5 changes: 2 additions & 3 deletions p2p/tools/factories/peer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import asyncio
import contextlib
from typing import cast, AsyncContextManager, AsyncIterator, Tuple, Type

from async_generator import asynccontextmanager

from lahja import EndpointAPI

from eth_keys import keys
Expand All @@ -15,7 +14,7 @@
from .connection import ConnectionPairFactory


@asynccontextmanager
@contextlib.asynccontextmanager
async def PeerPairFactory(*,
alice_peer_context: BasePeerContext,
alice_peer_factory_class: Type[BasePeerFactory],
Expand Down
3 changes: 0 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

deps = {
'p2p': [
"async-exit-stack==1.0.1",
"async-generator==1.10",
"async-service==0.1.0a7",
"asyncio-cancel-token>=0.2,<0.3",
"async_lru>=0.1.0,<1.0.0",
Expand All @@ -34,7 +32,6 @@
"bloom-filter==1.3",
"cachetools>=3.1.0,<4.0.0",
"coincurve>=10.0.0,<11.0.0",
"dataclasses>=0.6, <1;python_version<'3.7'",
"eth-utils>=1.8.4,<2",
# Fixing this dependency due to: requests 2.20.1 has requirement
# idna<2.8,>=2.5, but you'll have idna 2.8 which is incompatible.
Expand Down
5 changes: 2 additions & 3 deletions tests-trio/p2p-trio/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import contextlib
import logging

import trio
import pytest_trio

from async_generator import asynccontextmanager

from async_service import background_trio_service

from eth_hash.auto import keccak
Expand Down Expand Up @@ -38,7 +37,7 @@ async def socket_pair():
return sending_socket, receiving_socket


@asynccontextmanager
@contextlib.asynccontextmanager
async def _manually_driven_discovery(seed, socket, nursery):
_, port = socket.getsockname()
discovery = ManuallyDrivenDiscoveryService(
Expand Down
6 changes: 2 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import asyncio
import contextlib
import os
from pathlib import Path
import tempfile
import uuid

from async_generator import (
asynccontextmanager,
)
import pytest

from async_service import background_asyncio_service
Expand Down Expand Up @@ -97,7 +95,7 @@ def event_loop():
loop.close()


@asynccontextmanager
@contextlib.asynccontextmanager
async def make_networking_event_bus():
# Tests run concurrently, therefore we need unique IPC paths
ipc_path = Path(f"networking-{uuid.uuid4()}.ipc")
Expand Down
6 changes: 2 additions & 4 deletions tests/core/integration_test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import asyncio
import contextlib
from enum import Enum
from pathlib import Path
from tempfile import TemporaryDirectory
from zipfile import ZipFile

from async_generator import (
asynccontextmanager,
)
from async_service import background_asyncio_service
from cancel_token import OperationCancelled
from eth_keys import keys
Expand Down Expand Up @@ -93,7 +91,7 @@ def load_fixture_db(db_fixture, db_class=LevelDB):
yield db_class(Path(tmpdir) / db_fixture.value)


@asynccontextmanager
@contextlib.asynccontextmanager
async def run_peer_pool_event_server(event_bus, peer_pool, handler_type=None):

handler_type = DefaultPeerPoolEventServer if handler_type is None else handler_type
Expand Down
Loading

0 comments on commit e600f60

Please sign in to comment.