[Checkins] SVN: zope.schema/trunk/ fix __cmp__ method in ValidationError

Roger Ineichen roger at projekt01.ch
Thu Dec 18 18:18:11 EST 2008


Log message for revision 94176:
  fix __cmp__ method in ValidationError

Changed:
  U   zope.schema/trunk/CHANGES.txt
  U   zope.schema/trunk/src/zope/schema/_bootstrapinterfaces.py
  U   zope.schema/trunk/src/zope/schema/validation.txt

-=-
Modified: zope.schema/trunk/CHANGES.txt
===================================================================
--- zope.schema/trunk/CHANGES.txt	2008-12-18 19:08:10 UTC (rev 94175)
+++ zope.schema/trunk/CHANGES.txt	2008-12-18 23:18:10 UTC (rev 94176)
@@ -2,10 +2,11 @@
  Changes
 =========
 
-unreleased
-----------
+3.5.1dev (unreleased)
+---------------------
 
-- 
+- Fix __cmp__ method in ValidationError. Show some side effects based on the
+  existing __cmp__ implementation. See validation.txt
 
 
 3.5.0a2 (2008/12/11)

Modified: zope.schema/trunk/src/zope/schema/_bootstrapinterfaces.py
===================================================================
--- zope.schema/trunk/src/zope/schema/_bootstrapinterfaces.py	2008-12-18 19:08:10 UTC (rev 94175)
+++ zope.schema/trunk/src/zope/schema/_bootstrapinterfaces.py	2008-12-18 23:18:10 UTC (rev 94176)
@@ -34,6 +34,8 @@
         return self.__class__.__doc__
 
     def __cmp__(self, other):
+        if not hasattr(other, 'args'):
+            return -1
         return cmp(self.args, other.args)
 
     def __repr__(self):

Modified: zope.schema/trunk/src/zope/schema/validation.txt
===================================================================
--- zope.schema/trunk/src/zope/schema/validation.txt	2008-12-18 19:08:10 UTC (rev 94175)
+++ zope.schema/trunk/src/zope/schema/validation.txt	2008-12-18 23:18:10 UTC (rev 94176)
@@ -1,8 +1,7 @@
-================
-Schema Valiation
-================
+=================
+Schema Validation
+=================
 
-
 There are two helper methods to verify schemas and interfaces:
 
 getValidationErrors
@@ -15,79 +14,133 @@
 
 Create an interface to validate against:
 
->>> import zope.interface
->>> import zope.schema
->>> class ITwoInts(zope.interface.Interface):
-...     a = zope.schema.Int(max=10)
-...     b = zope.schema.Int(min=5)
-...
-...     @zope.interface.invariant
-...     def a_greater_b(obj):
-...         print "Checking if a > b"
-...         if obj.a <= obj.b:
-...             raise zope.interface.Invalid("%s<=%s" % (obj.a, obj.b))
-...     
+  >>> import zope.interface
+  >>> import zope.schema
+  >>> class ITwoInts(zope.interface.Interface):
+  ...     a = zope.schema.Int(max=10)
+  ...     b = zope.schema.Int(min=5)
+  ...
+  ...     @zope.interface.invariant
+  ...     def a_greater_b(obj):
+  ...         print "Checking if a > b"
+  ...         if obj.a <= obj.b:
+  ...             raise zope.interface.Invalid("%s<=%s" % (obj.a, obj.b))
+  ...     
 
 Create a silly model:
 
->>> class TwoInts(object):
-...     pass
+  >>> class TwoInts(object):
+  ...     pass
 
-
 Create an instance of TwoInts but do not set attributes. We get two errors:
 
->>> ti = TwoInts()
->>> zope.schema.getValidationErrors(ITwoInts, ti)
-[('a', 'TwoInts' object has no attribute 'a'),
- ('b', 'TwoInts' object has no attribute 'b')]
+  >>> ti = TwoInts()
+  >>> zope.schema.getValidationErrors(ITwoInts, ti)
+  [('a', 'TwoInts' object has no attribute 'a'),
+   ('b', 'TwoInts' object has no attribute 'b')]
 
 The `getSchemaValidationErrors` function returns the same result:
 
->>> zope.schema.getSchemaValidationErrors(ITwoInts, ti)
-[('a', 'TwoInts' object has no attribute 'a'),
- ('b', 'TwoInts' object has no attribute 'b')]
+  >>> zope.schema.getSchemaValidationErrors(ITwoInts, ti)
+  [('a', 'TwoInts' object has no attribute 'a'),
+   ('b', 'TwoInts' object has no attribute 'b')]
 
 Note that see no error from the invariant because the invariants are not
 vaildated if there are other schema errors.
 
 When we set an invalid value for `a` we still get the same error for `b`:
 
->>> ti.a = 11
->>> errors = zope.schema.getValidationErrors(ITwoInts, ti)
->>> errors
-[('a', 11 10),
- ('b', 'TwoInts' object has no attribute 'b')]
->>> errors[0][1].doc()
-u'Value is too big'
+  >>> ti.a = 11
+  >>> errors = zope.schema.getValidationErrors(ITwoInts, ti)
+  >>> errors
+  [('a', 11 10),
+   ('b', 'TwoInts' object has no attribute 'b')]
+  >>> errors[0][1].doc()
+  u'Value is too big'
 
 
 After setting a valid value for `a` there is only the error for the missing `b`
 left:
 
->>> ti.a = 8
->>> zope.schema.getValidationErrors(ITwoInts, ti)
-[('b', 'TwoInts' object has no attribute 'b')]
+  >>> ti.a = 8
+  >>> zope.schema.getValidationErrors(ITwoInts, ti)
+  [('b', 'TwoInts' object has no attribute 'b')]
 
 
 After setting valid value for `b` the schema is valid so the invariants are
 checked. As `b>a` the invariant fails:
 
->>> ti.b = 10
->>> errors = zope.schema.getValidationErrors(ITwoInts, ti)
-Checking if a > b
->>> errors
-[(None, <zope.interface.exceptions.Invalid instance at 0x...>)]
+  >>> ti.b = 10
+  >>> errors = zope.schema.getValidationErrors(ITwoInts, ti)
+  Checking if a > b
+  >>> errors
+  [(None, <zope.interface.exceptions.Invalid instance at 0x...>)]
 
 
 When using `getSchemaValidationErrors` we do not get an error any more:
 
->>> zope.schema.getSchemaValidationErrors(ITwoInts, ti)
-[]
+  >>> zope.schema.getSchemaValidationErrors(ITwoInts, ti)
+  []
 
 
 Set `b=5` so everything is fine:
 
->>> ti.b = 5
->>> zope.schema.getValidationErrors(ITwoInts, ti)
-Checking if a > b
-[]
+  >>> ti.b = 5
+  >>> zope.schema.getValidationErrors(ITwoInts, ti)
+  Checking if a > b
+  []
+
+
+Compare ValidationError
+-----------------------
+
+There was an issue with compare validation error with somthing else then an
+exceptions. Let's test if we can compare ValidationErrors with different things
+
+  >>> from zope.schema._bootstrapinterfaces import ValidationError
+  >>> v1 = ValidationError('one')
+  >>> v2 = ValidationError('one')
+  >>> v3 = ValidationError('another one')
+
+A ValidationError with the same arguments compares:
+
+  >>> v1 == v2
+  True
+
+but not with an error with different arguments:
+
+  >>> v1 == v3
+  False
+
+We can also compare validation erros with other things then errors. This 
+was running into an AttributeError in previous versions of zope.schema. e.g.
+AttributeError: 'NoneType' object has no attribute 'args'
+
+  >>> v1 == None
+  False
+
+  >>> v1 == object()
+  False
+
+  >>> v1 == False
+  False
+
+  >>> v1 == True
+  False
+
+  >>> v1 == 0
+  False
+
+  >>> v1 == 1
+  False
+
+  >>> v1 == int
+  False
+
+If we compare a ValidationError with another validation error based class,
+we will get the following result:
+
+  >>> from zope.schema._bootstrapinterfaces import RequiredMissing
+  >>> r1 = RequiredMissing('one')
+  >>> v1 == r1
+  True



More information about the Checkins mailing list