<commit_before># Copyright 2018
# Licensed under the MIT license


import collections.abc
from functools import wraps
from typing import Any, Callable, Coroutine, TypeVar

from typing_extensions import AsyncContextManager

_T = TypeVar("_T")


class ContextManager(collections.abc.Coroutine):
    __slots__ = ("_coro", "_obj")

    def __init__(self, coro):
        self._coro = coro
        self._obj = None

    def send(self, value):
        return self._coro.send(value)

    def throw(self, typ, val=None, tb=None):
        if val is None:
            return self._coro.throw(typ)

        if tb is None:
            return self._coro.throw(typ, val)

        return self._coro.throw(typ, val, tb)

    def close(self):
        return self._coro.close()

    def __await__(self):
        return self._coro.__await__()

    async def __aenter__(self):
        self._obj = await self._coro
        return self._obj

    async def __aexit__(self, exc_type, exc, tb):
        await self._obj.close()
        self._obj = None


def contextmanager(
    method: Callable[..., Coroutine[Any, Any, _T]]
) -> Callable[..., AsyncContextManager[_T]]:
    @wraps(method)
    def wrapper(self, *args, **kwargs) -> ContextManager:
        return ContextManager(method(self, *args, **kwargs))

    return wrapper<commit_msg>Fix type annotations and base classes for @contextmanager results<commit_after># Copyright 2018
# Licensed under the MIT license


from functools import wraps
from typing import Any, Callable, Coroutine, Generator, TypeVar

from typing_extensions import AsyncContextManager

from .cursor import Cursor

_T = TypeVar("_T")


class Result(AsyncContextManager[_T], Coroutine[Any, Any, _T]):
    __slots__ = ("_coro", "_obj")

    def __init__(self, coro: Coroutine[Any, Any, _T]):
        self._coro = coro
        self._obj: _T

    def send(self, value) -> None:
        return self._coro.send(value)

    def throw(self, typ, val=None, tb=None) -> None:
        if val is None:
            return self._coro.throw(typ)

        if tb is None:
            return self._coro.throw(typ, val)

        return self._coro.throw(typ, val, tb)

    def close(self) -> None:
        return self._coro.close()

    def __await__(self) -> Generator[Any, None, _T]:
        return self._coro.__await__()

    async def __aenter__(self) -> _T:
        self._obj = await self._coro
        return self._obj

    async def __aexit__(self, exc_type, exc, tb) -> None:
        if isinstance(self._obj, Cursor):
            await self._obj.close()


def contextmanager(
    method: Callable[..., Coroutine[Any, Any, _T]]
) -> Callable[..., Result[_T]]:
    @wraps(method)
    def wrapper(self, *args, **kwargs) -> Result[_T]:
        return Result(method(self, *args, **kwargs))

    return wrapper
{'fraction_non_alphanumeric': 0.11707841031149302, 'fraction_numerical': 0.0028643036161833156, 'mean_word_length': 2.8065395095367847, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7278760', 'n_tokens_mistral': 896, 'n_tokens_neox': 871, 'n_words': 251}