Skip to content

Commit 541dd0d

Browse files
authored
Prefer ModuleNotFoundError over ImportError (jpadilla#565)
ModuleNotFoundError was introduced in Python 3. It is raised when the module does not exist. On the other hand, ImportError is raised during any import failure. For example, a syntax error or other runtime error. Using ModuleNotFoundError means that errors unrelated to a missing package will be propagated to the user. PyJWT doesn't know how to handle these. This also allows more functions to always be available for import
1 parent b0c1e60 commit 541dd0d

File tree

7 files changed

+38
-41
lines changed

7 files changed

+38
-41
lines changed

jwt/algorithms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
)
4444

4545
has_crypto = True
46-
except ImportError:
46+
except ModuleNotFoundError:
4747
has_crypto = False
4848

4949
requires_cryptography = {

jwt/help.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
try:
88
import cryptography
9-
except ImportError:
9+
except ModuleNotFoundError:
1010
cryptography = None # type: ignore
1111

1212

jwt/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
decode_dss_signature,
99
encode_dss_signature,
1010
)
11-
except ImportError:
11+
except ModuleNotFoundError:
1212
EllipticCurve = Any # type: ignore
1313

1414

tests/keys/__init__.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
from jwt.algorithms import has_crypto
55
from jwt.utils import base64url_decode
66

7+
try:
8+
from cryptography.hazmat.primitives.asymmetric import ec
9+
except ModuleNotFoundError:
10+
pass
11+
12+
if has_crypto:
13+
from jwt.algorithms import RSAAlgorithm
14+
715
BASE_PATH = os.path.dirname(os.path.abspath(__file__))
816

917

@@ -19,38 +27,32 @@ def load_hmac_key():
1927
return base64url_decode(keyobj["k"])
2028

2129

22-
try:
23-
from cryptography.hazmat.primitives.asymmetric import ec
30+
def load_rsa_key():
31+
with open(os.path.join(BASE_PATH, "jwk_rsa_key.json")) as infile:
32+
return RSAAlgorithm.from_jwk(infile.read())
2433

25-
from jwt.algorithms import RSAAlgorithm
26-
except ImportError:
27-
pass
2834

29-
if has_crypto:
35+
def load_rsa_pub_key():
36+
with open(os.path.join(BASE_PATH, "jwk_rsa_pub.json")) as infile:
37+
return RSAAlgorithm.from_jwk(infile.read())
3038

31-
def load_rsa_key():
32-
with open(os.path.join(BASE_PATH, "jwk_rsa_key.json")) as infile:
33-
return RSAAlgorithm.from_jwk(infile.read())
3439

35-
def load_rsa_pub_key():
36-
with open(os.path.join(BASE_PATH, "jwk_rsa_pub.json")) as infile:
37-
return RSAAlgorithm.from_jwk(infile.read())
40+
def load_ec_key():
41+
with open(os.path.join(BASE_PATH, "jwk_ec_key.json")) as infile:
42+
keyobj = json.load(infile)
3843

39-
def load_ec_key():
40-
with open(os.path.join(BASE_PATH, "jwk_ec_key.json")) as infile:
41-
keyobj = json.load(infile)
44+
return ec.EllipticCurvePrivateNumbers(
45+
private_value=decode_value(keyobj["d"]),
46+
public_numbers=load_ec_pub_key_p_521().public_numbers(),
47+
)
4248

43-
return ec.EllipticCurvePrivateNumbers(
44-
private_value=decode_value(keyobj["d"]),
45-
public_numbers=load_ec_pub_key_p_521().public_numbers(),
46-
)
4749

48-
def load_ec_pub_key_p_521():
49-
with open(os.path.join(BASE_PATH, "jwk_ec_pub_P-521.json")) as infile:
50-
keyobj = json.load(infile)
50+
def load_ec_pub_key_p_521():
51+
with open(os.path.join(BASE_PATH, "jwk_ec_pub_P-521.json")) as infile:
52+
keyobj = json.load(infile)
5153

52-
return ec.EllipticCurvePublicNumbers(
53-
x=decode_value(keyobj["x"]),
54-
y=decode_value(keyobj["y"]),
55-
curve=ec.SECP521R1(),
56-
).public_key()
54+
return ec.EllipticCurvePublicNumbers(
55+
x=decode_value(keyobj["x"]),
56+
y=decode_value(keyobj["y"]),
57+
curve=ec.SECP521R1(),
58+
).public_key()

tests/test_algorithms.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,21 @@
33

44
import pytest
55

6-
from jwt.algorithms import Algorithm, HMACAlgorithm, NoneAlgorithm
6+
from jwt.algorithms import Algorithm, HMACAlgorithm, NoneAlgorithm, has_crypto
77
from jwt.exceptions import InvalidKeyError
88
from jwt.utils import base64url_decode
99

10-
from .keys import load_hmac_key
10+
from .keys import load_ec_pub_key_p_521, load_hmac_key, load_rsa_pub_key
1111
from .utils import crypto_required, key_path
1212

13-
try:
13+
if has_crypto:
1414
from jwt.algorithms import (
1515
ECAlgorithm,
1616
Ed25519Algorithm,
1717
RSAAlgorithm,
1818
RSAPSSAlgorithm,
1919
)
2020

21-
from .keys import load_ec_pub_key_p_521, load_rsa_pub_key
22-
except ImportError:
23-
pass
24-
2521

2622
class TestAlgorithms:
2723
def test_algorithm_should_throw_exception_if_prepare_key_not_impl(self):

tests/test_api_jwk.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import json
22

3+
from jwt.algorithms import has_crypto
34
from jwt.api_jwk import PyJWK, PyJWKSet
45

56
from .utils import crypto_required, key_path
67

7-
try:
8+
if has_crypto:
89
from jwt.algorithms import RSAAlgorithm
9-
except ImportError:
10-
pass
1110

1211

1312
class TestPyJWK:

tests/test_api_jws.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
load_pem_public_key,
2222
load_ssh_public_key,
2323
)
24-
except ImportError:
24+
except ModuleNotFoundError:
2525
pass
2626

2727

0 commit comments

Comments
 (0)