Skip to main content

async-object let you write classes with async def __init__.

Project description

async-object

async-object let you write classes with async def __init__

Usage

It is simple, with async-object you can do this :

from async_object import AsyncObject


class MyObject(AsyncObject):
    async def __new__(cls) -> "MyObject":
        self = await super().__new__(cls)

        # Do some async stuff

        return self

    async def __init__(self) -> None:
        await super().__init__()

        # Do some async stuff


if __name__ == "__main__":
    import asyncio

    async def main() -> None:
        instance = await MyObject()
        assert isinstance(instance, MyObject)

    asyncio.run(main())

Abstract base classes

import abc

from async_object import AsyncObject, AsyncABCMeta


class MyAbstractObject(AsyncObject, metaclass=AsyncABCMeta):
    @abc.abstractmethod
    def method(self) -> None:
        raise NotImplementedError

    @abc.abstractmethod
    async def async_method(self) -> None:
        raise NotImplementedError


class MyObject(MyAbstractObject):
    async def __init__(self, arg1: int, arg2: str) -> None:
        await super().__init__()

    def method(self) -> None:
        pass

    async def async_method(self) -> None:
        pass

Troubleshoots

Static type checking

Static type checker like mypy does not like having async def for __new__ and __init__. You can use # type: ignore[misc] comment to mask these errors when overriding these methods.

class MyObject(AsyncObject):
    async def __new__(cls) -> "MyObject":  # type: ignore[misc]
        return await super().__new__(cls)

    async def __init__(self) -> None:  # type: ignore[misc]
        await super().__init__()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

async-object-1.0.0rc0.tar.gz (4.0 kB view hashes)

Uploaded Source

Built Distribution

async_object-1.0.0rc0-py3-none-any.whl (4.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page