[openpgp] Improve type annotations

This commit is contained in:
Philipp Hörist
2025-02-27 22:59:18 +01:00
parent 043fc120da
commit 9571a622ed
8 changed files with 57 additions and 37 deletions

View File

@@ -16,7 +16,6 @@
from __future__ import annotations
from typing import Any
from typing import TYPE_CHECKING
from collections.abc import Sequence
@@ -29,8 +28,7 @@ if TYPE_CHECKING:
class BaseKeyringItem:
def __init__(self, key: Any) -> None:
self._key = key
def __init__(self) -> None:
self._uid = self._get_uid()
@property

View File

@@ -18,6 +18,7 @@ from typing import Any
from typing import cast
import logging
from collections.abc import Iterator
from collections.abc import Sequence
from pathlib import Path
@@ -37,6 +38,9 @@ log = logging.getLogger("gajim.p.openpgp.gpgme")
class KeyringItem(BaseKeyringItem):
def __init__(self, key: Key) -> None:
self._key = key
BaseKeyringItem.__init__(self)
def _get_uid(self) -> str | None:
for uid in self._key.uids:
@@ -98,7 +102,7 @@ class GPGMe(BasePGPBackend):
def get_keys(self) -> Sequence[KeyringItem]:
keys: list[KeyringItem] = []
with self._get_context() as context:
for key in context.keylist(secret=False):
for key in cast(Iterator[Key], context.keylist(secret=False)):
keyring_item = KeyringItem(key)
if not keyring_item.is_xmpp_key:
log.warning("Key not suited for xmpp: %s", key.fpr)
@@ -133,7 +137,7 @@ class GPGMe(BasePGPBackend):
recipients: list[Any] = []
with self._get_context() as context:
for key in keys:
key = context.get_key(key.fingerprint)
key = cast(Key | None, context.get_key(key.fingerprint))
if key is not None:
recipients.append(key)
@@ -155,16 +159,18 @@ class GPGMe(BasePGPBackend):
except Exception as error:
raise DecryptionFailed("Decryption failed: %s" % error)
plaintext, result, verify_result = result
plaintext = plaintext.decode()
plaintext, result, verify_result = result
plaintext = plaintext.decode()
fingerprints = [sig.fpr for sig in verify_result.signatures]
if not fingerprints or len(fingerprints) > 1:
log.error(result)
log.error(verify_result)
raise DecryptionFailed("Verification failed")
fingerprints = [sig.fpr for sig in verify_result.signatures]
if not fingerprints or len(fingerprints) > 1:
log.error(result)
log.error(verify_result)
raise DecryptionFailed("Verification failed")
return plaintext, fingerprints[0]
return plaintext, fingerprints[0]
raise RuntimeError
def import_key(self, data: bytes, jid: JID) -> KeyringItem | None:
log.info("Import key from %s", jid)

View File

@@ -1,19 +1,3 @@
# Copyright (C) 2025 Philipp Hörist <philipp AT hoerist.com>
#
# This file is part of the OpenPGP Gajim Plugin.
#
# OpenPGP Gajim Plugin is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; version 3 only.
#
# OpenPGP Gajim Plugin is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OpenPGP Gajim Plugin. If not, see <http://www.gnu.org/licenses/>.
from __future__ import annotations
from typing import Any

View File

@@ -14,6 +14,8 @@
# You should have received a copy of the GNU General Public License
# along with OpenPGP Gajim Plugin. If not, see <http://www.gnu.org/licenses/>.
from typing import Any
import logging
from collections.abc import Sequence
from pathlib import Path
@@ -35,6 +37,9 @@ if log.getEffectiveLevel() == logging.DEBUG:
class KeyringItem(BaseKeyringItem):
def __init__(self, key: dict[Any, Any]) -> None:
self._key = key
BaseKeyringItem.__init__(self)
@property
def keyid(self) -> str:

View File

@@ -105,7 +105,7 @@ class Storage:
) -> None:
sql = """REPLACE INTO
contacts(jid, fingerprint, active, trust, timestamp)
VALUES(?, ?, ?, ?, ?, ?)"""
VALUES(?, ?, ?, ?, ?)"""
for values in db_values:
log.info("Store key: %s", values)
self._con.execute(sql, values)

View File

@@ -71,7 +71,7 @@ class KeyWizard(Gtk.Assistant):
self.connect("cancel", self._on_cancel)
self.connect("close", self._on_cancel)
self._remove_sidebar()
# self._remove_sidebar()
self.show()
def _add_page(self, page: Gtk.Box) -> None: