Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed documentation on providing custom typed attributes #683

Merged
merged 2 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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