Skip to content

Commit 7068b33

Browse files
committed
lint: Don't use "else" when it's not needed
Reflow some related error message formatting as well: no functional changes
1 parent 0306ab2 commit 7068b33

File tree

4 files changed

+42
-56
lines changed

4 files changed

+42
-56
lines changed

securesystemslib/ecdsa_keys.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,15 +327,11 @@ def verify_signature(public_key, scheme, signature, data):
327327
f"Failed to load PEM key {public_key}"
328328
) from e
329329

330-
if not isinstance( # pylint: disable=no-else-raise
331-
ecdsa_key, ec.EllipticCurvePublicKey
332-
):
330+
if not isinstance(ecdsa_key, ec.EllipticCurvePublicKey):
333331
raise exceptions.FormatError(
334332
"Invalid ECDSA public" " key: " + repr(public_key)
335333
)
336-
337-
else:
338-
logger.debug("Loaded a valid ECDSA public key.")
334+
logger.debug("Loaded a valid ECDSA public key.")
339335

340336
# verify() raises an 'InvalidSignature' exception if 'signature'
341337
# is invalid.

securesystemslib/gpg/common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def parse_pubkey_bundle(data):
210210
# - there must be least one User ID packet, or
211211
# - order and type of signatures, or
212212
# - disallow duplicate packets
213-
if ( # pylint: disable=no-else-raise
213+
if (
214214
packet_type != PACKET_TYPE_PRIMARY_KEY
215215
and not key_bundle[PACKET_TYPE_PRIMARY_KEY]["key"]
216216
):
@@ -219,7 +219,7 @@ def parse_pubkey_bundle(data):
219219
"got '{}'.".format(PACKET_TYPE_PRIMARY_KEY, packet_type)
220220
)
221221

222-
elif (
222+
if (
223223
packet_type == PACKET_TYPE_PRIMARY_KEY
224224
and key_bundle[PACKET_TYPE_PRIMARY_KEY]["key"]
225225
):
@@ -228,7 +228,7 @@ def parse_pubkey_bundle(data):
228228
# Fully parse master key to fail early, e.g. if key is malformed
229229
# or not supported, but also retain original packet for subkey binding
230230
# signature verification
231-
elif packet_type == PACKET_TYPE_PRIMARY_KEY:
231+
if packet_type == PACKET_TYPE_PRIMARY_KEY:
232232
key_bundle[PACKET_TYPE_PRIMARY_KEY] = {
233233
"key": parse_pubkey_payload(bytearray(payload)),
234234
"packet": packet,

securesystemslib/keys.py

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -454,24 +454,21 @@ def format_keyval_to_metadata(keytype, scheme, key_value, private=False):
454454
# key in the returned dictionary, ensure the private key is actually
455455
# present in 'key_val' (a private key is optional for 'KEYVAL_SCHEMA'
456456
# dicts).
457-
if "private" not in key_value: # pylint: disable=no-else-raise
457+
if "private" not in key_value:
458458
raise exceptions.FormatError(
459-
"The required private key"
460-
" is missing from: " + repr(key_value)
459+
"The required private key is missing from: " + repr(key_value)
461460
)
462461

463-
else:
464-
return {"keytype": keytype, "scheme": scheme, "keyval": key_value}
462+
return {"keytype": keytype, "scheme": scheme, "keyval": key_value}
465463

466-
else:
467-
public_key_value = {"public": key_value["public"]}
464+
public_key_value = {"public": key_value["public"]}
468465

469-
return {
470-
"keytype": keytype,
471-
"scheme": scheme,
472-
"keyid_hash_algorithms": settings.HASH_ALGORITHMS,
473-
"keyval": public_key_value,
474-
}
466+
return {
467+
"keytype": keytype,
468+
"scheme": scheme,
469+
"keyid_hash_algorithms": settings.HASH_ALGORITHMS,
470+
"keyval": public_key_value,
471+
}
475472

476473

477474
def format_metadata_to_key(
@@ -830,15 +827,14 @@ def verify_signature(
830827

831828
# Verify that the KEYID in 'key_dict' matches the KEYID listed in the
832829
# 'signature'.
833-
if key_dict["keyid"] != signature["keyid"]: # pylint: disable=no-else-raise
830+
if key_dict["keyid"] != signature["keyid"]:
834831
raise exceptions.CryptoError(
835832
"The KEYID ("
836833
" " + repr(key_dict["keyid"]) + " ) in the given key does not match"
837834
" the KEYID ( " + repr(signature["keyid"]) + " ) in the signature."
838835
)
839836

840-
else:
841-
logger.debug("The KEYIDs of key_dict and the signature match.")
837+
logger.debug("The KEYIDs of key_dict and the signature match.")
842838

843839
# Using the public key belonging to 'key_dict'
844840
# (i.e., rsakey_dict['keyval']['public']), verify whether 'signature'
@@ -1235,42 +1231,40 @@ def extract_pem(pem, private_pem=False):
12351231

12361232
except ValueError:
12371233
# Be careful not to print private key material in exception message.
1238-
if not private_pem: # pylint: disable=no-else-raise
1239-
raise exceptions.FormatError( # pylint: disable=raise-missing-from
1240-
"Required PEM"
1241-
" header " + repr(pem_header) + "\n not found in PEM"
1242-
" string: " + repr(pem)
1243-
)
1244-
1245-
else:
1234+
if not private_pem:
12461235
raise exceptions.FormatError( # pylint: disable=raise-missing-from
1247-
"Required PEM"
1248-
" header "
1236+
"Required PEM header "
12491237
+ repr(pem_header)
1250-
+ "\n not found in private PEM string."
1238+
+ "\n not found in PEM string: "
1239+
+ repr(pem)
12511240
)
12521241

1242+
raise exceptions.FormatError( # pylint: disable=raise-missing-from
1243+
"Required PEM header "
1244+
+ repr(pem_header)
1245+
+ "\n not found in private PEM string."
1246+
)
1247+
12531248
try:
12541249
# Search for 'pem_footer' after the PEM header.
12551250
footer_start = pem.index(pem_footer, header_start + len(pem_header))
12561251

12571252
except ValueError:
12581253
# Be careful not to print private key material in exception message.
1259-
if not private_pem: # pylint: disable=no-else-raise
1254+
if not private_pem:
12601255
raise exceptions.FormatError( # pylint: disable=raise-missing-from
1261-
"Required PEM"
1262-
" footer " + repr(pem_footer) + "\n not found in PEM"
1263-
" string " + repr(pem)
1264-
)
1265-
1266-
else:
1267-
raise exceptions.FormatError( # pylint: disable=raise-missing-from
1268-
"Required PEM"
1269-
" footer "
1256+
"Required PEM footer "
12701257
+ repr(pem_footer)
1271-
+ "\n not found in private PEM string."
1258+
+ "\n not found in PEM string "
1259+
+ repr(pem)
12721260
)
12731261

1262+
raise exceptions.FormatError( # pylint: disable=raise-missing-from
1263+
"Required PEM footer "
1264+
+ repr(pem_footer)
1265+
+ "\n not found in private PEM string."
1266+
)
1267+
12741268
# Extract only the public portion of 'pem'. Leading or trailing whitespace
12751269
# is excluded.
12761270
pem = pem[header_start : footer_start + len(pem_footer)]

securesystemslib/schema.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -597,16 +597,14 @@ def __init__(self, lo=-2147483648, hi=2147483647):
597597
self._hi = hi
598598

599599
def check_match(self, object): # pylint: disable=redefined-builtin
600-
if isinstance( # pylint: disable=no-else-raise
601-
object, bool
602-
) or not isinstance(object, int):
600+
if isinstance(object, bool) or not isinstance(object, int):
603601
# We need to check for bool as a special case, since bool
604602
# is for historical reasons a subtype of int.
605603
raise exceptions.FormatError(
606604
"Got " + repr(object) + " instead of an integer."
607605
)
608606

609-
elif not (self._lo <= object <= self._hi):
607+
if not (self._lo <= object <= self._hi):
610608
int_range = "[" + repr(self._lo) + ", " + repr(self._hi) + "]."
611609
raise exceptions.FormatError(
612610
repr(object) + " not in range " + int_range
@@ -886,22 +884,20 @@ def __init__(
886884
self._struct_name = struct_name
887885

888886
def check_match(self, object): # pylint: disable=redefined-builtin
889-
if not isinstance( # pylint: disable=no-else-raise
890-
object, (list, tuple)
891-
):
887+
if not isinstance(object, (list, tuple)):
892888
raise exceptions.FormatError(
893889
"Expected "
894890
+ repr(self._struct_name)
895891
+ "; but got "
896892
+ repr(object)
897893
)
898894

899-
elif len(object) < self._min:
895+
if len(object) < self._min:
900896
raise exceptions.FormatError(
901897
"Too few fields in " + self._struct_name
902898
)
903899

904-
elif len(object) > len(self._sub_schemas) and not self._allow_more:
900+
if len(object) > len(self._sub_schemas) and not self._allow_more:
905901
raise exceptions.FormatError(
906902
"Too many fields in " + self._struct_name
907903
)

0 commit comments

Comments
 (0)