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
N.B.: There is a shorthand AsyncABC
like abc.ABC
.
import abc
from async_object import AsyncABC
class MyAbstractObject(AsyncABC):
@abc.abstractmethod
def method(self) -> None:
raise NotImplementedError
@abc.abstractmethod
async def async_method(self) -> None:
raise NotImplementedError
Troubleshoots
Static type checking
Static type checkers like mypy
do 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
Release history Release notifications | RSS feed
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.0.tar.gz
(4.5 kB
view hashes)
Built Distribution
Close
Hashes for async_object-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8cc9c6b72484b9ddf028d0e760414f1c0ceac2f7c58246b92856dcd273778fc8 |
|
MD5 | 46ebe56eefde831b3f9aba281be2f0ef |
|
BLAKE2b-256 | d39279bc428c773192f1a82f86fabdf9e28a9164f81c05c78471ff4596afabdf |