def convert(cls, value: Any) -> VarNames: """ Convert the given value to a list of variable name patterns. """ # Can be optional if value is None: return None if isinstance(value, str): return to_list(value) if not isinstance(value, list): raise ValueError('Variable name pattern can only be a string or a' ' list of strings.') for item in value: if not isinstance(item, str): raise ValueError('Variable name pattern can only be a string' ' or a list of strings.') return value
def test_int(self): self.assertEqual(to_list(1, dtype=int), [1]) self.assertEqual(to_list('1, 2, 3', dtype=int), [1, 2, 3]) self.assertEqual(to_list([1, 2, 3], dtype=int), [1, 2, 3]) self.assertEqual(to_list((1, 2, 3), dtype=int), [1, 2, 3]) self.assertEqual(to_list(['1', '2', '3'], dtype=int), [1, 2, 3])
def test_str(self): self.assertEqual(to_list('a'), ['a']) self.assertEqual(to_list('a, b, c'), ['a', 'b', 'c']) self.assertEqual(to_list(['a', 'b', 'c']), ['a', 'b', 'c']) self.assertEqual(to_list(('a', 'b', 'c')), ['a', 'b', 'c']) self.assertEqual(to_list([1, 2, 3]), ['1', '2', '3'])
def test_none_and_empty(self): self.assertEqual(to_list(None), None) self.assertEqual(to_list([]), [])
def test_int(self): self.assertEqual(to_list(1, dtype=int), [1]) self.assertEqual(to_list('1, 2, 3', dtype=int), [1, 2, 3]) self.assertEqual(to_list([1, 2, 3], dtype=int), [1, 2, 3]) self.assertEqual(to_list((1, 2, 3), dtype=int), [1, 2, 3]) self.assertEqual(to_list(['1', '2', '3'], dtype=int), [1, 2, 3])
def test_str(self): self.assertEqual(to_list('a'), ['a']) self.assertEqual(to_list('a, b, c'), ['a', 'b', 'c']) self.assertEqual(to_list(['a', 'b', 'c']), ['a', 'b', 'c']) self.assertEqual(to_list(('a', 'b', 'c')), ['a', 'b', 'c']) self.assertEqual(to_list([1, 2, 3]), ['1', '2', '3'])
def test_none_and_empty(self): self.assertEqual(to_list(None), None) self.assertEqual(to_list([]), [])