Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Remove non-None defaults and reinstate deleted code.

Manually reverted commits that caused Sequence and Tuple to return a
non-None default (primarily 67d3da8).
This also gets rid of LeafAttribute than didn't seem to be doing
anything useful anyway.

The default should be None unless the user explicitly sets it to
something. Otherwise, it makes it look there was data present, e.g.
None vs (None, None) for a 2-value Tuple.

Reinstated unexpected keyword check that seems to have been deleted by
commit aa5bb0e.
  • Loading branch information...
commit a9c658515aac4f915edbc066da9e4a38f1ec21cb 1 parent 359297c
@emgee emgee authored
Showing with 31 additions and 55 deletions.
  1. +20 −32 schemaish/attr.py
  2. +11 −23 schemaish/tests/test_attr.py
View
52 schemaish/attr.py
@@ -58,6 +58,7 @@ class Attribute(object):
title = None
description = None
validator = validatish.Always()
+ default = None
def __init__(self, **k):
"""
@@ -78,6 +79,12 @@ def __init__(self, **k):
validator = k.pop('validator', _MISSING)
if validator is not _MISSING:
self.validator = validator
+ default = k.pop('default', _MISSING)
+ if default is not _MISSING:
+ self.default = default
+ if k:
+ raise TypeError("__init__() got unexpected keyword arguments: %r"%list(k))
+
def validate(self, value):
"""
@@ -98,75 +105,68 @@ def __repr__(self):
attributes.append('description=%r' % self.description)
if self.validator:
attributes.append('validator=%r'% self.validator)
- if hasattr(self, 'default') and self.default:
+ if self.default:
attributes.append('default=%r' % self.default)
return 'schemaish.%s(%s)' % (self.__class__.__name__,
', '.join(attributes))
-class LeafAttribute(Attribute):
-
- def __init__(self, **k):
- self.default = k.pop('default', None)
- super(LeafAttribute, self).__init__(**k)
-
-
-
-class String(LeafAttribute):
+class String(Attribute):
"""
A Python unicode instance.
"""
type = 'String'
-class Integer(LeafAttribute):
+class Integer(Attribute):
"""
A Python integer.
"""
type='Integer'
-class Float(LeafAttribute):
+class Float(Attribute):
"""
A Python float.
"""
type='Float'
-class Decimal(LeafAttribute):
+class Decimal(Attribute):
"""
A decimal.Decimal instance.
"""
type='Decimal'
-class Date(LeafAttribute):
+class Date(Attribute):
"""
A datetime.date instance.
"""
type='Date'
-class Time(LeafAttribute):
+class Time(Attribute):
"""
A datetime.time instance.
"""
type='Time'
-class DateTime(LeafAttribute):
+class DateTime(Attribute):
"""
A datetime.datetime instance.
"""
type='DateTime'
-class Boolean(LeafAttribute):
+class Boolean(Attribute):
"""
A Python Boolean instance.
"""
type='Boolean'
+
class Container(Attribute):
type='Container'
@@ -189,7 +189,6 @@ def __init__(self, attr=None, **k):
super(Sequence, self).__init__(**k)
if attr is not None:
self.attr = attr
- self.defaults = k.pop('default', None)
def validate(self, value):
"""
@@ -216,14 +215,11 @@ def validate(self, value):
if error_dict:
raise Invalid(error_dict)
- @property
- def default(self):
- return []
-
def __repr__(self):
return 'schemaish.Sequence(%r)'%self.attr
-class Tuple(Container):
+
+class Tuple(Attribute):
"""
A Python tuple of attributes of specific types.
@@ -254,10 +250,6 @@ def add(self, attr):
else:
self.attrs.append(attr)
- @property
- def default(self):
- return tuple( [getattr(a,'default',None) for a in self.attrs] )
-
def validate(self, value):
"""
Validate the tuple's items and the tuple itself.
@@ -356,10 +348,6 @@ def get(self, name):
return attr
raise KeyError(name)
- @property
- def default(self):
- return dict( [(name, getattr(a,'default',None)) for name, a in self.attrs] )
-
def validate(self, value):
"""
Validate all items in the sequence and then validate the Sequence
@@ -390,7 +378,7 @@ def __repr__(self):
return 'schemaish.Structure(%s)'%(', '.join(attrstrings))
-class File(LeafAttribute):
+class File(Attribute):
"""
A File Object
"""
View
34 schemaish/tests/test_attr.py
@@ -1,9 +1,10 @@
import unittest
+
class TestAttribute(unittest.TestCase):
def _getTargetClass(self):
- from schemaish.attr import LeafAttribute
- return LeafAttribute
+ from schemaish.attr import Attribute
+ return Attribute
def _makeOne(self, **kw):
return self._getTargetClass()(**kw)
@@ -18,6 +19,10 @@ def test_defaults(self):
self.assertEqual(attr.default, None)
assert isinstance(attr.validator, validatish.Always)
+ def test_default(self):
+ attr = self._makeOne(default='foo')
+ self.assertEqual(attr.default, 'foo')
+
def test_subclass(self):
Attribute = self._getTargetClass()
class Something(Attribute):
@@ -33,13 +38,16 @@ class Something(Attribute):
assert attr.description is None
assert attr.validator is None
+ def test_args(self):
+ self.assertRaises(TypeError, self._makeOne, wibble=1)
+
def test__repr__(self):
attr = self._makeOne(title='title',
default=True,
description='description',
validator=required)
begin_expected = (
- "schemaish.LeafAttribute(title='title', "
+ "schemaish.Attribute(title='title', "
"description='description', validator=<function required")
end_expected = 'default=True)'
r = repr(attr)
@@ -162,9 +170,6 @@ def test__repr__(self):
attr = self._makeOne()
self.assertEqual(repr(attr), 'schemaish.Sequence(None)')
- def test_default(self):
- attr = self._makeOne()
- self.assertEqual(attr.default, [])
class TestTuple(unittest.TestCase):
@@ -417,20 +422,3 @@ class DummyAttribute(Attribute):
type = 'Dummy'
return DummyAttribute(*arg, **kw)
-
-class TestDefaults(unittest.TestCase):
-
- def test_defaults(self):
- import schemaish
- address = schemaish.Structure()
- address.add('street', schemaish.String(default='Lidgett Lane'))
- address.add('city', schemaish.String(default='Leeds'))
- schema = schemaish.Structure()
- schema.add('first_names', schemaish.String(default='Tim'))
- schema.add('last_name', schemaish.String(default='Parkin'))
- schema.add('code', schemaish.Tuple( [schemaish.String(default='legs'), schemaish.Integer(default=11)] ))
- schema.add('address',address)
- self.assertEqual(schema.default, {'address': {'city': 'Leeds', 'street': 'Lidgett Lane'},
- 'code': ('legs', 11),
- 'first_names': 'Tim',
- 'last_name': 'Parkin'})
Please sign in to comment.
Something went wrong with that request. Please try again.