Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
glyph committed Aug 11, 2024
1 parent 1fa3d70 commit bfd2592
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 5 deletions.
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 = (

Check warning on line 18 in src/klein/_attrs_zope.py

View check run for this annotation

Codecov / codecov/patch

src/klein/_attrs_zope.py#L18

Added line #L18 was not covered by tests
f"'{attr.name}' must provide {self.interface!r} "
f"which {value!r} doesn't."
)
raise TypeError(

Check warning on line 22 in src/klein/_attrs_zope.py

View check run for this annotation

Codecov / codecov/patch

src/klein/_attrs_zope.py#L22

Added line #L22 was not covered by tests
msg,
attr,
self.interface,
value,
)

def __repr__(self) -> str:
return f"<provides validator for interface {self.interface!r}>"

Check warning on line 30 in src/klein/_attrs_zope.py

View check run for this annotation

Codecov / codecov/patch

src/klein/_attrs_zope.py#L30

Added line #L30 was not covered by tests


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

0 comments on commit bfd2592

Please sign in to comment.