def test_any_of_unit(self): MyEnum = Enum('MyEnum', {"a": "a_string", "b": "b_string"}) SecondEnum = Enum('SecondEnum', {"c": "c_cake", "d": "d_face"}) SomeEnum = Union[MyEnum, SecondEnum] # Asking for a value not in ANY enum raises a value error. with self.assertRaises(ValueError): u.any_of("face", SomeEnum)
def test_any_of(self, k1, v1, k2, v2): m1 = dict(zip(k1, v1)) m2 = dict(zip(k2, v2)) enum1 = Enum('enum1', m1) enum2 = Enum('enum2', m2) union = Union[enum1, enum2] # If the item appears in the first map any_of will return it. for k, v in m1.items(): self.assertEqual(u.any_of(v, union), enum1(v)) for k, v in m2.items(): # If a value from the second enum appears in enum1 any_of will return it; # else, it'll return the value from enum2. try: expected = enum1(v) except ValueError: expected = enum2(v) self.assertEqual(u.any_of(v, union), expected)
def parse_region(s: str) -> Region: """Attempts to parse the string into a valid region; raises a sensible argparse error if that's not possible. """ try: return u.any_of(s, Region) except ValueError: valid_values = u.enum_vals(valid_regions()) raise argparse.ArgumentTypeError("'{}' isn't a valid region. \ Must be one of {}.".format(s, valid_values))