asyncutils.properties

Asynchronous descriptors, mimicking property and optionally applying a lock.

Classes

AsyncPropertyBase

A property with asynchronous getters, setters and deleters.

ConcurrentAsyncProperty

Allows set and delete operations to run concurrently once the operations are called, without any guarantee on the order of execution.

LazyAsyncProperty

A property that queues set and delete operations.

RWLockedAsyncProperty

Apply a reader-writer lock to the property. Naturally, setters and deleters are writers and getters are readers.

Module Contents

class asyncutils.properties.AsyncPropertyBase[T, R][source]

Bases: abc.ABC

A property with asynchronous getters, setters and deleters.

Create a new async property with getter fget, setter fset and deleter fdel.
If the getter is not provided, return a partial decorator instead. In that overload, none of the accessors are to be passed.
doc, if passed, will be the docstring of the property in the form of the __doc__ attribute.
Otherwise, an attempt is made to find it on the getter.
strict defaults to True, and controls whether performing an operation that invokes an unset accessor is allowed. If it is False, setters and deleters are also allowed to return something other than None.
If the property has no getter (only possible with explicit fget=None and at least one of fset and fdel passed, which is rare), accessing the attribute on instances would return the property itself if strict=False and raise an AttributeError otherwise.
If hide is True (default False), accessing the attribute on the class it is defined in would raise AttributeError as if the property didn’t exist.
Subclasses must define wrap_aw(), and are allowed to override _initialize() and _repr_accessor(). Nothing else is customizable.
__delete__(instance: R, /) None[source]
async __get__(instance: R, owner: type[R] | None = ..., /) T[source]
async __get__(instance: None, owner: type, /) Self
__getattr__(name: str, /) Any[source]

Find the attribute on the getter if it exists.

classmethod __init_subclass__(
/,
*,
lock_factory: collections.abc.Callable[[],
asyncutils._internal.prots.AsyncContextManager[Any]]=...,
**k: object,
) None[source]

lock_factory, a callable that returns a new per-instance async context manager, is required for immediate subclasses.

__reduce__() str[source]

Return the qualified name of this property for pickling. Hidden properties cannot be pickled.

__set__(instance: R, value: T, /) None[source]
__set_name__(typ: type[R], name: str, /) None[source]
_initialize(
fget: collections.abc.Callable[[R], collections.abc.Awaitable[T]],
/,
fset: collections.abc.Callable[[R, T], collections.abc.Awaitable[None]] | None = ...,
fdel: collections.abc.Callable[[R], collections.abc.Awaitable[None]] | None = ...,
*,
doc: str | None = ...,
strict: bool = ...,
hide: bool = ...,
) None[source]

Set the necessary attributes on the property; called at construction.

static _repr_accessor(
accessor: collections.abc.Callable[Concatenate[R, Ellipsis], collections.abc.Awaitable[T | None]] | None,
/,
) str

Called by the implementation of __repr__() sequentially with each accessor as argument.

deleter(fdel: collections.abc.Callable[[R], collections.abc.Awaitable[None]], /) Self[source]

Return another async property with the given function as the deleter.

getter(fget: collections.abc.Callable[[R], collections.abc.Awaitable[T]], /) Self[source]

Return another async property with the given function as the getter.

setter(fset: collections.abc.Callable[[R, T], collections.abc.Awaitable[None]], /) Self[source]

Return another async property with the given function as the setter.

abstractmethod wrap_aw[S](aw: collections.abc.Awaitable[S], /) collections.abc.Awaitable[S][source]

Return an awaitable resolving to the result of an awaitable, limited to those returned by the setter or deleter. This can be a coroutine, a future, a task or anything else, and affects the strategy used to handle assignments and deletions which must return synchronously but run in the background.

__doc__: str | None

The docstring for this property, or None if it doesn’t exist.

__module__: str | None

The module this property is defined in, determined by the function it decorates.

__name__: str

The name of this property, determined by the function it decorates.

property fdel: collections.abc.Callable[[R], collections.abc.Awaitable[None]] | None

The deleter function for this property, or None if it doesn’t exist.

property fget: collections.abc.Callable[[R], collections.abc.Awaitable[T]] | None

The getter function for this property, or None if it doesn’t exist.

property fset: collections.abc.Callable[[R, T], collections.abc.Awaitable[None]] | None

The setter function for this property, or None if it doesn’t exist.

class asyncutils.properties.ConcurrentAsyncProperty[T, R][source]

Bases: AsyncPropertyBase[T, R]

Allows set and delete operations to run concurrently once the operations are called, without any guarantee on the order of execution.

The setters and deleters can be implemented acquire a writer lock and the getter the corresponding reader lock from rwlocks with its lock policies that provide fluent decorator interfaces.
Note, however, that the accessor decorators must be outermost because they turn callables into properties.
wrap_aw[S](aw: collections.abc.Awaitable[S], /) asyncio.Task[S]

Return a task for the awaitable returned by the setter or deleter.

__doc__: str | None

The docstring for this property, or None if it doesn’t exist.

__module__: str | None

The module this property is defined in, determined by the function it decorates.

__name__: str

The name of this property, determined by the function it decorates.

class asyncutils.properties.LazyAsyncProperty[T, R][source]

Bases: AsyncPropertyBase[T, R]

A property that queues set and delete operations.

Operations are completed only when a get is called, strictly in order.

async wrap_aw[S](aw: collections.abc.Awaitable[S], /) S

Wrap the awaitable in a coroutine, run lazily.

__doc__: str | None

The docstring for this property, or None if it doesn’t exist.

__module__: str | None

The module this property is defined in, determined by the function it decorates.

__name__: str

The name of this property, determined by the function it decorates.

class asyncutils.properties.RWLockedAsyncProperty[T, R][source]

Bases: ConcurrentAsyncProperty[T, R]

Apply a reader-writer lock to the property. Naturally, setters and deleters are writers and getters are readers.

policy is the class used to create the readers-writer lock for the property. It must subclass RWLock.

_initialize(
f: collections.abc.Callable[[R], collections.abc.Awaitable[T]],
/,
fset: collections.abc.Callable[[R, T], collections.abc.Awaitable[None]] | None = None,
fdel: collections.abc.Callable[[R], collections.abc.Awaitable[None]] | None = None,
*,
policy: type[asyncutils.rwlocks.RWLock] = ...,
**k: object,
) None[source]

Set the necessary attributes on the property; called at construction.

static _repr_accessor(v: collections.abc.Callable[Ellipsis, collections.abc.Awaitable[Any]] | None, /) str

Called by the implementation of __repr__() sequentially with each accessor as argument.

__doc__: str | None

The docstring for this property, or None if it doesn’t exist.

__module__: str | None

The module this property is defined in, determined by the function it decorates.

__name__: str

The name of this property, determined by the function it decorates.