diff --git a/openpgp/backend/gpgme.py b/openpgp/backend/gpgme.py index b34061f..b42ffdc 100644 --- a/openpgp/backend/gpgme.py +++ b/openpgp/backend/gpgme.py @@ -24,6 +24,7 @@ from pathlib import Path import gpg from gpg.errors import KeyNotFound +from gpg.results import GenkeyResult from gpg.results import ImportResult from nbxmpp.protocol import JID @@ -64,12 +65,15 @@ class GPGMe(BasePGPBackend): def generate_key(self) -> None: with self._get_context() as context: - result = context.create_key( - f"xmpp:{self._jid}", - algorithm="default", - expires=False, - passphrase=None, - force=False, + result = cast( + GenkeyResult, + context.create_key( + f"xmpp:{self._jid}", + algorithm="default", + expires=False, + passphrase=None, + force=False, + ), ) log.info("Generated new key: %s", result.fpr) @@ -174,6 +178,7 @@ class GPGMe(BasePGPBackend): def import_key(self, data: bytes, jid: JID) -> KeyringItem | None: log.info("Import key from %s", jid) + item = None with self._get_context() as context: result = context.key_import(data) if not isinstance(result, ImportResult) or result.imported != 1: @@ -183,6 +188,7 @@ class GPGMe(BasePGPBackend): fingerprint = result.imports[0].fpr key = self._get_key(fingerprint) + assert key is not None item = KeyringItem(key) if not item.is_valid(jid): log.warning("Invalid key found") @@ -197,4 +203,4 @@ class GPGMe(BasePGPBackend): key = self._get_key(fingerprint) assert key is not None with self._get_context() as context: - context.op_delete(key, True) + context.op_delete(key, True) # pyright: ignore diff --git a/openpgp/modules/key_store.py b/openpgp/modules/key_store.py index 10b9d33..bcf29a8 100644 --- a/openpgp/modules/key_store.py +++ b/openpgp/modules/key_store.py @@ -80,8 +80,7 @@ class ContactData: @property def userid(self): - if self.jid is None: - raise ValueError("JID not set") + assert self.jid is not None, "JID not set" return "xmpp:%s" % self.jid @property diff --git a/typings/gpg/__init__.py b/typings/gpg/__init__.py index 9998082..2ba9a2d 100644 --- a/typings/gpg/__init__.py +++ b/typings/gpg/__init__.py @@ -1 +1 @@ -from .core import Context # noqa: F401 +from .core import Context # noqa: F401 # pyright: ignore diff --git a/typings/gpg/core.pyi b/typings/gpg/core.pyi index a7d371a..ec90582 100644 --- a/typings/gpg/core.pyi +++ b/typings/gpg/core.pyi @@ -4,7 +4,6 @@ from collections.abc import Iterator from gpg.results import DecryptResult from gpg.results import EncryptResult -from gpg.results import Key from gpg.results import SignResult from gpg.results import VerifyResult diff --git a/typings/gpg/results.pyi b/typings/gpg/results.pyi index 9fc5e84..e56230e 100644 --- a/typings/gpg/results.pyi +++ b/typings/gpg/results.pyi @@ -50,13 +50,31 @@ class VerifyResult(Result): is_mime: int signatures: list[Signature] -class ImportStatus(Result): ... +class ImportStatus(Result): + fpr: str + result: int + status: int class ImportResult(Result): - imports: ImportStatus + imported: int + imported_rsa: int + imports: list[ImportStatus] + new_revocations: int + new_signatures: int + new_sub_keys: int + new_user_ids: int + no_user_id: int + not_imported: int + secret_imported: int + secret_read: int + secret_unchanged: int + skipped_new_keys: int + skipped_v3_keys: int + unchanged: int class GenkeyResult(Result): _type = dict(primary=bool, sub=bool) + fpr: str class KeylistResult(Result): _type = dict(truncated=bool)