From 5718d96dc6ce7e2959f217c09c8c3c79e889920b Mon Sep 17 00:00:00 2001 From: Lucas Hoffmann Date: Wed, 6 Sep 2023 20:36:23 +0200 Subject: [PATCH] Fix type signatures --- khard/carddav_object.py | 8 ++++---- khard/helpers/__init__.py | 16 ++++++++++------ khard/helpers/typing.py | 3 ++- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/khard/carddav_object.py b/khard/carddav_object.py index a7c6456..24df4df 100644 --- a/khard/carddav_object.py +++ b/khard/carddav_object.py @@ -24,8 +24,8 @@ from . import address_book # pylint: disable=unused-import # for type checking from . import helpers -from .helpers.typing import (convert_to_vcard, Date, ObjectType, StrList, - list_to_string, string_to_date, string_to_list) +from .helpers.typing import (Date, ObjectType, PostAddress, StrList, + convert_to_vcard, list_to_string, string_to_date, string_to_list) from .query import AnyQuery, Query @@ -770,11 +770,11 @@ def add_email(self, type: str, address: str) -> None: label_obj.value = custom_types[0] @property - def post_addresses(self) -> Dict[str, List[Dict[str, Union[List, str]]]]: + def post_addresses(self) -> Dict[str, List[PostAddress]]: """ :returns: dict of type and post address list """ - post_adr_dict: Dict[str, List[Dict[str, Union[List, str]]]] = {} + post_adr_dict: Dict[str, List[PostAddress]] = {} for child in self.vcard.getChildren(): if child.name == "ADR": type = list_to_string(self._get_types_for_vcard_object( diff --git a/khard/helpers/__init__.py b/khard/helpers/__init__.py index 67b3df6..e549730 100644 --- a/khard/helpers/__init__.py +++ b/khard/helpers/__init__.py @@ -7,7 +7,10 @@ from typing import Any, Dict, List, Optional, Sequence, Union from ruamel.yaml.scalarstring import LiteralScalarString -from .typing import list_to_string +from .typing import list_to_string, PostAddress + + +YamlPostAddresses = Dict[str, Union[List[Dict[str, Any]], Dict[str, Any]]] def pretty_print(table: List[List[str]], justify: str = "L") -> str: @@ -115,10 +118,10 @@ def yaml_dicts( return data_dict -def yaml_addresses(addresses: Optional[Dict[str, List]], +def yaml_addresses(addresses: Optional[Dict[str, List[PostAddress]]], address_properties: List[str], defaults: Optional[List[str]] = None - ) -> Optional[Dict[str, Any]]: + ) -> Optional[YamlPostAddresses]: """ build a dict from an address, using a list of properties, an address has. @@ -134,7 +137,7 @@ def yaml_addresses(addresses: Optional[Dict[str, List]], address_fields = {key: None for key in address_properties} return {address_type: address_fields for address_type in defaults} - address_dict = {} + address_dict: YamlPostAddresses = {} for address_type, addresses_ in addresses.items(): entry = [ {key: yaml_clean(address.get(f"{key[0].lower()}{key[1:]}")) @@ -142,8 +145,9 @@ def yaml_addresses(addresses: Optional[Dict[str, List]], for address in addresses_ ] if len(entry) == 1: - entry = entry[0] - address_dict[address_type] = entry + address_dict[address_type] = entry[0] + else: + address_dict[address_type] = entry return address_dict diff --git a/khard/helpers/typing.py b/khard/helpers/typing.py index 9afd025..1ea67c4 100644 --- a/khard/helpers/typing.py +++ b/khard/helpers/typing.py @@ -2,7 +2,7 @@ from datetime import datetime from enum import Enum -from typing import List, Union +from typing import Dict, List, Union class ObjectType(Enum): @@ -14,6 +14,7 @@ class ObjectType(Enum): # some type aliases Date = Union[str, datetime] StrList = Union[str, List[str]] +PostAddress = Dict[str, str] def convert_to_vcard(name: str, value: StrList, constraint: ObjectType