diff options
author | Siavash Khodadadeh <siavash.khodadadeh@gmail.com> | 2018-12-19 14:05:09 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-12-19 14:05:22 -0800 |
commit | de91d0a265b8c067b1d01d63731684819f62e4bc (patch) | |
tree | 135ed474dc654e39249c05e707bcc866459dabfc /absl/flags/tests | |
parent | 8575d76290eada1a3bcc6da4288cc263f7b3e84b (diff) | |
download | absl-py-de91d0a265b8c067b1d01d63731684819f62e4bc.tar.gz |
Allow defining multi fields with any Iterable, not just lists.
This was motivated to allow tuples as input, but was expanded to allow any
iterable. Strings and non-iterables are still special cased and converted
to a single-element list. A copy of the input iterable is made so that
the flag has sole ownership over the set of values.
NOTE: Custom MultiFlag behavior change: The items of the iterable, rather than
the iterable itself, are now passed onto the underlying self.parser object.
Custom flags using DEFINE_multi or MultiFlag should update their custom flag
code as appropriate.
NOTE: Heavily modified from original PR to adjust for various lint/style
errors and from the auto-formatter.
Resolves #78
Closes #80
PiperOrigin-RevId: 226230348
Diffstat (limited to 'absl/flags/tests')
-rw-r--r-- | absl/flags/tests/flags_test.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/absl/flags/tests/flags_test.py b/absl/flags/tests/flags_test.py index 90094d3..ac77707 100644 --- a/absl/flags/tests/flags_test.py +++ b/absl/flags/tests/flags_test.py @@ -1184,6 +1184,26 @@ class MultiNumericalFlagsTest(absltest.TestCase): expected_floats, FLAGS.get_flag_value('m_float', None)): self.assertAlmostEqual(expected, actual) + def test_multi_numerical_with_tuples(self): + """Verify multi_int/float accept tuples as default values.""" + flags.DEFINE_multi_integer( + 'm_int_tuple', + (77, 88), + 'integer option that can occur multiple times', + short_name='mi_tuple') + self.assertListEqual(FLAGS.get_flag_value('m_int_tuple', None), [77, 88]) + + dict_with_float_keys = {2.2: 'hello', 3: 'happy'} + float_defaults = dict_with_float_keys.keys() + flags.DEFINE_multi_float( + 'm_float_tuple', + float_defaults, + 'float option that can occur multiple times', + short_name='mf_tuple') + for (expected, actual) in zip(float_defaults, + FLAGS.get_flag_value('m_float_tuple', None)): + self.assertAlmostEqual(expected, actual) + def test_single_value_default(self): """Test multi_int and multi_float flags with a single default value.""" int_default = 77 |