[Zope3-dev] zope.schema Field... constraints: should they really
constrain each other?
Shaun Cutts
shaun at cuttshome.net
Thu Jan 19 04:05:21 EST 2006
I am a new user of the component system (currently using it with
Twisted, not using zope itself). I'm not sure I understand all the
philosophy behind its design. Nevertheless, I believe the following
behavior is not the best:
>>> a = Int( missing_value = -1 )
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "c:\python24\lib\site-packages\zope\schema\_bootstrapfields.py",
line 351, in __init__
super(Int, self).__init__(*args, **kw)
File "c:\python24\lib\site-packages\zope\schema\_bootstrapfields.py",
line 231, in __init__
self.min = min
File "c:\python24\lib\site-packages\zope\schema\_bootstrapfields.py",
line 42, in __set__
inst.validate(value)
File "c:\python24\lib\site-packages\zope\schema\_bootstrapfields.py",
line 138, in validate
self._validate(value)
File "c:\python24\lib\site-packages\zope\schema\_bootstrapfields.py",
line 240, in _validate
super(Orderable, self)._validate(value)
File "c:\python24\lib\site-packages\zope\schema\_bootstrapfields.py",
line 165, in _validate
raise WrongType(value, self._type)
zope.schema._bootstrapinterfaces.WrongType: (None, (<type 'int'>, <type
'long'>))
What is happening is that "min" (and max) are ValidatedProperties of
Orderable. When a "missing_value" is supplied, min = None (by default)
is checked and fails validation.
One solution is to change "None" into missing_value in the Orderable
__init__. More exhaustively, min, max, and default should have defaults
as "__missing_value_marker" in the Orderable __init__ (using the trick
in Field.__init__), which would then be converted to the actual
missing_value, if specified, or None, if not.
This gives me the following __init__ in
zope.schema_bootstrapfields.Orderable:
__missing_value_marker = object()
def __init__(self, min=__missing_value_marker,
max=__missing_value_marker,
default=__missing_value_marker, **kw):
# Set min and max to None so that we can validate if
# one of the super methods invoke validation.
self.min = None
self.max = None
super(Orderable, self).__init__(**kw)
# make sure that unspecified min, max, default get correct
missing value
try: missing = self.missing_value
except AttributeError: missing = None
if min is self.__missing_value_marker: min = missing
if max is self.__missing_value_marker: max = missing
if default is self.__missing_value_marker: default = missing
# Now really set min and max
self.min = min
self.max = max
# We've taken over setting default so it can be limited by min
# and max.
self.default = default
(But I don't know if constraints should (always?) apply to each other.
For instance, if "min" were actually defined as the largest value
smaller than the range, it wouldn't validate against itself.)
- Shaun
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.zope.org/pipermail/zope3-dev/attachments/20060119/bbabf789/attachment-0001.htm
More information about the Zope3-dev
mailing list