Skip to content

Commit

Permalink
Clean up NotImplemented/NotImplementedError
Browse files Browse the repository at this point in the history
- if something is not implemented, just raise NotImplementedError
  directly, rather than trying to sometimes return a NotImplemented
  object.  Returning a NotImplemented object complicates the type
  signature without providing much of an advantage in terms of
  workflow.

- PGPSignature.target_signature is a property that was never
  implemented or used.  We can just drop this property entirely,
  implementing it only when it can actually be implemented.
  • Loading branch information
dkg committed Jul 21, 2023
1 parent 88e761f commit 457e812
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
16 changes: 8 additions & 8 deletions pgpy/packet/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def __getitem__(self, key):

def __delitem__(self, key):
##TODO: this
raise NotImplementedError
raise NotImplementedError()

def __contains__(self, key):
return key in {k for k, _ in itertools.chain(self._hashed_sp, self._unhashed_sp)}
Expand Down Expand Up @@ -384,10 +384,10 @@ def publen(self) -> int:
return len(self)

def verify(self, subj, sigbytes, hash_alg):
raise NotImplemented # pragma: no cover
raise NotImplementedError() # pragma: no cover

def encrypt(self, symalg: Optional[SymmetricKeyAlgorithm], data: bytes, fpr: Fingerprint) -> CipherText:
raise NotImplemented
raise NotImplementedError()

def _encrypt_helper(self, symalg: Optional[SymmetricKeyAlgorithm], plaintext: bytes) -> bytes:
'Common code for re-shaping session keys before storing in PKESK'
Expand All @@ -405,7 +405,7 @@ def __iter__(self):
yield self.data

def __pubkey__(self):
return NotImplemented
raise NotImplementedError()

def __bytearray__(self) -> bytearray:
return self.data
Expand Down Expand Up @@ -1441,7 +1441,7 @@ def encrypt_keyblob(self, passphrase: str,

@abc.abstractmethod
def decrypt_keyblob(self, passphrase: Union[str, bytes]) -> None:
...
raise NotImplementedError()

def _decrypt_keyblob_helper(self, passphrase: Union[str, bytes]) -> Optional[bytearray]:
if not self.s2k: # pragma: no cover
Expand Down Expand Up @@ -1477,10 +1477,10 @@ def _decrypt_keyblob_helper(self, passphrase: Union[str, bytes]) -> Optional[byt
return bytearray(pt)

def sign(self, sigdata, hash_alg):
return NotImplemented # pragma: no cover
raise NotImplementedError() # pragma: no cover

def decrypt(self, ct: CipherText, fpr: Fingerprint, get_symalg: bool) -> Tuple[Optional[SymmetricKeyAlgorithm],bytes]:
raise NotImplemented
raise NotImplementedError()

def _decrypt_helper(self, plaintext: bytes, get_symalg: bool) -> Tuple[Optional[SymmetricKeyAlgorithm], bytes]:

Expand Down Expand Up @@ -1528,7 +1528,7 @@ def clear(self) -> None:

class OpaquePrivKey(PrivKey, OpaquePubKey): # pragma: no cover
def __privkey__(self):
return NotImplemented
raise NotImplementedError()

def _generate(self, key_size_or_oid: Optional[Union[int, EllipticCurveOID]]) -> None:
raise NotImplementedError()
Expand Down
12 changes: 2 additions & 10 deletions pgpy/pgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,6 @@ def intended_recipients(self):
"""
return map(lambda x: x.intended_recipient, self._signature.subpackets['IntendedRecipient'])

@property
def target_signature(self):
return NotImplemented

@property
def type(self) -> SignatureType:
"""
Expand Down Expand Up @@ -1010,7 +1006,7 @@ def __format__(self, format_spec: str) -> str:
if isinstance(self._uid, UserID):
return self._uid.uid

raise NotImplementedError
raise NotImplementedError()


class PGPMessage(Armorable):
Expand Down Expand Up @@ -1103,7 +1099,7 @@ def type(self) -> Literal['cleartext', 'literal', 'encrypted']:
if isinstance(self._message, (SKEData, IntegrityProtectedSKEData)):
return 'encrypted'

raise NotImplementedError
raise NotImplementedError()

def __init__(self) -> None:
"""
Expand Down Expand Up @@ -2225,8 +2221,6 @@ def _sign(self, subject: PGPSubject, sig: PGPSignature, **prefs) -> PGPSignature
sig._signature.hash2 = bytearray(h2.finalize()[:2])

_sig = self._key.sign(sigdata, getattr(hashes, sig.hash_algorithm.name)())
if _sig is NotImplemented:
raise NotImplementedError(self.key_algorithm)

sig._signature.signature.from_signer(_sig)
sig._signature.update_hlen()
Expand Down Expand Up @@ -2811,8 +2805,6 @@ def _filter_sigs(sigs: Iterable[PGPSignature]) -> Iterator[PGPSignature]:
sigv.add_sigsubj(sig, self, subj, issues)
else:
verified = self._key.verify(sig.hashdata(subj), sig.__sig__, getattr(hashes, sig.hash_algorithm.name)())
if verified is NotImplemented:
raise NotImplementedError(sig.key_algorithm)

sigv.add_sigsubj(sig, self, subj, SecurityIssues.WrongSig if not verified else SecurityIssues.OK)

Expand Down

0 comments on commit 457e812

Please sign in to comment.