Skip to content

Commit

Permalink
Add repetitive case to overloaded type signature
Browse files Browse the repository at this point in the history
This seems to be needed to make mypy happy. It can otherwise not deduce
that the usage in _get_private_objects is correct.
  • Loading branch information
lucc committed Dec 16, 2024
1 parent f42dd91 commit 0b6bd5e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 2 additions & 0 deletions khard/carddav_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
def multi_property_key(item: str) -> Tuple[Literal[0], str]: ...
@overload
def multi_property_key(item: Dict[T, Any]) -> Tuple[Literal[1], T]: ...
@overload
def multi_property_key(item: Union[str, Dict[T, Any]]) -> Tuple[Union[Literal[0], Literal[1]], Union[str, T]]: ...
def multi_property_key(item: Union[str, Dict[T, Any]]
) -> Tuple[int, Union[T, str]]:
"""Key function to pass to sorted(), allowing sorting of dicts with lists
Expand Down
27 changes: 26 additions & 1 deletion test/test_carddav_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import unittest
from unittest import mock

from khard.carddav_object import CarddavObject
from khard.carddav_object import CarddavObject, multi_property_key


class CarddavObjectFormatDateObject(unittest.TestCase):
Expand Down Expand Up @@ -57,3 +57,28 @@ def test_parsing_base64_ecoded_photo_vcard_v4(self):
self.assertEqual(uri_stuff, 'data:image/png;base64')
data = base64.decodebytes(data.encode())
self.assertEqual(data[:8], self.PNG_HEADER)


class MultiPropertyKey(unittest.TestCase):
"""Test for the multi_property_key helper function"""

def test_strings_are_in_the_first_sort_group(self) -> None:
group, _key = multi_property_key("some string")
self.assertEqual(group, 0)

def test_dicts_are_in_the_second_sort_group(self) -> None:
group, _key = multi_property_key({"some": "dict"})
self.assertEqual(group, 1)

def test_strings_are_their_own_keys(self) -> None:
_group, key = multi_property_key("some string")
self.assertEqual(key, "some string")

def test_dicts_are_keyed_by_the_first_key(self) -> None:
_group, key = multi_property_key({"some": "dict", "more": "stuff"})
self.assertEqual(key, "some")

def test_all_strings_are_sorted_before_dicts(self) -> None:
my_list = ["a", {"c": "d"}, "e", {"f": "g"}]
my_list.sort(key=multi_property_key)
self.assertEqual(my_list, ["a", "e", {"c": "d"}, {"f": "g"}])
18 changes: 18 additions & 0 deletions test/test_yaml_editable.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,21 @@ def test_private_objects_can_have_an_ablabel(self) -> None:
label.group = "1"
result = ye._get_private_objects()
self.assertEqual(result, {"foo": [{"baz": "bar"}]})

def test_private_objects_with_ablabels_are_sorted_by_ablabel(self) -> None:
ye = TestYAMLEditable()
ye.supported_private_objects = ["foo"]
foo1 = ye.vcard.add("X-FOO")
foo1.value = "AAA"
foo1.group = "1"
label1 = ye.vcard.add("X-ABLABEL")
label1.value = "yyy"
label1.group = "1"
foo2 = ye.vcard.add("X-FOO")
foo2.value = "BBB"
foo2.group = "2"
label2 = ye.vcard.add("X-ABLABEL")
label2.value = "xxx"
label2.group = "2"
result = ye._get_private_objects()
self.assertEqual(result, {"foo": [{"xxx": "BBB"}, {"yyy": "AAA"}]})

0 comments on commit 0b6bd5e

Please sign in to comment.