-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
copy and paste code from https://gist.github.com/hynek/9eaaaeb659808f…
…3519870dfa16d2b6b2 as advertised in python-attrs/attrs#1265
- Loading branch information
Showing
6 changed files
with
56 additions
and
5 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
attrs==23.2.0 | ||
attrs==24.2.0 | ||
Automat==22.10.0 | ||
characteristic==14.3.0 | ||
constantly==15.1.0 | ||
|
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,47 @@ | ||
from __future__ import annotations | ||
|
||
import attrs | ||
from zope.interface import Interface | ||
|
||
|
||
@attrs.define(repr=False) | ||
class _ProvidesValidator: | ||
interface: type[Interface] = attrs.field() | ||
|
||
def __call__( | ||
self, inst: object, attr: attrs.Attribute, value: object | ||
) -> None: | ||
""" | ||
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} " | ||
f"which {value!r} doesn't." | ||
) | ||
raise TypeError( | ||
msg, | ||
attr, | ||
self.interface, | ||
value, | ||
) | ||
|
||
def __repr__(self) -> str: | ||
return f"<provides validator for interface {self.interface!r}>" | ||
|
||
|
||
def provides(interface: type[Interface]) -> _ProvidesValidator: | ||
""" | ||
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
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