2015-02-20 15:34:21 +00:00
|
|
|
"""
|
|
|
|
Commonly useful validators.
|
|
|
|
"""
|
|
|
|
|
2015-01-29 11:20:17 +00:00
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
2015-01-29 18:39:49 +00:00
|
|
|
from ._make import attr, attributes
|
2015-01-29 11:20:17 +00:00
|
|
|
|
2015-01-29 15:24:49 +00:00
|
|
|
|
2015-02-20 12:29:47 +00:00
|
|
|
@attributes(repr=False)
|
2015-01-29 15:24:49 +00:00
|
|
|
class _InstanceOfValidator(object):
|
2015-03-23 09:42:42 +00:00
|
|
|
type = attr()
|
2015-01-29 11:20:17 +00:00
|
|
|
|
2015-02-09 12:16:56 +00:00
|
|
|
def __call__(self, inst, attr, value):
|
2015-01-29 15:24:49 +00:00
|
|
|
"""
|
|
|
|
We use a callable class to be able to change the ``__repr__``.
|
|
|
|
"""
|
2015-03-23 09:42:42 +00:00
|
|
|
if not isinstance(value, self.type):
|
2015-01-29 11:20:17 +00:00
|
|
|
raise TypeError(
|
|
|
|
"'{name}' must be {type!r} (got {value!r} that is a "
|
|
|
|
"{actual!r})."
|
2015-03-23 09:42:42 +00:00
|
|
|
.format(name=attr.name, type=self.type,
|
2015-01-29 11:20:17 +00:00
|
|
|
actual=value.__class__, value=value),
|
2015-03-23 09:42:42 +00:00
|
|
|
attr, self.type, value,
|
2015-01-29 11:20:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return (
|
|
|
|
"<instance_of validator for type {type!r}>"
|
2015-03-23 09:42:42 +00:00
|
|
|
.format(type=self.type)
|
2015-01-29 11:20:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-03-23 09:42:42 +00:00
|
|
|
def instance_of(type):
|
2015-01-29 11:20:17 +00:00
|
|
|
"""
|
|
|
|
A validator that raises a :exc:`TypeError` if the initializer is called
|
|
|
|
with a wrong type for this particular attribute (checks are perfomed using
|
|
|
|
:func:`isinstance`).
|
|
|
|
|
2015-03-23 09:42:42 +00:00
|
|
|
:param type: The type to check for.
|
|
|
|
:type type: type
|
2015-01-29 11:20:17 +00:00
|
|
|
|
|
|
|
The :exc:`TypeError` is raised with a human readable error message, the
|
|
|
|
attribute (of type :class:`attr.Attribute`), the expected type and the
|
|
|
|
value it got.
|
|
|
|
"""
|
2015-03-23 09:42:42 +00:00
|
|
|
return _InstanceOfValidator(type)
|
2015-01-29 18:04:23 +00:00
|
|
|
|
|
|
|
|
2015-02-20 12:29:47 +00:00
|
|
|
@attributes(repr=False)
|
2015-01-29 18:04:23 +00:00
|
|
|
class _ProvidesValidator(object):
|
2015-01-29 18:39:49 +00:00
|
|
|
interface = attr()
|
2015-01-29 18:04:23 +00:00
|
|
|
|
2015-02-09 12:16:56 +00:00
|
|
|
def __call__(self, inst, attr, value):
|
2015-01-29 18:04:23 +00:00
|
|
|
"""
|
|
|
|
We use a callable class to be able to change the ``__repr__``.
|
|
|
|
"""
|
|
|
|
if not self.interface.providedBy(value):
|
|
|
|
raise TypeError(
|
|
|
|
"'{name}' must provide {interface!r} which {value!r} "
|
|
|
|
"doesn't."
|
|
|
|
.format(name=attr.name, interface=self.interface, value=value),
|
|
|
|
attr, self.interface, value,
|
|
|
|
)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return (
|
|
|
|
"<provides validator for interface {interface!r}>"
|
|
|
|
.format(interface=self.interface)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def provides(interface):
|
|
|
|
"""
|
|
|
|
A validator that raises a :exc:`TypeError` if the initializer is called
|
|
|
|
with an object that does not provide the requested *interface* (checks are
|
2015-07-23 15:08:21 +00:00
|
|
|
performed using ``interface.providedBy(value)`` (see `zope.interface
|
2015-01-29 18:04:23 +00:00
|
|
|
<http://docs.zope.org/zope.interface/>`_).
|
|
|
|
|
2015-03-23 09:42:42 +00:00
|
|
|
:param interface: The interface to check for.
|
|
|
|
:type interface: zope.interface.Interface
|
2015-01-29 18:04:23 +00:00
|
|
|
|
|
|
|
The :exc:`TypeError` is raised with a human readable error message, the
|
|
|
|
attribute (of type :class:`attr.Attribute`), the expected interface, and
|
|
|
|
the value it got.
|
|
|
|
"""
|
|
|
|
return _ProvidesValidator(interface)
|
Support optional values through a new validator.
Unfortunately, I'm unable to run tests locally b/c of severe dependency
hell. I am unable to get tox, detox, python setup.py test, and other
means of running tests to work at all. So, since I'm blocked on a
project by the lack of optional validator support, I am going to close
my eyes, look away, and pull the trigger, and hope I hit the target.
If someone who has a working Python configuration can please be kind
enough to let me know if my changes fail any tests, I'd be very
appreciative.
Even better, I'd love to know why a stock Python distribution with
dependencies installed is incapable of running the tests for attrs.
However, this isn't the right forum to answer that question.
(This PR has the 17+ debugging commits cleaned up.)
2015-07-07 22:27:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
@attributes(repr=False)
|
|
|
|
class _OptionalValidator(object):
|
2015-07-09 23:14:32 +00:00
|
|
|
validator = attr()
|
Support optional values through a new validator.
Unfortunately, I'm unable to run tests locally b/c of severe dependency
hell. I am unable to get tox, detox, python setup.py test, and other
means of running tests to work at all. So, since I'm blocked on a
project by the lack of optional validator support, I am going to close
my eyes, look away, and pull the trigger, and hope I hit the target.
If someone who has a working Python configuration can please be kind
enough to let me know if my changes fail any tests, I'd be very
appreciative.
Even better, I'd love to know why a stock Python distribution with
dependencies installed is incapable of running the tests for attrs.
However, this isn't the right forum to answer that question.
(This PR has the 17+ debugging commits cleaned up.)
2015-07-07 22:27:03 +00:00
|
|
|
|
|
|
|
def __call__(self, inst, attr, value):
|
|
|
|
if value is None:
|
|
|
|
return
|
2015-07-09 23:14:32 +00:00
|
|
|
return self.validator(inst, attr, value)
|
Support optional values through a new validator.
Unfortunately, I'm unable to run tests locally b/c of severe dependency
hell. I am unable to get tox, detox, python setup.py test, and other
means of running tests to work at all. So, since I'm blocked on a
project by the lack of optional validator support, I am going to close
my eyes, look away, and pull the trigger, and hope I hit the target.
If someone who has a working Python configuration can please be kind
enough to let me know if my changes fail any tests, I'd be very
appreciative.
Even better, I'd love to know why a stock Python distribution with
dependencies installed is incapable of running the tests for attrs.
However, this isn't the right forum to answer that question.
(This PR has the 17+ debugging commits cleaned up.)
2015-07-07 22:27:03 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return (
|
|
|
|
"<optional validator for {type} or None>"
|
2015-07-09 23:14:32 +00:00
|
|
|
.format(type=repr(self.validator))
|
Support optional values through a new validator.
Unfortunately, I'm unable to run tests locally b/c of severe dependency
hell. I am unable to get tox, detox, python setup.py test, and other
means of running tests to work at all. So, since I'm blocked on a
project by the lack of optional validator support, I am going to close
my eyes, look away, and pull the trigger, and hope I hit the target.
If someone who has a working Python configuration can please be kind
enough to let me know if my changes fail any tests, I'd be very
appreciative.
Even better, I'd love to know why a stock Python distribution with
dependencies installed is incapable of running the tests for attrs.
However, this isn't the right forum to answer that question.
(This PR has the 17+ debugging commits cleaned up.)
2015-07-07 22:27:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-07-09 23:14:32 +00:00
|
|
|
def optional(validator):
|
Support optional values through a new validator.
Unfortunately, I'm unable to run tests locally b/c of severe dependency
hell. I am unable to get tox, detox, python setup.py test, and other
means of running tests to work at all. So, since I'm blocked on a
project by the lack of optional validator support, I am going to close
my eyes, look away, and pull the trigger, and hope I hit the target.
If someone who has a working Python configuration can please be kind
enough to let me know if my changes fail any tests, I'd be very
appreciative.
Even better, I'd love to know why a stock Python distribution with
dependencies installed is incapable of running the tests for attrs.
However, this isn't the right forum to answer that question.
(This PR has the 17+ debugging commits cleaned up.)
2015-07-07 22:27:03 +00:00
|
|
|
"""
|
2015-07-10 17:11:11 +00:00
|
|
|
A validator that makes an attribute optional. An optional attribute is one
|
2015-07-26 10:32:02 +00:00
|
|
|
which can be set to ``None`` in addition to satisfying the requirements of
|
|
|
|
the sub-validator.
|
Support optional values through a new validator.
Unfortunately, I'm unable to run tests locally b/c of severe dependency
hell. I am unable to get tox, detox, python setup.py test, and other
means of running tests to work at all. So, since I'm blocked on a
project by the lack of optional validator support, I am going to close
my eyes, look away, and pull the trigger, and hope I hit the target.
If someone who has a working Python configuration can please be kind
enough to let me know if my changes fail any tests, I'd be very
appreciative.
Even better, I'd love to know why a stock Python distribution with
dependencies installed is incapable of running the tests for attrs.
However, this isn't the right forum to answer that question.
(This PR has the 17+ debugging commits cleaned up.)
2015-07-07 22:27:03 +00:00
|
|
|
|
2015-07-26 10:32:02 +00:00
|
|
|
:param validator: A validator that is used for non-``None`` values.
|
Support optional values through a new validator.
Unfortunately, I'm unable to run tests locally b/c of severe dependency
hell. I am unable to get tox, detox, python setup.py test, and other
means of running tests to work at all. So, since I'm blocked on a
project by the lack of optional validator support, I am going to close
my eyes, look away, and pull the trigger, and hope I hit the target.
If someone who has a working Python configuration can please be kind
enough to let me know if my changes fail any tests, I'd be very
appreciative.
Even better, I'd love to know why a stock Python distribution with
dependencies installed is incapable of running the tests for attrs.
However, this isn't the right forum to answer that question.
(This PR has the 17+ debugging commits cleaned up.)
2015-07-07 22:27:03 +00:00
|
|
|
"""
|
2015-07-09 23:14:32 +00:00
|
|
|
return _OptionalValidator(validator)
|