-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add a docstring & tests for _flatten_dict. #14981
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add tests for `_flatten_dict`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -466,6 +466,29 @@ def _flatten_dict( | |
prefix: Optional[List[str]] = None, | ||
result: Optional[Dict[str, str]] = None, | ||
) -> Dict[str, str]: | ||
""" | ||
Given a JSON dictionary (or event) which might contain sub dictionaries, | ||
flatten it into a single layer dictionary by combining the keys & sub-keys. | ||
|
||
clokep marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Any (non-dictionary), non-string value is dropped. | ||
|
||
Transforms: | ||
|
||
{"foo": {"bar": "test"}} | ||
|
||
To: | ||
|
||
{"foo.bar": "test"} | ||
Comment on lines
+475
to
+481
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How would (should) the dict {"foo.bar": 1, "foo": {"bar": 2}} be transformed? I imagine that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is undefined behavior, one of which the pull requests that I'll be implementing will be fixing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MSC3873 for the record. |
||
|
||
Args: | ||
d: The event or content to continue flattening. | ||
room_version: The room version object. | ||
prefix: The key prefix (from outer dictionaries). | ||
result: The result to mutate. | ||
|
||
Returns: | ||
The resulting dictionary. | ||
""" | ||
if prefix is None: | ||
prefix = [] | ||
if result is None: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import Dict, List, Optional, Set, Union, cast | ||
from typing import Any, Dict, List, Optional, Set, Union, cast | ||
|
||
import frozendict | ||
|
||
|
@@ -37,6 +37,30 @@ | |
from tests.test_utils.event_injection import create_event, inject_member_event | ||
|
||
|
||
class FlattenDictTestCase(unittest.TestCase): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be worth a test case where this function is given an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe -- I'm not changing that behavior so I'll try to do that as a follow-up so I can at least avoid the self-conflicts. |
||
def test_simple(self) -> None: | ||
"""Test a dictionary that isn't modified.""" | ||
input = {"foo": "abc"} | ||
self.assertEqual(input, _flatten_dict(input)) | ||
|
||
def test_nested(self) -> None: | ||
"""Nested dictionaries become dotted paths.""" | ||
input = {"foo": {"bar": "abc"}} | ||
self.assertEqual({"foo.bar": "abc"}, _flatten_dict(input)) | ||
|
||
def test_non_string(self) -> None: | ||
"""Non-string items are dropped.""" | ||
input: Dict[str, Any] = { | ||
"woo": "woo", | ||
"foo": True, | ||
"bar": 1, | ||
"baz": None, | ||
"fuzz": [], | ||
"boo": {}, | ||
} | ||
self.assertEqual({"woo": "woo"}, _flatten_dict(input)) | ||
|
||
|
||
class PushRuleEvaluatorTestCase(unittest.TestCase): | ||
def _get_evaluator( | ||
self, | ||
|
Uh oh!
There was an error while loading. Please reload this page.