Skip to content

Commit 2a5a219

Browse files
committed
lint
1 parent d379654 commit 2a5a219

10 files changed

+42
-44
lines changed

tests/test_02_jwk.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ def _eq(l1, l2):
6868

6969

7070
def test_urlsafe_base64decode():
71-
l = base64_to_long(N)
71+
length = base64_to_long(N)
7272
# convert it to base64
73-
bys = long2intarr(l)
74-
data = struct.pack("%sB" % len(bys), *bys)
73+
bys = long2intarr(length)
74+
data = struct.pack(f"{len(bys)}B", *bys)
7575
if not len(data):
7676
data = "\x00"
7777
s0 = base64.b64encode(data)
@@ -81,8 +81,8 @@ def test_urlsafe_base64decode():
8181
base64url_to_long(s0)
8282

8383
# Not else, should not raise exception
84-
l = base64_to_long(s0)
85-
assert l
84+
length = base64_to_long(s0)
85+
assert length
8686

8787

8888
def test_import_rsa_key_from_cert_file():
@@ -246,7 +246,8 @@ def test_get_key():
246246
def test_private_rsa_key_from_jwk():
247247
keys = []
248248

249-
kspec = json.loads(open(full_path("jwk_private_key.json")).read())
249+
with open(full_path("jwk_private_key.json")) as fp:
250+
kspec = json.loads(fp.read())
250251
keys.append(key_from_jwk_dict(kspec))
251252

252253
key = keys[0]
@@ -272,7 +273,8 @@ def test_private_rsa_key_from_jwk():
272273
def test_public_key_from_jwk():
273274
keys = []
274275

275-
kspec = json.loads(open(full_path("jwk_private_key.json")).read())
276+
with open(full_path("jwk_private_key.json")) as fp:
277+
kspec = json.loads(fp.read())
276278
keys.append(key_from_jwk_dict(kspec, private=False))
277279

278280
key = keys[0]
@@ -288,7 +290,8 @@ def test_public_key_from_jwk():
288290
def test_ec_private_key_from_jwk():
289291
keys = []
290292

291-
kspec = json.loads(open(full_path("jwk_private_ec_key.json")).read())
293+
with open(full_path("jwk_private_ec_key.json")) as fp:
294+
kspec = json.loads(fp.read())
292295
keys.append(key_from_jwk_dict(kspec))
293296

294297
key = keys[0]
@@ -306,7 +309,8 @@ def test_ec_private_key_from_jwk():
306309
def test_ec_public_key_from_jwk():
307310
keys = []
308311

309-
kspec = json.loads(open(full_path("jwk_private_ec_key.json")).read())
312+
with open(full_path("jwk_private_ec_key.json")) as fp:
313+
kspec = json.loads(fp.read())
310314
keys.append(key_from_jwk_dict(kspec, private=False))
311315

312316
key = keys[0]

tests/test_03_key_bundle.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# pylint: disable=missing-docstring,no-self-use
2+
import contextlib
23
import json
34
import os
45
import shutil
@@ -408,7 +409,7 @@ def test_mark_as_inactive():
408409
desc = {"kty": "oct", "key": "highestsupersecret", "use": "sig"}
409410
kb = KeyBundle([desc])
410411
assert len(kb.keys()) == 1
411-
for k in kb.keys():
412+
for k in kb.keys(): # noqa
412413
kb.mark_as_inactive(k.kid)
413414
desc = {"kty": "oct", "key": "highestsupersecret", "use": "enc"}
414415
kb.add_jwk_dicts([desc])
@@ -420,7 +421,7 @@ def test_copy():
420421
desc = {"kty": "oct", "key": "highestsupersecret", "use": "sig"}
421422
kb = KeyBundle([desc])
422423
assert len(kb.keys()) == 1
423-
for k in kb.keys():
424+
for k in kb.keys(): # noqa
424425
kb.mark_as_inactive(k.kid)
425426
desc = {"kty": "oct", "key": "highestsupersecret", "use": "enc"}
426427
kb.add_jwk_dicts([desc])
@@ -482,7 +483,7 @@ def test_httpc_params_1():
482483
httpc_params = {"timeout": (2, 2)} # connect, read timeouts in seconds
483484
kb = KeyBundle(source=source, httpc=requests.request, httpc_params=httpc_params)
484485
updated, _ = kb._do_remote()
485-
assert updated == True
486+
assert updated is True
486487

487488

488489
@pytest.mark.network
@@ -908,7 +909,7 @@ def test_export_inactive():
908909
desc = {"kty": "oct", "key": "highestsupersecret", "use": "sig"}
909910
kb = KeyBundle([desc])
910911
assert len(kb.keys()) == 1
911-
for k in kb.keys():
912+
for k in kb.keys(): # noqa
912913
kb.mark_as_inactive(k.kid)
913914
desc = {"kty": "oct", "key": "highestsupersecret", "use": "enc"}
914915
kb.add_jwk_dicts([desc])
@@ -976,7 +977,7 @@ def test_remote_not_modified():
976977
with responses.RequestsMock() as rsps:
977978
rsps.add(method="GET", url=source, json=JWKS_DICT, status=200, headers=headers)
978979
updated, _ = kb._do_remote()
979-
assert updated == True
980+
assert updated is True
980981
assert kb.last_remote == headers.get("Last-Modified")
981982
timeout1 = kb.time_out
982983

@@ -1019,28 +1020,26 @@ def test_ignore_errors_period():
10191020
ignore_errors_period=ignore_errors_period,
10201021
)
10211022
res, _ = kb._do_remote()
1022-
assert res == True
1023+
assert res is True
10231024
assert kb.ignore_errors_until is None
10241025

10251026
# refetch, but fail by using a bad source
10261027
kb.source = source_bad
1027-
try:
1028+
with contextlib.suppress(UpdateFailed):
10281029
res, _ = kb._do_remote()
1029-
except UpdateFailed:
1030-
pass
10311030

10321031
# retry should fail silently as we're in holddown
10331032
res, _ = kb._do_remote()
10341033
assert kb.ignore_errors_until is not None
1035-
assert res == False
1034+
assert res is False
10361035

10371036
# wait until holddown
10381037
time.sleep(ignore_errors_period + 1)
10391038

10401039
# try again
10411040
kb.source = source_good
10421041
res, _ = kb._do_remote()
1043-
assert res == True
1042+
assert res is True
10441043

10451044

10461045
def test_ignore_invalid_keys():

tests/test_04_key_issuer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def test_get_enc_not_mine(self):
287287
assert issuer.get("enc", "oct")
288288

289289
def test_dump_issuer_keys(self):
290-
kb = keybundle_from_local_file("file://%s/jwk.json" % BASE_PATH, "jwks", ["sig"])
290+
kb = keybundle_from_local_file(f"file://{BASE_PATH}/jwk.json", "jwks", ["sig"])
291291
assert len(kb) == 1
292292
issuer = KeyIssuer()
293293
issuer.add_kb(kb)
@@ -701,7 +701,7 @@ def test_localhost_url():
701701
kb = issuer.find(url)
702702
assert len(kb) == 1
703703
assert "verify" in kb[0].httpc_params
704-
assert kb[0].httpc_params["verify"] == False
704+
assert kb[0].httpc_params["verify"] is False
705705

706706

707707
def test_add_url():

tests/test_04_key_jar.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def test_get_enc_not_mine(self):
370370
assert ks.get("enc", "oct", "http://www.example.org/")
371371

372372
def test_dump_issuer_keys(self):
373-
kb = keybundle_from_local_file("file://%s/jwk.json" % BASE_PATH, "jwks", ["sig"])
373+
kb = keybundle_from_local_file(f"file://{BASE_PATH}/jwk.json", "jwks", ["sig"])
374374
assert len(kb) == 1
375375
kj = KeyJar()
376376
kj.add_kb("", kb)
@@ -405,14 +405,11 @@ def test_no_use(self):
405405
def test_provider(self):
406406
kj = KeyJar()
407407
_url = "https://connect-op.herokuapp.com/jwks.json"
408-
kj.load_keys(
409-
"https://connect-op.heroku.com",
410-
jwks_uri=_url,
411-
)
408+
kj.load_keys("https://connect-op.heroku.com", jwks_uri=_url)
412409
iss_keys = kj.get_issuer_keys("https://connect-op.heroku.com")
413410
if not iss_keys:
414411
_msg = f"{_url} is not available at this moment!"
415-
warnings.warn(_msg)
412+
warnings.warn(_msg, stacklevel=1)
416413
else:
417414
assert iss_keys[0].keys()
418415

tests/test_05_jwx.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def test_jws_set_jku():
5050

5151

5252
def test_jwx_set_x5c():
53-
jwx = JWx(x5c=open(full_path("cert.pem")).read())
53+
with open(full_path("cert.pem")) as fp:
54+
jwx = JWx(x5c=fp.read())
5455
keys = jwx._get_keys()
5556
assert len(keys)
5657
assert isinstance(keys[0], RSAKey)

tests/test_06_jws.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ def test_signer_ps256_fail():
572572
except BadSignature:
573573
pass
574574
else:
575-
assert False
575+
raise AssertionError
576576

577577

578578
def test_signer_ps384():
@@ -645,11 +645,11 @@ def test_signer_eddsa_fail():
645645
_pubkey = OKPKey().load_key(okp2.public_key())
646646
_rj = JWS(alg="Ed25519")
647647
try:
648-
info = _rj.verify_compact(_jwt, [_pubkey])
648+
_ = _rj.verify_compact(_jwt, [_pubkey])
649649
except BadSignature:
650650
pass
651651
else:
652-
assert False
652+
raise AssertionError
653653

654654

655655
def test_no_alg_and_alg_none_same():
@@ -904,7 +904,7 @@ def test_rs256_rm_signature():
904904
except WrongNumberOfParts:
905905
pass
906906
else:
907-
assert False
907+
raise AssertionError
908908

909909

910910
def test_pick_alg_assume_alg_from_single_key():

tests/test_07_jwe.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,7 @@ def str2intarr(string):
6262
return array.array("B", string).tolist()
6363

6464

65-
if sys.version < "3":
66-
to_intarr = str2intarr
67-
else:
68-
to_intarr = bytes2intarr
65+
to_intarr = str2intarr if sys.version < "3" else bytes2intarr
6966

7067

7168
def test_jwe_09_a1():
@@ -671,9 +668,9 @@ def test_fernet_symkey():
671668

672669
def test_fernet_bad():
673670
with pytest.raises(TypeError):
674-
encrypter = FernetEncrypter(key="xyzzy")
671+
_ = FernetEncrypter(key="xyzzy")
675672
with pytest.raises(ValueError):
676-
encrypter = FernetEncrypter(key=os.urandom(16))
673+
_ = FernetEncrypter(key=os.urandom(16))
677674

678675

679676
def test_fernet_bytes():

tests/test_09_jwt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def test_jwt_pack_and_unpack_unknown_key():
156156
kj.add_kb(ALICE, KeyBundle())
157157
bob = JWT(key_jar=kj, iss=BOB, allowed_sign_algs=["RS256"])
158158
with pytest.raises(NoSuitableSigningKeys):
159-
info = bob.unpack(_jwt)
159+
_ = bob.unpack(_jwt)
160160

161161

162162
def test_jwt_pack_and_unpack_with_lifetime():

tests/test_31_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33

44
def test_check_content_type():
5-
assert check_content_type(content_type="application/json", mime_type="application/json") == True
5+
assert check_content_type(content_type="application/json", mime_type="application/json") is True
66
assert (
77
check_content_type(
88
content_type="application/json; charset=utf-8", mime_type="application/json"
99
)
10-
== True
10+
is True
1111
)
1212
assert (
1313
check_content_type(
1414
content_type="application/html; charset=utf-8", mime_type="application/json"
1515
)
16-
== False
16+
is False
1717
)

tests/test_40_serialize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def full_path(local_file):
1515

1616

1717
def test_key_issuer():
18-
kb = keybundle_from_local_file("file://%s/jwk.json" % BASE_PATH, "jwks", ["sig"])
18+
kb = keybundle_from_local_file(f"file://{BASE_PATH}/jwk.json", "jwks", ["sig"])
1919
assert len(kb) == 1
2020
issuer = KeyIssuer()
2121
issuer.add(kb)

0 commit comments

Comments
 (0)