Debian Codegraph @

ml_collections/config_flags/tests/configdict_config.py

raw symbols SCIP
1 # Copyright 2024 The ML Collections Authors.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """ConfigDict config file."""
16
17 from ml_collections import config_dict
18
19
20 class UnusableConfig(object):
21 """Test against code assuming the semantics of attributes (such as `lock`).
22
23 This class is to test that the flags implementation does not assume the
24 semantics of attributes. This is to avoid code such as:
25
26 ```python
27 if hasattr(obj, lock):
28 obj.lock()
29 ```
30
31 which will fail if `obj` has an attribute `lock` that does not behave in the
32 way we expect.
33
34 This class only has unusable attributes. There are two
35 exceptions for which this class behaves normally:
36 * Python's special functions which start and end with a double underscore.
37 * `valid_attribute`, an attribute used to test the class.
38
39 For other attributes, both `hasttr(obj, attr)` and `callable(obj, attr)` will
40 return True. Calling `obj.attr` will return a function which takes no
41 arguments and raises an AttributeError when called. For example, the `lock`
42 example above will raise an AttributeError. The only valid action on
43 attributes is assignment, e.g.
44
45 ```python
46 obj = UnusableConfig()
47 obj.attr = 1
48 ```
49
50 In which case the attribute will keep its assigned value and become usable.
51 """
52
53 def __init__(self):
54 self._valid_attribute = 1
55
56 def __getattr__(self, attribute):
57 """Get an arbitrary attribute.
58
59 Returns a function which takes no arguments and raises an AttributeError,
60 except for Python special functions in which case an AttributeError is
61 directly raised.
62
63 Args:
64 attribute: A string representing the attribute's name.
65 Returns:
66 A function which raises an AttributeError when called.
67 Raises:
68 AttributeError: when the attribute is a Python special function starting
69 and ending with a double underscore.
70 """
71 if attribute.startswith("__") and attribute.endswith("__"):
72 raise AttributeError("UnusableConfig does not contain entry {}.".
73 format(attribute))
74
75 def raise_attribute_error_fun():
76 raise AttributeError(
77 "{} is not a usable attribute of UnusableConfig".format(
78 attribute))
79
80 return raise_attribute_error_fun
81
82 @property
83 def valid_attribute(self):
84 return self._valid_attribute
85
86 @valid_attribute.setter
87 def valid_attribute(self, value):
88 self._valid_attribute = value
89
90 def get_config():
91 """Returns a ConfigDict. Used for tests."""
92 cfg = config_dict.ConfigDict()
93 cfg.integer = 1
94 cfg.reference = config_dict.FieldReference(1)
95 cfg.list = [1, 2, 3]
96 cfg.nested_list = [[1, 2, 3]]
97 cfg.nested_configdict = config_dict.ConfigDict()
98 cfg.nested_configdict.integer = 1
99 cfg.unusable_config = UnusableConfig()
100
101 return cfg