Skip to content

Commit

Permalink
Fix issue of creating single option group as class attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelchen committed Jun 3, 2019
1 parent 1a5f341 commit 15ffeb9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
4 changes: 4 additions & 0 deletions RELEASENOTE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

# v1.1.5

* Fix issue of creating single option group as class attribute

# v1.1.4

* Fix option not found issue when creating option group as class attribute (class attributes creation is not in order)
Expand Down
14 changes: 11 additions & 3 deletions optenum/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@
class OptionGroup(list):

def __init__(self, *args):
# to ensure args are in list, consider the following
#
# class Foo(Options):
# A = 1
# B = 2, 'B is 2'
#
# G1 = OptionGroup(A) # should get args [ 1 ]
# G2 = OptionGroup(B) # should get args [ (2, 'B is 2') ]
#

n = len(args)
if n == 0:
super(OptionGroup, self).__init__()
elif n == 1:
super(OptionGroup, self).__init__(args[0])
else:
super(OptionGroup, self).__init__(args)

Expand Down Expand Up @@ -106,7 +114,7 @@ def __new__(mcs, name, bases, namespace):
code = code[0]
opt = code_options_mapping.get(code, None)
if opt is None or not isinstance(opt, Option):
raise SyntaxError('%s is not available Option of %s' % (code, instance.__name__))
raise SyntaxError('"%s" is not available Option of %s' % (code, instance.__name__))
assert callable(opt.tag_added)
assert callable(opt.tag_removed)
opt.add_tag(attr)
Expand Down
2 changes: 1 addition & 1 deletion optenum/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
""" version file """

__version__ = '1.1.4'
__version__ = '1.1.5'
24 changes: 24 additions & 0 deletions tests/test_options_customized.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,30 @@ class Foo(Options):
Foo.B.remove_tag('FOO')
self.assertEqual(Foo.FOO, ())

def test_grouping_single_option(self):

class Foo(Options):
A = 1
B = 2, 'B is 2'
C = 'C', 'C is letter', ['FOO']
D = Option('d', name='D')
E = Option('e1', 'E', 'Earth')
F = Option('15', 'F', 'Finish', ['FOO'])

G1 = G(A)
G2 = G(B)
G3 = G(C)
G4 = G(D)
G5 = G(E)
G6 = G(F)

self.assertEqual(Foo.G1, (Foo.A, ))
self.assertEqual(Foo.G2, (Foo.B, ))
self.assertEqual(Foo.G3, (Foo.C, ))
self.assertEqual(Foo.G4, (Foo.D, ))
self.assertEqual(Foo.G5, (Foo.E, ))
self.assertEqual(Foo.G6, (Foo.F, ))


if __name__ == '__main__':
unittest.main()

0 comments on commit 15ffeb9

Please sign in to comment.