def test_works_fn(self): # pylint: disable=unused-argument def my_function( str_: str = 'foo', int_: int = 10, float_: float = 1.0, bool_: bool = False, list_int: List[int] = [1, 2, 3], tuple_str: Tuple[str] = ('foo', ), sequence_bool: Sequence[bool] = [True, False], optional_int: Optional[int] = None, optional_float: Optional[float] = None, optional_list_int: Optional[List[int]] = None, ): # pylint: disable=dangerous-default-value pass # pylint: enable=unused-argument expected_settings = { 'str_': 'foo', 'int_': 10, 'float_': 1.0, 'bool_': False, 'list_int': [1, 2, 3], 'tuple_str': ('foo', ), 'sequence_bool': [True, False], 'optional_int': None, 'optional_float': None, 'optional_list_int': None, } ff_dict = ff.auto(my_function) self.assertCountEqual(expected_settings, ff_dict) ff.DEFINE_dict('my_function_settings', **ff_dict) self.assertEqual(FLAGS.my_function_settings, expected_settings)
def test_supports_tuples_with_more_than_one_element(self): def my_function(three_ints: Tuple[int, int, int] = (1, 2, 3), zero_or_more_strings: Tuple[str, ...] = ('foo', 'bar')): del three_ints, zero_or_more_strings expected_settings = { 'three_ints': (1, 2, 3), 'zero_or_more_strings': ('foo', 'bar'), } ff_dict = ff.auto(my_function) self.assertCountEqual(expected_settings, ff_dict) ff.DEFINE_dict('my_function_settings', **ff_dict) self.assertEqual(FLAGS.my_function_settings, expected_settings)
def test_works_metaclass(self): # This replicates an issue with Sonnet v2 modules, where the constructor # arguments are hidden by the metaclass. class MyMetaclass(abc.ABCMeta): def __call__(cls, *args, **kwargs): del args, kwargs class MyClass(metaclass=MyMetaclass): def __init__(self, a: int = 10, b: float = 1.0, c: Sequence[int] = (1, 2, 3)): del a, b, c ff_dict = ff.auto(MyClass) self.assertEqual(['a', 'b', 'c'], list(ff_dict)) ff.DEFINE_dict('my_meta_class_settings', **ff_dict) self.assertEqual(FLAGS.my_meta_class_settings['a'], 10) self.assertEqual(FLAGS.my_meta_class_settings['b'], 1.0) self.assertEqual(FLAGS.my_meta_class_settings['c'], (1, 2, 3))
# See the License for the specific language governing permissions and # limitations under the License. # ============================================================================ """Tests for compatibility with absl.testing.flagsaver.""" from absl import flags from absl.testing import absltest from absl.testing import flagsaver import fancyflags as ff flags.DEFINE_string("string_flag", "unchanged", "flag to test with") ff.DEFINE_dict( "test_dict_flag", dict=dict( nested=ff.Float(1.0, "nested flag"), ), unnested=ff.Integer(4, "unnested flag"), ) FLAGS = flags.FLAGS class FlagSaverTest(absltest.TestCase): def test_flagsaver_with_context_overrides(self): with flagsaver.flagsaver(**{ "string_flag": "new value", "test_dict_flag.dict.nested": -1.0, }): self.assertEqual("new value", FLAGS.string_flag)
# Copyright 2021 DeepMind Technologies Limited. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================ """A module that defines a dict flag, for testing purposes.""" import fancyflags as ff ff.DEFINE_dict( "settings", integer_field=ff.Integer(1, "integer field"), string_field=ff.String(None, "string field"), )
# distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================ """Test for flag overrides in fancyflags..""" from absl import flags from absl.testing import absltest import fancyflags as ff SETTINGS = ff.DEFINE_dict( "settings", integer_field=ff.Integer(1, "integer field"), string_field=ff.String(None, "string field"), nested=dict(float_field=ff.Float(0.0, "float field")), sequence_field=ff.Sequence([1, 2, 3], "sequence field"), another_sequence_field=ff.Sequence((3.14, 2.718), "another sequence field"), string_list_field=ff.StringList(["a"], "string list flag.")) class OverrideTest(absltest.TestCase): def test_give_me_a_name(self): expected = dict( integer_field=5, string_field=None, # Not overridden in BUILD args. nested=dict(float_field=3.2, ), sequence_field=[4, 5, 6], another_sequence_field=[3.0, 2.0], string_list_field=["a", "bunch", "of", "overrides"],