Skip to content

Commit 9324d6f

Browse files
authored
Run pyupgrade to simplify code and use Python 3.6 syntax (jpadilla#536)
pyugrade is a command line tool to automatically update Python syntax to modern usage and patterns. For additional details, see: https://github.com/asottile/pyupgrade Changes made by the tool: - Use short Python3 super() syntax. - Use f-strings when they are simple and more readable. - Drop Python 2 u prefix from strings. - Drop "r" argument from open(). It is the default and so specifying it is unnecessary.
1 parent a086e61 commit 9324d6f

File tree

8 files changed

+71
-79
lines changed

8 files changed

+71
-79
lines changed

jwt/algorithms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ def from_jwk(jwk):
468468
"Coords should be 66 bytes for curve P-521"
469469
)
470470
else:
471-
raise InvalidKeyError("Invalid curve: {}".format(curve))
471+
raise InvalidKeyError(f"Invalid curve: {curve}")
472472

473473
public_numbers = ec.EllipticCurvePublicNumbers(
474474
x=int_from_bytes(x, "big"),

jwt/api_jws.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,7 @@ def _load(self, jwt):
174174
jwt = jwt.encode("utf-8")
175175

176176
if not isinstance(jwt, bytes):
177-
raise DecodeError(
178-
"Invalid token type. Token must be a {}".format(bytes)
179-
)
177+
raise DecodeError(f"Invalid token type. Token must be a {bytes}")
180178

181179
try:
182180
signing_input, crypto_segment = jwt.rsplit(b".", 1)

jwt/jwks_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def get_signing_key(self, kid):
5353

5454
if not signing_key:
5555
raise PyJWKClientError(
56-
'Unable to find a signing key that matches: "{}"'.format(kid)
56+
f'Unable to find a signing key that matches: "{kid}"'
5757
)
5858

5959
return signing_key

tests/keys/__init__.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def decode_value(val):
1313

1414

1515
def load_hmac_key():
16-
with open(os.path.join(BASE_PATH, "jwk_hmac.json"), "r") as infile:
16+
with open(os.path.join(BASE_PATH, "jwk_hmac.json")) as infile:
1717
keyobj = json.load(infile)
1818

1919
return base64url_decode(force_bytes(keyobj["k"]))
@@ -31,15 +31,15 @@ def load_hmac_key():
3131
if has_crypto:
3232

3333
def load_rsa_key():
34-
with open(os.path.join(BASE_PATH, "jwk_rsa_key.json"), "r") as infile:
34+
with open(os.path.join(BASE_PATH, "jwk_rsa_key.json")) as infile:
3535
return RSAAlgorithm.from_jwk(infile.read())
3636

3737
def load_rsa_pub_key():
38-
with open(os.path.join(BASE_PATH, "jwk_rsa_pub.json"), "r") as infile:
38+
with open(os.path.join(BASE_PATH, "jwk_rsa_pub.json")) as infile:
3939
return RSAAlgorithm.from_jwk(infile.read())
4040

4141
def load_ec_key():
42-
with open(os.path.join(BASE_PATH, "jwk_ec_key.json"), "r") as infile:
42+
with open(os.path.join(BASE_PATH, "jwk_ec_key.json")) as infile:
4343
keyobj = json.load(infile)
4444

4545
return ec.EllipticCurvePrivateNumbers(
@@ -48,9 +48,7 @@ def load_ec_key():
4848
)
4949

5050
def load_ec_pub_key_p_521():
51-
with open(
52-
os.path.join(BASE_PATH, "jwk_ec_pub_P-521.json"), "r"
53-
) as infile:
51+
with open(os.path.join(BASE_PATH, "jwk_ec_pub_P-521.json")) as infile:
5452
keyobj = json.load(infile)
5553

5654
return ec.EllipticCurvePublicNumbers(

tests/test_algorithms.py

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -80,41 +80,41 @@ def test_hmac_should_throw_exception_if_key_is_pem_public_key(self):
8080
algo = HMACAlgorithm(HMACAlgorithm.SHA256)
8181

8282
with pytest.raises(InvalidKeyError):
83-
with open(key_path("testkey2_rsa.pub.pem"), "r") as keyfile:
83+
with open(key_path("testkey2_rsa.pub.pem")) as keyfile:
8484
algo.prepare_key(keyfile.read())
8585

8686
def test_hmac_should_throw_exception_if_key_is_x509_certificate(self):
8787
algo = HMACAlgorithm(HMACAlgorithm.SHA256)
8888

8989
with pytest.raises(InvalidKeyError):
90-
with open(key_path("testkey_rsa.cer"), "r") as keyfile:
90+
with open(key_path("testkey_rsa.cer")) as keyfile:
9191
algo.prepare_key(keyfile.read())
9292

9393
def test_hmac_should_throw_exception_if_key_is_ssh_public_key(self):
9494
algo = HMACAlgorithm(HMACAlgorithm.SHA256)
9595

9696
with pytest.raises(InvalidKeyError):
97-
with open(key_path("testkey_rsa.pub"), "r") as keyfile:
97+
with open(key_path("testkey_rsa.pub")) as keyfile:
9898
algo.prepare_key(keyfile.read())
9999

100100
def test_hmac_should_throw_exception_if_key_is_x509_cert(self):
101101
algo = HMACAlgorithm(HMACAlgorithm.SHA256)
102102

103103
with pytest.raises(InvalidKeyError):
104-
with open(key_path("testkey2_rsa.pub.pem"), "r") as keyfile:
104+
with open(key_path("testkey2_rsa.pub.pem")) as keyfile:
105105
algo.prepare_key(keyfile.read())
106106

107107
def test_hmac_should_throw_exception_if_key_is_pkcs1_pem_public(self):
108108
algo = HMACAlgorithm(HMACAlgorithm.SHA256)
109109

110110
with pytest.raises(InvalidKeyError):
111-
with open(key_path("testkey_pkcs1.pub.pem"), "r") as keyfile:
111+
with open(key_path("testkey_pkcs1.pub.pem")) as keyfile:
112112
algo.prepare_key(keyfile.read())
113113

114114
def test_hmac_jwk_should_parse_and_verify(self):
115115
algo = HMACAlgorithm(HMACAlgorithm.SHA256)
116116

117-
with open(key_path("jwk_hmac.json"), "r") as keyfile:
117+
with open(key_path("jwk_hmac.json")) as keyfile:
118118
key = algo.from_jwk(keyfile.read())
119119

120120
signature = algo.sign(b"Hello World!", key)
@@ -129,7 +129,7 @@ def test_hmac_to_jwk_returns_correct_values(self):
129129
def test_hmac_from_jwk_should_raise_exception_if_not_hmac_key(self):
130130
algo = HMACAlgorithm(HMACAlgorithm.SHA256)
131131

132-
with open(key_path("jwk_rsa_pub.json"), "r") as keyfile:
132+
with open(key_path("jwk_rsa_pub.json")) as keyfile:
133133
with pytest.raises(InvalidKeyError):
134134
algo.from_jwk(keyfile.read())
135135

@@ -139,7 +139,7 @@ def test_hmac_from_jwk_should_raise_exception_if_not_hmac_key(self):
139139
def test_rsa_should_parse_pem_public_key(self):
140140
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
141141

142-
with open(key_path("testkey2_rsa.pub.pem"), "r") as pem_key:
142+
with open(key_path("testkey2_rsa.pub.pem")) as pem_key:
143143
algo.prepare_key(pem_key.read())
144144

145145
@pytest.mark.skipif(
@@ -157,7 +157,7 @@ def test_rsa_should_accept_pem_private_key_bytes(self):
157157
def test_rsa_should_accept_unicode_key(self):
158158
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
159159

160-
with open(key_path("testkey_rsa.priv"), "r") as rsa_key:
160+
with open(key_path("testkey_rsa.priv")) as rsa_key:
161161
algo.prepare_key(force_unicode(rsa_key.read()))
162162

163163
@pytest.mark.skipif(
@@ -190,7 +190,7 @@ def test_rsa_verify_should_return_false_if_signature_invalid(self):
190190

191191
sig += force_bytes("123") # Signature is now invalid
192192

193-
with open(key_path("testkey_rsa.pub"), "r") as keyfile:
193+
with open(key_path("testkey_rsa.pub")) as keyfile:
194194
pub_key = algo.prepare_key(keyfile.read())
195195

196196
result = algo.verify(message, pub_key, sig)
@@ -208,14 +208,10 @@ def test_ec_jwk_public_and_private_keys_should_parse_and_verify(self):
208208
for (curve, hash) in tests.items():
209209
algo = ECAlgorithm(hash)
210210

211-
with open(
212-
key_path("jwk_ec_pub_{}.json".format(curve)), "r"
213-
) as keyfile:
211+
with open(key_path(f"jwk_ec_pub_{curve}.json")) as keyfile:
214212
pub_key = algo.from_jwk(keyfile.read())
215213

216-
with open(
217-
key_path("jwk_ec_key_{}.json".format(curve)), "r"
218-
) as keyfile:
214+
with open(key_path(f"jwk_ec_key_{curve}.json")) as keyfile:
219215
priv_key = algo.from_jwk(keyfile.read())
220216

221217
signature = algo.sign(force_bytes("Hello World!"), priv_key)
@@ -290,10 +286,10 @@ def test_ec_jwk_fails_on_invalid_json(self):
290286
def test_rsa_jwk_public_and_private_keys_should_parse_and_verify(self):
291287
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
292288

293-
with open(key_path("jwk_rsa_pub.json"), "r") as keyfile:
289+
with open(key_path("jwk_rsa_pub.json")) as keyfile:
294290
pub_key = algo.from_jwk(keyfile.read())
295291

296-
with open(key_path("jwk_rsa_key.json"), "r") as keyfile:
292+
with open(key_path("jwk_rsa_key.json")) as keyfile:
297293
priv_key = algo.from_jwk(keyfile.read())
298294

299295
signature = algo.sign(force_bytes("Hello World!"), priv_key)
@@ -305,7 +301,7 @@ def test_rsa_jwk_public_and_private_keys_should_parse_and_verify(self):
305301
def test_rsa_private_key_to_jwk_works_with_from_jwk(self):
306302
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
307303

308-
with open(key_path("testkey_rsa.priv"), "r") as rsa_key:
304+
with open(key_path("testkey_rsa.priv")) as rsa_key:
309305
orig_key = algo.prepare_key(force_unicode(rsa_key.read()))
310306

311307
parsed_key = algo.from_jwk(algo.to_jwk(orig_key))
@@ -321,7 +317,7 @@ def test_rsa_private_key_to_jwk_works_with_from_jwk(self):
321317
def test_rsa_public_key_to_jwk_works_with_from_jwk(self):
322318
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
323319

324-
with open(key_path("testkey_rsa.pub"), "r") as rsa_key:
320+
with open(key_path("testkey_rsa.pub")) as rsa_key:
325321
orig_key = algo.prepare_key(force_unicode(rsa_key.read()))
326322

327323
parsed_key = algo.from_jwk(algo.to_jwk(orig_key))
@@ -333,7 +329,7 @@ def test_rsa_public_key_to_jwk_works_with_from_jwk(self):
333329
def test_rsa_jwk_private_key_with_other_primes_is_invalid(self):
334330
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
335331

336-
with open(key_path("jwk_rsa_key.json"), "r") as keyfile:
332+
with open(key_path("jwk_rsa_key.json")) as keyfile:
337333
with pytest.raises(InvalidKeyError):
338334
keydata = json.loads(keyfile.read())
339335
keydata["oth"] = []
@@ -346,7 +342,7 @@ def test_rsa_jwk_private_key_with_other_primes_is_invalid(self):
346342
def test_rsa_jwk_private_key_with_missing_values_is_invalid(self):
347343
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
348344

349-
with open(key_path("jwk_rsa_key.json"), "r") as keyfile:
345+
with open(key_path("jwk_rsa_key.json")) as keyfile:
350346
with pytest.raises(InvalidKeyError):
351347
keydata = json.loads(keyfile.read())
352348
del keydata["p"]
@@ -359,7 +355,7 @@ def test_rsa_jwk_private_key_with_missing_values_is_invalid(self):
359355
def test_rsa_jwk_private_key_can_recover_prime_factors(self):
360356
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
361357

362-
with open(key_path("jwk_rsa_key.json"), "r") as keyfile:
358+
with open(key_path("jwk_rsa_key.json")) as keyfile:
363359
keybytes = keyfile.read()
364360
control_key = algo.from_jwk(keybytes).private_numbers()
365361

@@ -383,7 +379,7 @@ def test_rsa_jwk_private_key_can_recover_prime_factors(self):
383379
def test_rsa_jwk_private_key_with_missing_required_values_is_invalid(self):
384380
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
385381

386-
with open(key_path("jwk_rsa_key.json"), "r") as keyfile:
382+
with open(key_path("jwk_rsa_key.json")) as keyfile:
387383
with pytest.raises(InvalidKeyError):
388384
keydata = json.loads(keyfile.read())
389385
del keydata["p"]
@@ -410,7 +406,7 @@ def test_rsa_jwk_raises_exception_if_not_a_valid_key(self):
410406
def test_rsa_to_jwk_returns_correct_values_for_public_key(self):
411407
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
412408

413-
with open(key_path("testkey_rsa.pub"), "r") as keyfile:
409+
with open(key_path("testkey_rsa.pub")) as keyfile:
414410
pub_key = algo.prepare_key(keyfile.read())
415411

416412
key = algo.to_jwk(pub_key)
@@ -436,7 +432,7 @@ def test_rsa_to_jwk_returns_correct_values_for_public_key(self):
436432
def test_rsa_to_jwk_returns_correct_values_for_private_key(self):
437433
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
438434

439-
with open(key_path("testkey_rsa.priv"), "r") as keyfile:
435+
with open(key_path("testkey_rsa.priv")) as keyfile:
440436
priv_key = algo.prepare_key(keyfile.read())
441437

442438
key = algo.to_jwk(priv_key)
@@ -504,7 +500,7 @@ def test_rsa_to_jwk_raises_exception_on_invalid_key(self):
504500
def test_rsa_from_jwk_raises_exception_on_invalid_key(self):
505501
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
506502

507-
with open(key_path("jwk_hmac.json"), "r") as keyfile:
503+
with open(key_path("jwk_hmac.json")) as keyfile:
508504
with pytest.raises(InvalidKeyError):
509505
algo.from_jwk(keyfile.read())
510506

@@ -532,7 +528,7 @@ def test_ec_should_accept_pem_private_key_bytes(self):
532528
def test_ec_should_accept_ssh_public_key_bytes(self):
533529
algo = ECAlgorithm(ECAlgorithm.SHA256)
534530

535-
with open(key_path("testkey_ec_ssh.pub"), "r") as ec_key:
531+
with open(key_path("testkey_ec_ssh.pub")) as ec_key:
536532
algo.prepare_key(ec_key.read())
537533

538534
@pytest.mark.skipif(
@@ -554,7 +550,7 @@ def test_ec_verify_should_return_false_if_signature_invalid(self):
554550
)
555551
)
556552

557-
with open(key_path("testkey_ec.pub"), "r") as keyfile:
553+
with open(key_path("testkey_ec.pub")) as keyfile:
558554
pub_key = algo.prepare_key(keyfile.read())
559555

560556
result = algo.verify(message, pub_key, sig)
@@ -570,7 +566,7 @@ def test_ec_verify_should_return_false_if_signature_wrong_length(self):
570566

571567
sig = base64.b64decode(force_bytes("AC+m4Jf/xI3guAC6w0w3"))
572568

573-
with open(key_path("testkey_ec.pub"), "r") as keyfile:
569+
with open(key_path("testkey_ec.pub")) as keyfile:
574570
pub_key = algo.prepare_key(keyfile.read())
575571

576572
result = algo.verify(message, pub_key, sig)
@@ -584,11 +580,11 @@ def test_rsa_pss_sign_then_verify_should_return_true(self):
584580

585581
message = force_bytes("Hello World!")
586582

587-
with open(key_path("testkey_rsa.priv"), "r") as keyfile:
583+
with open(key_path("testkey_rsa.priv")) as keyfile:
588584
priv_key = algo.prepare_key(keyfile.read())
589585
sig = algo.sign(message, priv_key)
590586

591-
with open(key_path("testkey_rsa.pub"), "r") as keyfile:
587+
with open(key_path("testkey_rsa.pub")) as keyfile:
592588
pub_key = algo.prepare_key(keyfile.read())
593589

594590
result = algo.verify(message, pub_key, sig)
@@ -615,7 +611,7 @@ def test_rsa_pss_verify_should_return_false_if_signature_invalid(self):
615611

616612
jwt_sig += force_bytes("123") # Signature is now invalid
617613

618-
with open(key_path("testkey_rsa.pub"), "r") as keyfile:
614+
with open(key_path("testkey_rsa.pub")) as keyfile:
619615
jwt_pub_key = algo.prepare_key(keyfile.read())
620616

621617
result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)

tests/test_api_jwk.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class TestPyJWK:
2222
def test_should_load_key_from_jwk_data_dict(self):
2323
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
2424

25-
with open(key_path("jwk_rsa_pub.json"), "r") as keyfile:
25+
with open(key_path("jwk_rsa_pub.json")) as keyfile:
2626
pub_key = algo.from_jwk(keyfile.read())
2727

2828
key_data_str = algo.to_jwk(pub_key)
@@ -46,7 +46,7 @@ def test_should_load_key_from_jwk_data_dict(self):
4646
def test_should_load_key_from_jwk_data_json_string(self):
4747
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
4848

49-
with open(key_path("jwk_rsa_pub.json"), "r") as keyfile:
49+
with open(key_path("jwk_rsa_pub.json")) as keyfile:
5050
pub_key = algo.from_jwk(keyfile.read())
5151

5252
key_data_str = algo.to_jwk(pub_key)
@@ -72,7 +72,7 @@ class TestPyJWKSet:
7272
def test_should_load_keys_from_jwk_data_dict(self):
7373
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
7474

75-
with open(key_path("jwk_rsa_pub.json"), "r") as keyfile:
75+
with open(key_path("jwk_rsa_pub.json")) as keyfile:
7676
pub_key = algo.from_jwk(keyfile.read())
7777

7878
key_data_str = algo.to_jwk(pub_key)
@@ -97,7 +97,7 @@ def test_should_load_keys_from_jwk_data_dict(self):
9797
def test_should_load_keys_from_jwk_data_json_string(self):
9898
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
9999

100-
with open(key_path("jwk_rsa_pub.json"), "r") as keyfile:
100+
with open(key_path("jwk_rsa_pub.json")) as keyfile:
101101
pub_key = algo.from_jwk(keyfile.read())
102102

103103
key_data_str = algo.to_jwk(pub_key)

0 commit comments

Comments
 (0)