Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

address removal of attrs.validators.provides #764

Merged
merged 3 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements/tox-pin-base.txt
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
Expand Down
47 changes: 47 additions & 0 deletions src/klein/_attrs_zope.py
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)
3 changes: 2 additions & 1 deletion src/klein/_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
from typing import Union

from attr import Factory, attrib, attrs
from attr.validators import instance_of, provides
from attr.validators import instance_of
from hyperlink import DecodedURL
from tubes.itube import IFount
from zope.interface import implementer

from ._attrs_zope import provides
from ._imessage import IHTTPHeaders, IHTTPRequest
from ._message import MessageState, bodyAsBytes, bodyAsFount, validateBody

Expand Down
2 changes: 1 addition & 1 deletion src/klein/_request_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
from typing import cast

from attr import Factory, attrib, attrs
from attr.validators import provides
from hyperlink import DecodedURL
from tubes.itube import IFount
from zope.interface import implementer

from twisted.python.compat import nativeString
from twisted.web.iweb import IRequest

from ._attrs_zope import provides
from ._headers import IHTTPHeaders
from ._headers_compat import HTTPHeadersWrappingHeaders
from ._message import FountAlreadyAccessedError, MessageState
Expand Down
3 changes: 2 additions & 1 deletion src/klein/_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
from typing import Union

from attr import Factory, attrib, attrs
from attr.validators import instance_of, provides
from attr.validators import instance_of
from tubes.itube import IFount
from zope.interface import implementer

from ._attrs_zope import provides
from ._imessage import IHTTPHeaders, IHTTPResponse
from ._message import MessageState, bodyAsBytes, bodyAsFount, validateBody

Expand Down
4 changes: 3 additions & 1 deletion src/klein/_tubes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
from typing import Any, BinaryIO

from attr import attrib, attrs
from attr.validators import instance_of, optional, provides
from attr.validators import instance_of, optional
from tubes.itube import IDrain, IFount, ISegment
from tubes.kit import Pauser, beginFlowingTo
from tubes.undefer import fountToDeferred
from zope.interface import implementer

from twisted.python.failure import Failure

from ._attrs_zope import provides


__all__ = ()

Expand Down
39 changes: 39 additions & 0 deletions src/klein/test/test_attrs_zope.py
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)))
Loading