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__


  File "c:\python24\lib\site-packages\zope\schema\_bootstrapfields.py", line 138, in validate


  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




Zope3-dev mailing list
Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com

Reply via email to