Add sample() method to ArrayType and DictType

Update ArrayType and DictType so they have sample()
methods for the sphinxext code to use when documenting
nested types.
This commit is contained in:
Doug Hellmann 2012-12-07 16:12:48 -05:00
parent 23a276058d
commit e1f82b01ce
2 changed files with 20 additions and 0 deletions

View File

@ -355,3 +355,15 @@ class TestTypes(unittest.TestCase):
def test_array_eq(self):
l = [types.ArrayType(str)]
assert types.ArrayType(str) in l
def test_array_sample(self):
s = types.ArrayType(str).sample()
assert isinstance(s, list)
assert s
assert s[0] == ''
def test_dict_sample(self):
s = types.DictType(str, str).sample()
assert isinstance(s, dict)
assert s
assert s == {'': ''}

View File

@ -34,6 +34,9 @@ class ArrayType(object):
return isinstance(other, ArrayType) \
and self.item_type == other.item_type
def sample(self):
return [getattr(self.item_type, 'sample', self.item_type)()]
@property
def item_type(self):
if isinstance(self._item_type, weakref.ref):
@ -67,6 +70,11 @@ class DictType(object):
def __hash__(self):
return hash((self.key_type, self.value_type))
def sample(self):
key = getattr(self.key_type, 'sample', self.key_type)()
value = getattr(self.value_type, 'sample', self.value_type)()
return {key: value}
@property
def value_type(self):
if isinstance(self._value_type, weakref.ref):