From a692c55834f906af1f6d82a27cd789354b2b3ad4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Gr=C3=B6nholm?= Date: Thu, 1 Feb 2024 22:05:54 +0200 Subject: [PATCH] Fixed documentation on providing custom typed attributes --- docs/typedattrs.rst | 16 ++++++++++------ docs/versionhistory.rst | 1 + 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/docs/typedattrs.rst b/docs/typedattrs.rst index 89b18280..f75ea191 100644 --- a/docs/typedattrs.rst +++ b/docs/typedattrs.rst @@ -34,21 +34,24 @@ Defining your own typed attributes By convention, typed attributes are stored together in a container class with other attributes of the same category:: - from anyio import TypedAttribute, TypedAttributeSet + from anyio import TypedAttributeSet, typed_attribute - class MyTypedAttribute: - string_valued_attribute = TypedAttribute[str]() - some_float_attribute = TypedAttribute[float]() + class MyTypedAttribute(TypedAttributeSet): + string_valued_attribute: str = typed_attribute() + some_float_attribute: float = typed_attribute() To provide values for these attributes, implement the :meth:`~.TypedAttributeProvider.extra_attributes` property in your class:: + from collections.abc import Callable, Mapping + from anyio import TypedAttributeProvider class MyAttributeProvider(TypedAttributeProvider): - def extra_attributes(): + @property + def extra_attributes() -> Mapping[Any, Callable[[], Any]]: return { MyTypedAttribute.string_valued_attribute: lambda: 'my attribute value', MyTypedAttribute.some_float_attribute: lambda: 6.492 @@ -58,7 +61,8 @@ If your class inherits from another typed attribute provider, make sure you incl attributes in the return value:: class AnotherAttributeProvider(MyAttributeProvider): - def extra_attributes(): + @property + def extra_attributes() -> Mapping[Any, Callable[[], Any]]: return { **super().extra_attributes, MyTypedAttribute.string_valued_attribute: lambda: 'overridden attribute value' diff --git a/docs/versionhistory.rst b/docs/versionhistory.rst index 5d39e459..a8853cb7 100644 --- a/docs/versionhistory.rst +++ b/docs/versionhistory.rst @@ -10,6 +10,7 @@ This library adheres to `Semantic Versioning 2.0 `_. - Fixed passing ``total_tokens`` to ``anyio.CapacityLimiter()`` as a keyword argument not working on the ``trio`` backend (`#515 `_) +- Fixed documentation on how to provide your own typed attributes **4.2.0**