Merge pull request #1326 from tahoe-lafs/4056.provides

attrs "provides()"
This commit is contained in:
meejah 2023-08-09 17:36:58 -06:00 committed by GitHub
commit d0e1d8e598
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 14 deletions

View File

@ -31,9 +31,15 @@ from twisted.internet.defer import (
from twisted.internet.task import (
deferLater,
)
from twisted.internet.protocol import ProcessProtocol # see ticket 4056
from twisted.internet.interfaces import (
IProcessTransport,
IProcessProtocol,
)
from twisted.internet.error import ProcessTerminated
from allmydata.util.attrs_provides import (
provides,
)
from allmydata.node import read_config
from .util import (
_CollectOutputProtocol,
@ -67,16 +73,11 @@ class FlogGatherer(object):
"""
Flog Gatherer process.
"""
# it would be best to use attr.validators.provides() here with the
# corresponding Twisted interface (IProcessTransport,
# IProcessProtocol) but that is deprecated; please replace with
# our own "provides" as part of
# https://tahoe-lafs.org/trac/tahoe-lafs/ticket/4056#ticket
# insisting on a subclass is narrower than necessary
process = attr.ib()
process = attr.ib(
validator=provides(IProcessTransport)
)
protocol = attr.ib(
validator=attr.validators.instance_of(ProcessProtocol)
validator=provides(IProcessProtocol)
)
furl = attr.ib()
@ -156,7 +157,7 @@ class StorageServer(object):
validator=attr.validators.instance_of(TahoeProcess)
)
protocol = attr.ib(
validator=attr.validators.instance_of(ProcessProtocol)
validator=provides(IProcessProtocol)
)
@inlineCallbacks
@ -208,7 +209,7 @@ class Client(object):
validator=attr.validators.instance_of(TahoeProcess)
)
protocol = attr.ib(
validator=attr.validators.instance_of(ProcessProtocol)
validator=provides(IProcessProtocol)
)
request = attr.ib() # original request, for addfinalizer()
@ -336,7 +337,7 @@ class Introducer(object):
validator=attr.validators.instance_of(TahoeProcess)
)
protocol = attr.ib(
validator=attr.validators.instance_of(ProcessProtocol)
validator=provides(IProcessProtocol)
)
furl = attr.ib()

View File

@ -0,0 +1,3 @@
Provide our own copy of attrs' "provides()" validator
This validator is deprecated and slated for removal; that project's suggestion is to copy the code to our project.

View File

@ -88,6 +88,7 @@ from allmydata.util.rrefutil import add_version_to_remote_reference
from allmydata.util.hashutil import permute_server_hash
from allmydata.util.dictutil import BytesKeyDict, UnicodeKeyDict
from allmydata.util.deferredutil import async_to_deferred, race
from allmydata.util.attrs_provides import provides
from allmydata.storage.http_client import (
StorageClient, StorageClientImmutables, StorageClientGeneral,
ClientException as HTTPClientException, StorageClientMutables,
@ -659,7 +660,7 @@ class _FoolscapStorage(object):
permutation_seed = attr.ib()
tubid = attr.ib()
storage_server = attr.ib(validator=attr.validators.provides(IStorageServer))
storage_server = attr.ib(validator=provides(IStorageServer))
_furl = attr.ib()
_short_description = attr.ib()

View File

@ -0,0 +1,50 @@
"""
Utilities related to attrs
Handling for zope.interface is deprecated in attrs so we copy the
relevant support method here since we depend on zope.interface anyway
"""
from attr._make import attrs, attrib
@attrs(repr=False, slots=True, hash=True)
class _ProvidesValidator:
interface = attrib()
def __call__(self, inst, attr, value):
"""
We use a callable class to be able to change the ``__repr__``.
"""
if not self.interface.providedBy(value):
raise TypeError(
"'{name}' must provide {interface!r} which {value!r} "
"doesn't.".format(
name=attr.name, interface=self.interface, value=value
),
attr,
self.interface,
value,
)
def __repr__(self):
return "<provides validator for interface {interface!r}>".format(
interface=self.interface
)
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)