Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates when python min version is 3.9 #1237

Open
schloerke opened this issue Mar 21, 2024 · 3 comments
Open

Updates when python min version is 3.9 #1237

schloerke opened this issue Mar 21, 2024 · 3 comments

Comments

@schloerke
Copy link
Collaborator

No description provided.

@schloerke
Copy link
Collaborator Author

schloerke commented Mar 21, 2024

  • Add in WrapAsync generic class so that AsyncValueFn can inherit from it.

Definition:

class WrapAsync(Generic[P, R]):
    """
    Make a function asynchronous.

    Parameters
    ----------
    fn
        Function to make asynchronous.

    Returns
    -------
    :
        Asynchronous function (within the `WrapAsync` instance)
    """

    def __init__(self, fn: Callable[P, R] | Callable[P, Awaitable[R]]):
        if isinstance(fn, WrapAsync):
            fn = cast(WrapAsync[P, R], fn)
            return fn
        self._is_async = is_async_callable(fn)
        self._fn = wrap_async(fn)

    async def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R:
        """
        Call the asynchronous function.
        """
        return await self._fn(*args, **kwargs)

    @property
    def is_async(self) -> bool:
        """
        Was the original function asynchronous?

        Returns
        -------
        :
            Whether the original function is asynchronous.
        """
        return self._is_async

    @property
    def fn(self) -> Callable[P, R] | Callable[P, Awaitable[R]]:
        """
        Retrieve the original function

        Returns
        -------
        :
            Original function supplied to the `WrapAsync` constructor.
        """
        return self._fn

Update AsyncValueFn to inherit from WrapAsync:

class AsyncValueFn(WrapAsync[[], IT]):
    ...

@wch
Copy link
Collaborator

wch commented Mar 21, 2024

Just curious: why does this Python 3.9?

@schloerke
Copy link
Collaborator Author

When inheriting from a class that has a ParamSpec, in Python >= 3.9 the paramspec can be defined as []. In python 3.8, it does not understand it and raises. You can pass in a P paramspec, but for the case of AsycnValueFn, the ParamSpec should be empty and I couldn't find a way to define it as empty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants