Skip to content

Commit

Permalink
Fixed documentation on providing custom typed attributes (#683)
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Feb 3, 2024
1 parent 8b8806c commit 137de70
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
16 changes: 10 additions & 6 deletions docs/typedattrs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
Expand Down
1 change: 1 addition & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.
- Fixed ``Process.stdin.aclose()``, ``Process.stdout.aclose()``, and
``Process.stderr.aclose()`` not including a checkpoint on asyncio (PR by Ganden
Schaffner)
- Fixed documentation on how to provide your own typed attributes

**4.2.0**

Expand Down

0 comments on commit 137de70

Please sign in to comment.