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
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.0rc0.tar.gz
(4.0 kB
view hashes)
Built Distribution
Close
Hashes for async_object-1.0.0rc0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 18cb0168cba6d6cc81d5ebbe0c4ae6b683c644c49b9f9bd5ea4e85d7f525ff77 |
|
MD5 | 72e1c015487f63695a2ac94427daa56a |
|
BLAKE2b-256 | ced01785e0ad9967db5c97cf482fd260a22ef952e2a7d1adc39530371cf5c561 |