forked from PrivateStorageio/ZKAPAuthorizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
attrs 24 dropped 'provides()' from its API
... and provided this copypasta for people who rely on it, see python-attrs/attrs#1265
- Loading branch information
1 parent
20ab993
commit a85b3df
Showing
3 changed files
with
42 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters