Python typing.AsyncContextManager() Examples

The following are 11 code examples of typing.AsyncContextManager(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module typing , or try the search function .
Example #1
Source File: test_typing.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
def test_async_contextmanager(self):
        class NotACM:
            pass
        self.assertIsInstance(ACM(), typing.AsyncContextManager)
        self.assertNotIsInstance(NotACM(), typing.AsyncContextManager)
        @contextlib.contextmanager
        def manager():
            yield 42

        cm = manager()
        self.assertNotIsInstance(cm, typing.AsyncContextManager)
        self.assertEqual(typing.AsyncContextManager[int].__args__, (int,))
        with self.assertRaises(TypeError):
            isinstance(42, typing.AsyncContextManager[int])
        with self.assertRaises(TypeError):
            typing.AsyncContextManager[int, str] 
Example #2
Source File: utils.py    From lbry-sdk with MIT License 5 votes vote down vote up
def aiohttp_request(method, url, **kwargs) -> typing.AsyncContextManager[aiohttp.ClientResponse]:
    async with aiohttp.ClientSession() as session:
        async with session.request(method, url, ssl=get_ssl_context(), **kwargs) as response:
            yield response 
Example #3
Source File: test_requests.py    From aiomoex with The Unlicense 5 votes vote down vote up
def test_iss_client_session():
    assert issubclass(requests.ISSClientSession, typing.AsyncContextManager)
    async with requests.ISSClientSession() as session:
        assert not session.closed
    assert session.closed 
Example #4
Source File: initializers.py    From lahja with MIT License 5 votes vote down vote up
def __init__(
        self, fn: Callable[..., AsyncContextManager[EndpointAPI]], **kwargs: Any
    ) -> None:
        functools.update_wrapper(self, fn)
        self._fn = fn
        self._kwargs = kwargs 
Example #5
Source File: initializers.py    From lahja with MIT License 5 votes vote down vote up
def __call__(self, engine: EngineAPI) -> AsyncContextManager[EndpointAPI]:
        return self._fn(engine, **self._kwargs)


#
# EndpointAPI.serve
# 
Example #6
Source File: initializers.py    From lahja with MIT License 5 votes vote down vote up
def _serve_endpoint(
    engine: EngineAPI, config: ConnectionConfig
) -> AsyncContextManager[EndpointAPI]:
    logger.debug(
        "[%s(%s)].serve(%s)", engine.endpoint_class.__name__, config.name, config.path
    )
    return engine.endpoint_class.serve(config) 
Example #7
Source File: initializers.py    From lahja with MIT License 5 votes vote down vote up
def _run_endpoint(engine: EngineAPI, name: str) -> AsyncContextManager[EndpointAPI]:
    logger.debug("[%s(%s)].run()", engine.endpoint_class.__name__, name)
    # EndpointAPI doesn't specify an __init__ so mypy doesn't understand this.
    return engine.endpoint_class(name).run()  # type: ignore 
Example #8
Source File: contextgroup.py    From trinity with MIT License 5 votes vote down vote up
def __init__(self, context_managers: Sequence[AsyncContextManager[Any]]) -> None:
        self.cms = tuple(context_managers)
        self.cms_to_exit: Sequence[AsyncContextManager[Any]] = tuple() 
Example #9
Source File: services.py    From trinity with MIT License 5 votes vote down vote up
def _run_background_services(
        services: Sequence[ServiceAPI],
        runner: Callable[[ServiceAPI], AsyncContextManager[ManagerAPI]]
) -> None:
    async with contextlib.AsyncExitStack() as stack:
        managers = tuple([
            await stack.enter_async_context(runner(service))
            for service in services
        ])
        # If any of the services terminate, we do so as well.
        await wait_first([
            asyncio.create_task(manager.wait_finished())
            for manager in managers
        ]) 
Example #10
Source File: peer.py    From trinity with MIT License 5 votes vote down vote up
def ParagonPeerPairFactory(*,
                           alice_peer_context: ParagonContext = None,
                           alice_remote: NodeAPI = None,
                           alice_private_key: keys.PrivateKey = None,
                           alice_client_version: str = 'alice',
                           bob_peer_context: ParagonContext = None,
                           bob_remote: NodeAPI = None,
                           bob_private_key: keys.PrivateKey = None,
                           bob_client_version: str = 'bob',
                           event_bus: EndpointAPI = None,
                           ) -> AsyncContextManager[Tuple[ParagonPeer, ParagonPeer]]:
    if alice_peer_context is None:
        alice_peer_context = ParagonContext()
    if bob_peer_context is None:
        bob_peer_context = ParagonContext()

    return cast(AsyncContextManager[Tuple[ParagonPeer, ParagonPeer]], PeerPairFactory(
        alice_peer_context=alice_peer_context,
        alice_peer_factory_class=ParagonPeerFactory,
        bob_peer_context=bob_peer_context,
        bob_peer_factory_class=ParagonPeerFactory,
        alice_remote=alice_remote,
        alice_private_key=alice_private_key,
        alice_client_version=alice_client_version,
        bob_remote=bob_remote,
        bob_private_key=bob_private_key,
        bob_client_version=bob_client_version,
        event_bus=event_bus,
    )) 
Example #11
Source File: paralleltransfer.py    From tgfilestream with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_connection(self) -> AsyncContextManager[Connection]:
        async with self._list_lock:
            conn: Connection = await asyncio.shield(self._next_connection())
            # The connection is locked so reconnections don't stack
            async with conn.lock:
                conn.users += 1
        try:
            yield conn
        finally:
            conn.users -= 1