Skip to content

Commit bbc4b62

Browse files
committed
lint
1 parent dc40590 commit bbc4b62

File tree

10 files changed

+48
-50
lines changed

10 files changed

+48
-50
lines changed

src/cryptojwt/jwe/jwe.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import contextlib
12
import logging
23

34
from ..jwk.asym import AsymmetricKey
@@ -157,10 +158,8 @@ def decrypt(self, token=None, keys=None, alg=None, cek=None):
157158
else:
158159
keys = self.pick_keys(self._get_keys(), use="enc", alg=_alg)
159160

160-
try:
161+
with contextlib.suppress(KeyError):
161162
keys.append(key_from_jwk_dict(_jwe.headers["jwk"]))
162-
except KeyError:
163-
pass
164163

165164
if not keys and not cek:
166165
raise NoSuitableDecryptionKey(_alg)

src/cryptojwt/jwe/jwe_ec.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import contextlib
12
import struct
23

34
from cryptography.hazmat.primitives.asymmetric import ec
@@ -190,10 +191,8 @@ def encrypt(self, key=None, iv="", cek="", **kwargs):
190191
_msg = as_bytes(self.msg)
191192

192193
_args = self._dict
193-
try:
194+
with contextlib.suppress(KeyError):
194195
_args["kid"] = kwargs["kid"]
195-
except KeyError:
196-
pass
197196

198197
if "params" in kwargs:
199198
if "apu" in kwargs["params"]:

src/cryptojwt/jwe/jwe_hmac.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import contextlib
12
import logging
23
import zlib
34

@@ -34,10 +35,8 @@ def encrypt(self, key, iv="", cek="", **kwargs):
3435
_msg = as_bytes(self.msg)
3536

3637
_args = self._dict
37-
try:
38+
with contextlib.suppress(KeyError):
3839
_args["kid"] = kwargs["kid"]
39-
except KeyError:
40-
pass
4140

4241
jwe = JWEnc(**_args)
4342

src/cryptojwt/jwt.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Basic JSON Web Token implementation."""
22

3+
import contextlib
34
import json
45
import logging
56
import time
@@ -143,10 +144,8 @@ def receivers(self):
143144
def my_keys(self, issuer_id="", use="sig"):
144145
_k = self.key_jar.get(use, issuer_id=issuer_id)
145146
if issuer_id != "":
146-
try:
147+
with contextlib.suppress(KeyError):
147148
_k.extend(self.key_jar.get(use, issuer_id=""))
148-
except KeyError:
149-
pass
150149
return _k
151150

152151
def _encrypt(self, payload, recv, cty="JWT", zip=""):
@@ -442,9 +441,7 @@ def remove_jwt_parameters(arg):
442441
"""
443442

444443
for param in JWT.jwt_parameters:
445-
try:
444+
with contextlib.suppress(KeyError):
446445
del arg[param]
447-
except KeyError:
448-
pass
449446

450447
return arg

src/cryptojwt/key_bundle.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Implementation of a Key Bundle."""
22

3+
import contextlib
34
import copy
45
import json
56
import logging
@@ -8,7 +9,8 @@
89
import time
910
from datetime import datetime
1011
from functools import cmp_to_key
11-
from typing import List, Optional
12+
from typing import List
13+
from typing import Optional
1214

1315
import requests
1416

@@ -17,19 +19,23 @@
1719
from cryptojwt.jwk.okp import OKP_CRV2PUBLIC
1820
from cryptojwt.jwk.x509 import import_private_key_from_pem_file
1921

20-
from .exception import (
21-
JWKException,
22-
UnknownKeyType,
23-
UnsupportedAlgorithm,
24-
UnsupportedECurve,
25-
UpdateFailed,
26-
)
27-
from .jwk.ec import ECKey, new_ec_key
22+
from .exception import JWKException
23+
from .exception import UnknownKeyType
24+
from .exception import UnsupportedAlgorithm
25+
from .exception import UnsupportedECurve
26+
from .exception import UpdateFailed
27+
from .jwk.ec import ECKey
28+
from .jwk.ec import new_ec_key
2829
from .jwk.hmac import SYMKey
29-
from .jwk.jwk import dump_jwk, import_jwk
30-
from .jwk.okp import OKPKey, new_okp_key
31-
from .jwk.rsa import RSAKey, new_rsa_key
32-
from .utils import as_unicode, check_content_type, httpc_params_loader
30+
from .jwk.jwk import dump_jwk
31+
from .jwk.jwk import import_jwk
32+
from .jwk.okp import OKPKey
33+
from .jwk.okp import new_okp_key
34+
from .jwk.rsa import RSAKey
35+
from .jwk.rsa import new_rsa_key
36+
from .utils import as_unicode
37+
from .utils import check_content_type
38+
from .utils import httpc_params_loader
3339

3440
__author__ = "Roland Hedberg"
3541

@@ -685,10 +691,8 @@ def remove(self, key):
685691
686692
:param key: The key that should be removed
687693
"""
688-
try:
694+
with contextlib.suppress(ValueError):
689695
self._keys.remove(key)
690-
except ValueError:
691-
pass
692696

693697
def __len__(self):
694698
"""

src/cryptojwt/key_issuer.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import json
22
import logging
33
import os
4-
from typing import List, Optional
4+
from typing import List
5+
from typing import Optional
56

67
from requests import request
78

89
from .jwe.utils import alg2keytype as jwe_alg2keytype
910
from .jws.utils import alg2keytype as jws_alg2keytype
10-
from .key_bundle import KeyBundle, build_key_bundle, key_diff, update_key_bundle
11-
from .utils import httpc_params_loader, importer, qualified_name
11+
from .key_bundle import KeyBundle
12+
from .key_bundle import build_key_bundle
13+
from .key_bundle import key_diff
14+
from .key_bundle import update_key_bundle
15+
from .utils import httpc_params_loader
16+
from .utils import importer
17+
from .utils import qualified_name
1218

1319
__author__ = "Roland Hedberg"
1420

src/cryptojwt/key_jar.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import contextlib
12
import json
23
import logging
34
from typing import List
@@ -669,10 +670,8 @@ def dump(
669670

670671
if exclude_attributes:
671672
for attr in exclude_attributes:
672-
try:
673+
with contextlib.suppress(KeyError):
673674
del info[attr]
674-
except KeyError:
675-
pass
676675

677676
if exclude_attributes is None or "issuers" not in exclude_attributes:
678677
info["issuers"] = self._dump_issuers(

src/cryptojwt/simple_jwt.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import contextlib
12
import json
23
import logging
34

@@ -36,10 +37,8 @@ def unpack(self, token, **kwargs):
3637
against.
3738
"""
3839
if isinstance(token, str):
39-
try:
40+
with contextlib.suppress(UnicodeDecodeError):
4041
token = token.encode("utf-8")
41-
except UnicodeDecodeError:
42-
pass
4342

4443
part = split_token(token)
4544
self.b64part = part
@@ -98,10 +97,8 @@ def payload(self):
9897
if "cty" in self.headers and self.headers["cty"].lower() != "jwt":
9998
pass
10099
else:
101-
try:
100+
with contextlib.suppress(ValueError):
102101
_msg = json.loads(_msg)
103-
except ValueError:
104-
pass
105102

106103
return _msg
107104

src/cryptojwt/tools/jwtpeek.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
from cryptojwt.jwe import jwe
1414
from cryptojwt.jwk.hmac import SYMKey
1515
from cryptojwt.jwk.jwk import key_from_jwk_dict
16-
from cryptojwt.jwk.rsa import RSAKey, import_rsa_key
16+
from cryptojwt.jwk.rsa import RSAKey
17+
from cryptojwt.jwk.rsa import import_rsa_key
1718
from cryptojwt.jws import jws
1819
from cryptojwt.key_bundle import KeyBundle
1920
from cryptojwt.key_issuer import KeyIssuer

src/cryptojwt/utils.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import base64
2+
import contextlib
23
import functools
34
import importlib
45
import json
@@ -140,10 +141,8 @@ def as_bytes(s):
140141
:param s: Unicode / bytes string
141142
:return: bytes string
142143
"""
143-
try:
144+
with contextlib.suppress(AttributeError, UnicodeDecodeError):
144145
s = s.encode()
145-
except (AttributeError, UnicodeDecodeError):
146-
pass
147146
return s
148147

149148

@@ -154,10 +153,8 @@ def as_unicode(b):
154153
:param b: byte string
155154
:return: unicode string
156155
"""
157-
try:
156+
with contextlib.suppress(AttributeError, UnicodeDecodeError):
158157
b = b.decode()
159-
except (AttributeError, UnicodeDecodeError):
160-
pass
161158
return b
162159

163160

0 commit comments

Comments
 (0)