Skip to content

Commit

Permalink
attrs 24 dropped 'provides()' from its API
Browse files Browse the repository at this point in the history
... and provided this copypasta for people who
rely on it, see python-attrs/attrs#1265
  • Loading branch information
hacklschorsch committed Nov 4, 2024
1 parent 20ab993 commit a85b3df
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
39 changes: 39 additions & 0 deletions src/_zkapauthorizer/_attrs_zope.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import attrs

@attrs.define(repr=False)
class _ProvidesValidator:
interface = attrs.field()

def __call__(self, inst, attr, value):
"""
We use a callable class to be able to change the ``__repr__``.
"""
if not self.interface.providedBy(value):
msg = f"'{attr.name}' must provide {self.interface!r} which {value!r} doesn't."
raise TypeError(
msg,
attr,
self.interface,
value,
)

def __repr__(self):
return f"<provides validator for interface {self.interface!r}>"


def provides(interface):
"""
A validator that raises a `TypeError` if the initializer is called
with an object that does not provide the requested *interface* (checks are
performed using ``interface.providedBy(value)`` (see `zope.interface
<https://zopeinterface.readthedocs.io/en/latest/>`_).
:param interface: The interface to check for.
:type interface: ``zope.interface.Interface``
:raises TypeError: With a human readable error message, the attribute
(of type `attrs.Attribute`), the expected interface, and the
value it got.
"""
return _ProvidesValidator(interface)

2 changes: 1 addition & 1 deletion src/_zkapauthorizer/_storage_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from typing import Any, Awaitable, Callable, Optional, Protocol, TypeVar

from allmydata.interfaces import IStorageServer
from attr.validators import provides
from attrs import Factory, define, field
from foolscap.ipb import IRemoteReference
from foolscap.referenceable import RemoteReference
Expand All @@ -51,6 +50,7 @@
slot_testv_and_readv_and_writev_message,
)
from .validators import positive_integer
from ._attrs_zope import provides

_T = TypeVar("_T")
_P = ParamSpec("_P")
Expand Down
3 changes: 2 additions & 1 deletion src/_zkapauthorizer/_storage_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
from allmydata.storage.server import StorageServer
from allmydata.storage.shares import get_share_file
from allmydata.util.base32 import b2a
from attr.validators import instance_of, provides
from attr.validators import instance_of
from attrs import field, frozen
from challenge_bypass_ristretto import (
PublicKey,
Expand Down Expand Up @@ -94,6 +94,7 @@
slot_testv_and_readv_and_writev_message,
)
from .validators import positive_integer
from ._attrs_zope import provides

# See allmydata/storage/mutable.py
SLOT_HEADER_SIZE = 468
Expand Down

0 comments on commit a85b3df

Please sign in to comment.