[Zope3-dev] should schema.Date accectp datetime values

Bernd Dorn zope-mailinglist at mopa.at
Tue Nov 14 17:34:44 EST 2006


hi all

i just saw that zope.schema.Date objects accept datetime values  
because the default validate implementation uses isinstance to check  
the value. this is a problem imho, because datetime is a subclass of  
date but instances can't be compmared to each other.

so what are you guys thinking about handling this case in schema.Date

any problems with this? and if no, is it ok to backport it to 3.3

i also tried using type instead of instance in the base  
implementation of the validate method, but this affects i18n  
messages, because they are not unicode.

here my diffs,  regards Bernd

Index: tests/test_date.py
===================================================================
--- tests/test_date.py  (revision 70847)
+++ tests/test_date.py  (working copy)
@@ -17,7 +17,7 @@
"""
from unittest import main, makeSuite
from zope.schema import Date
-from zope.schema.interfaces import RequiredMissing, InvalidValue
+from zope.schema.interfaces import RequiredMissing, InvalidValue,  
WrongType
from zope.schema.interfaces import TooSmall, TooBig
from zope.schema.tests.test_field import FieldTestBase
from datetime import datetime, date
@@ -37,6 +37,7 @@
                                      readonly=False, required=False)
          field.validate(None)
          field.validate(datetime.now().date())
+        self.assertRaises(WrongType, field.validate, datetime.now())
      def testValidateRequired(self):
          field = self._Field_Factory(title=u'Date field',  
description=u'',
Index: _field.py
===================================================================
--- _field.py   (revision 70847)
+++ _field.py   (working copy)
@@ -205,6 +205,11 @@
      __doc__ = IDate.__doc__
      implements(IDate)
      _type = date
+
+    def _validate(self, value):
+        super(Date, self)._validate(value)
+        if isinstance(value, datetime):
+            raise WrongType(value, self._type)






More information about the Zope3-dev mailing list