Skip to content

Commit

Permalink
Remove non-None defaults and reinstate deleted code.
Browse files Browse the repository at this point in the history
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
atomatt committed Apr 3, 2010
1 parent 359297c commit a9c6585
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 55 deletions.
52 changes: 20 additions & 32 deletions schemaish/attr.py
Expand Up @@ -58,6 +58,7 @@ class Attribute(object):
title = None title = None
description = None description = None
validator = validatish.Always() validator = validatish.Always()
default = None


def __init__(self, **k): def __init__(self, **k):
""" """
Expand All @@ -78,6 +79,12 @@ def __init__(self, **k):
validator = k.pop('validator', _MISSING) validator = k.pop('validator', _MISSING)
if validator is not _MISSING: if validator is not _MISSING:
self.validator = validator 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): def validate(self, value):
""" """
Expand All @@ -98,75 +105,68 @@ def __repr__(self):
attributes.append('description=%r' % self.description) attributes.append('description=%r' % self.description)
if self.validator: if self.validator:
attributes.append('validator=%r'% self.validator) attributes.append('validator=%r'% self.validator)
if hasattr(self, 'default') and self.default: if self.default:
attributes.append('default=%r' % self.default) attributes.append('default=%r' % self.default)
return 'schemaish.%s(%s)' % (self.__class__.__name__, return 'schemaish.%s(%s)' % (self.__class__.__name__,
', '.join(attributes)) ', '.join(attributes))




class LeafAttribute(Attribute): class String(Attribute):

def __init__(self, **k):
self.default = k.pop('default', None)
super(LeafAttribute, self).__init__(**k)



class String(LeafAttribute):
""" """
A Python unicode instance. A Python unicode instance.
""" """
type = 'String' type = 'String'




class Integer(LeafAttribute): class Integer(Attribute):
""" """
A Python integer. A Python integer.
""" """
type='Integer' type='Integer'




class Float(LeafAttribute): class Float(Attribute):
""" """
A Python float. A Python float.
""" """
type='Float' type='Float'




class Decimal(LeafAttribute): class Decimal(Attribute):
""" """
A decimal.Decimal instance. A decimal.Decimal instance.
""" """
type='Decimal' type='Decimal'




class Date(LeafAttribute): class Date(Attribute):
""" """
A datetime.date instance. A datetime.date instance.
""" """
type='Date' type='Date'




class Time(LeafAttribute): class Time(Attribute):
""" """
A datetime.time instance. A datetime.time instance.
""" """
type='Time' type='Time'




class DateTime(LeafAttribute): class DateTime(Attribute):
""" """
A datetime.datetime instance. A datetime.datetime instance.
""" """
type='DateTime' type='DateTime'




class Boolean(LeafAttribute): class Boolean(Attribute):
""" """
A Python Boolean instance. A Python Boolean instance.
""" """
type='Boolean' type='Boolean'



class Container(Attribute): class Container(Attribute):
type='Container' type='Container'


Expand All @@ -189,7 +189,6 @@ def __init__(self, attr=None, **k):
super(Sequence, self).__init__(**k) super(Sequence, self).__init__(**k)
if attr is not None: if attr is not None:
self.attr = attr self.attr = attr
self.defaults = k.pop('default', None)


def validate(self, value): def validate(self, value):
""" """
Expand All @@ -216,14 +215,11 @@ def validate(self, value):
if error_dict: if error_dict:
raise Invalid(error_dict) raise Invalid(error_dict)


@property
def default(self):
return []

def __repr__(self): def __repr__(self):
return 'schemaish.Sequence(%r)'%self.attr return 'schemaish.Sequence(%r)'%self.attr


class Tuple(Container):
class Tuple(Attribute):
""" """
A Python tuple of attributes of specific types. A Python tuple of attributes of specific types.
Expand Down Expand Up @@ -254,10 +250,6 @@ def add(self, attr):
else: else:
self.attrs.append(attr) self.attrs.append(attr)


@property
def default(self):
return tuple( [getattr(a,'default',None) for a in self.attrs] )

def validate(self, value): def validate(self, value):
""" """
Validate the tuple's items and the tuple itself. Validate the tuple's items and the tuple itself.
Expand Down Expand Up @@ -356,10 +348,6 @@ def get(self, name):
return attr return attr
raise KeyError(name) raise KeyError(name)


@property
def default(self):
return dict( [(name, getattr(a,'default',None)) for name, a in self.attrs] )

def validate(self, value): def validate(self, value):
""" """
Validate all items in the sequence and then validate the Sequence Validate all items in the sequence and then validate the Sequence
Expand Down Expand Up @@ -390,7 +378,7 @@ def __repr__(self):
return 'schemaish.Structure(%s)'%(', '.join(attrstrings)) return 'schemaish.Structure(%s)'%(', '.join(attrstrings))




class File(LeafAttribute): class File(Attribute):
""" """
A File Object A File Object
""" """
Expand Down
34 changes: 11 additions & 23 deletions schemaish/tests/test_attr.py
@@ -1,9 +1,10 @@
import unittest import unittest



class TestAttribute(unittest.TestCase): class TestAttribute(unittest.TestCase):
def _getTargetClass(self): def _getTargetClass(self):
from schemaish.attr import LeafAttribute from schemaish.attr import Attribute
return LeafAttribute return Attribute


def _makeOne(self, **kw): def _makeOne(self, **kw):
return self._getTargetClass()(**kw) return self._getTargetClass()(**kw)
Expand All @@ -18,6 +19,10 @@ def test_defaults(self):
self.assertEqual(attr.default, None) self.assertEqual(attr.default, None)
assert isinstance(attr.validator, validatish.Always) assert isinstance(attr.validator, validatish.Always)


def test_default(self):
attr = self._makeOne(default='foo')
self.assertEqual(attr.default, 'foo')

def test_subclass(self): def test_subclass(self):
Attribute = self._getTargetClass() Attribute = self._getTargetClass()
class Something(Attribute): class Something(Attribute):
Expand All @@ -33,13 +38,16 @@ class Something(Attribute):
assert attr.description is None assert attr.description is None
assert attr.validator is None assert attr.validator is None


def test_args(self):
self.assertRaises(TypeError, self._makeOne, wibble=1)

def test__repr__(self): def test__repr__(self):
attr = self._makeOne(title='title', attr = self._makeOne(title='title',
default=True, default=True,
description='description', description='description',
validator=required) validator=required)
begin_expected = ( begin_expected = (
"schemaish.LeafAttribute(title='title', " "schemaish.Attribute(title='title', "
"description='description', validator=<function required") "description='description', validator=<function required")
end_expected = 'default=True)' end_expected = 'default=True)'
r = repr(attr) r = repr(attr)
Expand Down Expand Up @@ -162,9 +170,6 @@ def test__repr__(self):
attr = self._makeOne() attr = self._makeOne()
self.assertEqual(repr(attr), 'schemaish.Sequence(None)') self.assertEqual(repr(attr), 'schemaish.Sequence(None)')


def test_default(self):
attr = self._makeOne()
self.assertEqual(attr.default, [])


class TestTuple(unittest.TestCase): class TestTuple(unittest.TestCase):


Expand Down Expand Up @@ -417,20 +422,3 @@ class DummyAttribute(Attribute):
type = 'Dummy' type = 'Dummy'
return DummyAttribute(*arg, **kw) 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'})

0 comments on commit a9c6585

Please sign in to comment.