From 15ffeb940b317d4200d7aa5227eace1374deae18 Mon Sep 17 00:00:00 2001 From: Samuel Chen Date: Tue, 4 Jun 2019 02:00:12 +0800 Subject: [PATCH] Fix issue of creating single option group as class attribute --- RELEASENOTE.md | 4 ++++ optenum/options.py | 14 +++++++++++--- optenum/version.py | 2 +- tests/test_options_customized.py | 24 ++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/RELEASENOTE.md b/RELEASENOTE.md index ffb28ea..8a1f432 100644 --- a/RELEASENOTE.md +++ b/RELEASENOTE.md @@ -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) diff --git a/optenum/options.py b/optenum/options.py index c459d1d..9440c10 100644 --- a/optenum/options.py +++ b/optenum/options.py @@ -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) @@ -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) diff --git a/optenum/version.py b/optenum/version.py index beb7a39..9da63a8 100644 --- a/optenum/version.py +++ b/optenum/version.py @@ -1,3 +1,3 @@ """ version file """ -__version__ = '1.1.4' +__version__ = '1.1.5' diff --git a/tests/test_options_customized.py b/tests/test_options_customized.py index e9a5191..b3f590d 100644 --- a/tests/test_options_customized.py +++ b/tests/test_options_customized.py @@ -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()