Skip to content

Commit

Permalink
Use new type annotation features from python 3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
lucc committed Dec 16, 2024
1 parent b864931 commit 6d1310c
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 193 deletions.
10 changes: 6 additions & 4 deletions khard/actions.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
"""Names and aliases for the subcommands on the command line"""

from typing import Dict, Generator, Iterable, List, Optional
from __future__ import annotations

from typing import Generator, Iterable


class Actions:

"""A class to manage the names and aliases of the command line
subcommands."""

action_map: Dict[str, List[str]] = {
action_map: dict[str, list[str]] = {
"add-email": [],
"addressbooks": ["abooks"],
"birthdays": ["bdays"],
Expand All @@ -28,7 +30,7 @@ class Actions:
}

@classmethod
def get_action(cls, alias: str) -> Optional[str]:
def get_action(cls, alias: str) -> None | str:
"""Find the name of the action for the supplied alias. If no action is
associated with the given alias, None is returned.
Expand All @@ -42,7 +44,7 @@ def get_action(cls, alias: str) -> Optional[str]:
return None

@classmethod
def get_aliases(cls, action: str) -> List[str]:
def get_aliases(cls, action: str) -> list[str]:
"""Find all aliases for the given action. If there is no such action,
None is returned.
Expand Down
23 changes: 12 additions & 11 deletions khard/address_book.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""A simple class to load and manage the vcard files from disk."""

from __future__ import annotations

import abc
import binascii
from collections.abc import Mapping, Sequence
import glob
import logging
import os
from typing import Dict, Generator, Iterator, List, Optional, Union, overload
from typing import Generator, Iterator, overload

import vobject.base

Expand Down Expand Up @@ -42,9 +44,8 @@ class AddressBook(metaclass=abc.ABCMeta):
def __init__(self, name: str) -> None:
""":param name: the name to identify the address book"""
self._loaded = False
self.contacts: Dict[str, "carddav_object.CarddavObject"] = {}
self._short_uids: Optional[Dict[str,
"carddav_object.CarddavObject"]] = None
self.contacts: dict[str, "carddav_object.CarddavObject"] = {}
self._short_uids: None | dict[str, "carddav_object.CarddavObject"] = None
self.name = name

def __str__(self) -> str:
Expand Down Expand Up @@ -83,7 +84,7 @@ def search(self, query: Query) -> Generator["carddav_object.CarddavObject",
if query.match(contact):
yield contact

def get_short_uid_dict(self, query: Query = AnyQuery()) -> Dict[
def get_short_uid_dict(self, query: Query = AnyQuery()) -> dict[
str, "carddav_object.CarddavObject"]:
"""Create a dictionary of shortened UIDs for all contacts.
Expand Down Expand Up @@ -154,7 +155,7 @@ class VdirAddressBook(AddressBook):
"""

def __init__(self, name: str, path: str,
private_objects: Optional[List[str]] = None,
private_objects: None | list[str] = None,
localize_dates: bool = True, skip: bool = False) -> None:
"""
:param name: the name to identify the address book
Expand Down Expand Up @@ -236,7 +237,7 @@ class AddressBookCollection(AddressBook, Mapping, Sequence):
this class to use all other methods from the parent AddressBook class.
"""

def __init__(self, name: str, abooks: List[VdirAddressBook]) -> None:
def __init__(self, name: str, abooks: list[VdirAddressBook]) -> None:
"""
:param name: the name to identify the address book
:param abooks: a list of address books to combine in this collection
Expand Down Expand Up @@ -270,11 +271,11 @@ def load(self, query: Query = AnyQuery()) -> None:
len(self.contacts), self.name)

@overload
def __getitem__(self, key: Union[int, str]) -> VdirAddressBook: ...
def __getitem__(self, key: int | str) -> VdirAddressBook: ...
@overload
def __getitem__(self, key: slice) -> List[VdirAddressBook]: ...
def __getitem__(self, key: Union[int, str, slice]
) -> Union[VdirAddressBook, List[VdirAddressBook]]:
def __getitem__(self, key: slice) -> list[VdirAddressBook]: ...
def __getitem__(self, key: int | str | slice
) -> VdirAddressBook | list[VdirAddressBook]:
"""Get one or more of the backing address books by name or index
:param key: the name of the address book to get or its index
Expand Down
Loading

0 comments on commit 6d1310c

Please sign in to comment.