-
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.
Merge pull request #764 from twisted/attrs-provides-deprecation
address removal of `attrs.validators.provides`
- Loading branch information
Showing
7 changed files
with
95 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
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 @@ | ||
from __future__ import annotations | ||
|
||
import attrs | ||
from zope.interface import Interface, implementer | ||
|
||
from twisted.trial.unittest import SynchronousTestCase | ||
|
||
from .._attrs_zope import provides | ||
|
||
|
||
class IWhatever(Interface): | ||
... | ||
|
||
|
||
@implementer(IWhatever) | ||
class YesWhatever: | ||
... | ||
|
||
|
||
class NoWhatever: | ||
... | ||
|
||
|
||
@attrs.define() | ||
class WhateverContainer: | ||
whatever: object = attrs.field(validator=provides(IWhatever)) | ||
|
||
|
||
class ProvidesTestCase(SynchronousTestCase): | ||
def test_yes(self) -> None: | ||
WhateverContainer(YesWhatever()) | ||
|
||
def test_no(self) -> None: | ||
with self.assertRaises(TypeError): | ||
WhateverContainer(NoWhatever()) | ||
|
||
def test_repr(self) -> None: | ||
self.assertIn("provides validator for", repr(provides(IWhatever))) | ||
self.assertIn("IWhatever", repr(provides(IWhatever))) |