Skip to content

Commit 65bca5c

Browse files
authored
Run pyupgrade across project to use modern Python 3 conventions (jpadilla#491)
pyupgrade is a tool to automatically upgrade Python syntax for newer versions of the language. Running pyupgrade removes several Python-2-isms that are no longer necessary now that the project is Python 3 only. https://github.com/asottile/pyupgrade
1 parent 7b2b4ff commit 65bca5c

File tree

13 files changed

+115
-122
lines changed

13 files changed

+115
-122
lines changed

docs/conf.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# -*- coding: utf-8 -*-
2-
#
31
# PyJWT documentation build configuration file, created by
42
# sphinx-quickstart on Thu Oct 22 18:11:10 2015.
53
#
@@ -51,9 +49,9 @@
5149
master_doc = "index"
5250

5351
# General information about the project.
54-
project = u"PyJWT"
55-
copyright = u"2015, José Padilla"
56-
author = u"José Padilla"
52+
project = "PyJWT"
53+
copyright = "2015, José Padilla"
54+
author = "José Padilla"
5755

5856
# The version info for the project you're documenting, acts as replacement for
5957
# |version| and |release|, also used in various other places throughout the
@@ -244,8 +242,8 @@ def get_version(package):
244242
(
245243
master_doc,
246244
"PyJWT.tex",
247-
u"PyJWT Documentation",
248-
u"José Padilla",
245+
"PyJWT Documentation",
246+
"José Padilla",
249247
"manual",
250248
)
251249
]
@@ -275,7 +273,7 @@ def get_version(package):
275273

276274
# One entry per manual page. List of tuples
277275
# (source start file, name, description, authors, manual section).
278-
man_pages = [(master_doc, "pyjwt", u"PyJWT Documentation", [author], 1)]
276+
man_pages = [(master_doc, "pyjwt", "PyJWT Documentation", [author], 1)]
279277

280278
# If true, show URL addresses after external links.
281279
# man_show_urls = False
@@ -290,7 +288,7 @@ def get_version(package):
290288
(
291289
master_doc,
292290
"PyJWT",
293-
u"PyJWT Documentation",
291+
"PyJWT Documentation",
294292
author,
295293
"PyJWT",
296294
"One line description of project.",

jwt/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
# flake8: noqa
32

43
"""

jwt/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def decode_payload(args):
6868
if sys.stdin.isatty():
6969
token = sys.stdin.readline().strip()
7070
else:
71-
raise IOError("Cannot read from stdin: terminal not a TTY")
71+
raise OSError("Cannot read from stdin: terminal not a TTY")
7272

7373
token = token.encode("utf-8")
7474
data = decode(token, key=args.key, verify=args.verify)

jwt/algorithms.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@
4545
has_crypto = False
4646
has_ed25519 = False
4747

48-
requires_cryptography = set(
49-
[
48+
requires_cryptography = {
5049
"RS256",
5150
"RS384",
5251
"RS512",
@@ -58,8 +57,7 @@
5857
"PS384",
5958
"PS512",
6059
"EdDSA",
61-
]
62-
)
60+
}
6361

6462

6563
def get_default_algorithms():
@@ -104,7 +102,7 @@ def get_default_algorithms():
104102
return default_algorithms
105103

106104

107-
class Algorithm(object):
105+
class Algorithm:
108106
"""
109107
The interface for an algorithm used to sign and verify tokens.
110108
"""

jwt/api_jws.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
pass
2121

2222

23-
class PyJWS(object):
23+
class PyJWS:
2424
header_typ = "JWT"
2525

2626
def __init__(self, algorithms=None, options=None):
@@ -182,7 +182,7 @@ def _load(self, jwt):
182182

183183
if not issubclass(type(jwt), binary_type):
184184
raise DecodeError(
185-
"Invalid token type. Token must be a {0}".format(binary_type)
185+
"Invalid token type. Token must be a {}".format(binary_type)
186186
)
187187

188188
try:

jwt/api_jwt.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def encode(
6868
payload, separators=(",", ":"), cls=json_encoder
6969
).encode("utf-8")
7070

71-
return super(PyJWT, self).encode(
71+
return super().encode(
7272
json_payload, key, algorithm, headers, json_encoder
7373
)
7474

@@ -98,7 +98,7 @@ def decode(
9898
else:
9999
options.setdefault("verify_signature", verify)
100100

101-
decoded = super(PyJWT, self).decode(
101+
decoded = super().decode(
102102
jwt, key=key, algorithms=algorithms, options=options, **kwargs
103103
)
104104

@@ -138,7 +138,7 @@ def _validate_claims(
138138
if options[opt]:
139139
require_options.append(opt_claim)
140140
warnings.warn(
141-
"The {0} parameter is deprecated. Please add {1} to"
141+
"The {} parameter is deprecated. Please add {} to"
142142
" the require list in options instead".format(opt, opt_claim),
143143
DeprecationWarning,
144144
)

jwt/help.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import print_function
2-
31
import json
42
import platform
53
import sys
@@ -27,15 +25,15 @@ def info():
2725
"system": platform.system(),
2826
"release": platform.release(),
2927
}
30-
except IOError:
28+
except OSError:
3129
platform_info = {"system": "Unknown", "release": "Unknown"}
3230

3331
implementation = platform.python_implementation()
3432

3533
if implementation == "CPython":
3634
implementation_version = platform.python_version()
3735
elif implementation == "PyPy":
38-
implementation_version = "%s.%s.%s" % (
36+
implementation_version = "{}.{}.{}".format(
3937
sys.pypy_version_info.major,
4038
sys.pypy_version_info.minor,
4139
sys.pypy_version_info.micro,

tests/contrib/test_algorithms.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ class TestPycryptoAlgorithms:
3636
def test_rsa_should_parse_pem_public_key(self):
3737
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
3838

39-
with open(key_path("testkey2_rsa.pub.pem"), "r") as pem_key:
39+
with open(key_path("testkey2_rsa.pub.pem")) as pem_key:
4040
algo.prepare_key(pem_key.read())
4141

4242
def test_rsa_should_accept_unicode_key(self):
4343
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
4444

45-
with open(key_path("testkey_rsa"), "r") as rsa_key:
45+
with open(key_path("testkey_rsa")) as rsa_key:
4646
algo.prepare_key(force_unicode(rsa_key.read()))
4747

4848
def test_rsa_should_reject_non_string_key(self):
@@ -67,10 +67,10 @@ def test_rsa_sign_should_generate_correct_signature_value(self):
6767
)
6868
)
6969

70-
with open(key_path("testkey_rsa"), "r") as keyfile:
70+
with open(key_path("testkey_rsa")) as keyfile:
7171
jwt_key = algo.prepare_key(keyfile.read())
7272

73-
with open(key_path("testkey_rsa.pub"), "r") as keyfile:
73+
with open(key_path("testkey_rsa.pub")) as keyfile:
7474
jwt_pub_key = algo.prepare_key(keyfile.read())
7575

7676
algo.sign(jwt_message, jwt_key)
@@ -95,7 +95,7 @@ def test_rsa_verify_should_return_false_if_signature_invalid(self):
9595

9696
jwt_sig += force_bytes("123") # Signature is now invalid
9797

98-
with open(key_path("testkey_rsa.pub"), "r") as keyfile:
98+
with open(key_path("testkey_rsa.pub")) as keyfile:
9999
jwt_pub_key = algo.prepare_key(keyfile.read())
100100

101101
result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
@@ -117,7 +117,7 @@ def test_rsa_verify_should_return_true_if_signature_valid(self):
117117
)
118118
)
119119

120-
with open(key_path("testkey_rsa.pub"), "r") as keyfile:
120+
with open(key_path("testkey_rsa.pub")) as keyfile:
121121
jwt_pub_key = algo.prepare_key(keyfile.read())
122122

123123
result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
@@ -126,7 +126,7 @@ def test_rsa_verify_should_return_true_if_signature_valid(self):
126126
def test_rsa_prepare_key_should_be_idempotent(self):
127127
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
128128

129-
with open(key_path("testkey_rsa.pub"), "r") as keyfile:
129+
with open(key_path("testkey_rsa.pub")) as keyfile:
130130
jwt_pub_key_first = algo.prepare_key(keyfile.read())
131131
jwt_pub_key_second = algo.prepare_key(jwt_pub_key_first)
132132

@@ -146,7 +146,7 @@ def test_ec_should_reject_non_string_key(self):
146146
def test_ec_should_accept_unicode_key(self):
147147
algo = ECAlgorithm(ECAlgorithm.SHA256)
148148

149-
with open(key_path("testkey_ec"), "r") as ec_key:
149+
with open(key_path("testkey_ec")) as ec_key:
150150
algo.prepare_key(force_unicode(ec_key.read()))
151151

152152
def test_ec_sign_should_generate_correct_signature_value(self):
@@ -162,10 +162,10 @@ def test_ec_sign_should_generate_correct_signature_value(self):
162162
)
163163
)
164164

165-
with open(key_path("testkey_ec"), "r") as keyfile:
165+
with open(key_path("testkey_ec")) as keyfile:
166166
jwt_key = algo.prepare_key(keyfile.read())
167167

168-
with open(key_path("testkey_ec.pub"), "r") as keyfile:
168+
with open(key_path("testkey_ec.pub")) as keyfile:
169169
jwt_pub_key = algo.prepare_key(keyfile.read())
170170

171171
algo.sign(jwt_message, jwt_key)
@@ -187,7 +187,7 @@ def test_ec_verify_should_return_false_if_signature_invalid(self):
187187

188188
jwt_sig += force_bytes("123") # Signature is now invalid
189189

190-
with open(key_path("testkey_ec.pub"), "r") as keyfile:
190+
with open(key_path("testkey_ec.pub")) as keyfile:
191191
jwt_pub_key = algo.prepare_key(keyfile.read())
192192

193193
result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
@@ -206,7 +206,7 @@ def test_ec_verify_should_return_true_if_signature_valid(self):
206206
)
207207
)
208208

209-
with open(key_path("testkey_ec.pub"), "r") as keyfile:
209+
with open(key_path("testkey_ec.pub")) as keyfile:
210210
jwt_pub_key = algo.prepare_key(keyfile.read())
211211

212212
result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
@@ -215,7 +215,7 @@ def test_ec_verify_should_return_true_if_signature_valid(self):
215215
def test_ec_prepare_key_should_be_idempotent(self):
216216
algo = ECAlgorithm(ECAlgorithm.SHA256)
217217

218-
with open(key_path("testkey_ec.pub"), "r") as keyfile:
218+
with open(key_path("testkey_ec.pub")) as keyfile:
219219
jwt_pub_key_first = algo.prepare_key(keyfile.read())
220220
jwt_pub_key_second = algo.prepare_key(jwt_pub_key_first)
221221

@@ -235,16 +235,16 @@ def test_ed25519_should_reject_non_string_key(self):
235235
with pytest.raises(TypeError):
236236
algo.prepare_key(None)
237237

238-
with open(key_path("testkey_ed25519"), "r") as keyfile:
238+
with open(key_path("testkey_ed25519")) as keyfile:
239239
jwt_key = algo.prepare_key(keyfile.read())
240240

241-
with open(key_path("testkey_ed25519.pub"), "r") as keyfile:
241+
with open(key_path("testkey_ed25519.pub")) as keyfile:
242242
jwt_pub_key = algo.prepare_key(keyfile.read())
243243

244244
def test_ed25519_should_accept_unicode_key(self):
245245
algo = Ed25519Algorithm()
246246

247-
with open(key_path("testkey_ed25519"), "r") as ec_key:
247+
with open(key_path("testkey_ed25519")) as ec_key:
248248
algo.prepare_key(force_unicode(ec_key.read()))
249249

250250
def test_ed25519_sign_should_generate_correct_signature_value(self):
@@ -254,10 +254,10 @@ def test_ed25519_sign_should_generate_correct_signature_value(self):
254254

255255
expected_sig = base64.b64decode(force_bytes(self.hello_world_sig))
256256

257-
with open(key_path("testkey_ed25519"), "r") as keyfile:
257+
with open(key_path("testkey_ed25519")) as keyfile:
258258
jwt_key = algo.prepare_key(keyfile.read())
259259

260-
with open(key_path("testkey_ed25519.pub"), "r") as keyfile:
260+
with open(key_path("testkey_ed25519.pub")) as keyfile:
261261
jwt_pub_key = algo.prepare_key(keyfile.read())
262262

263263
algo.sign(jwt_message, jwt_key)
@@ -272,7 +272,7 @@ def test_ed25519_verify_should_return_false_if_signature_invalid(self):
272272

273273
jwt_sig += force_bytes("123") # Signature is now invalid
274274

275-
with open(key_path("testkey_ed25519.pub"), "r") as keyfile:
275+
with open(key_path("testkey_ed25519.pub")) as keyfile:
276276
jwt_pub_key = algo.prepare_key(keyfile.read())
277277

278278
result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
@@ -284,7 +284,7 @@ def test_ed25519_verify_should_return_true_if_signature_valid(self):
284284
jwt_message = self.hello_world
285285
jwt_sig = base64.b64decode(force_bytes(self.hello_world_sig))
286286

287-
with open(key_path("testkey_ed25519.pub"), "r") as keyfile:
287+
with open(key_path("testkey_ed25519.pub")) as keyfile:
288288
jwt_pub_key = algo.prepare_key(keyfile.read())
289289

290290
result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
@@ -293,7 +293,7 @@ def test_ed25519_verify_should_return_true_if_signature_valid(self):
293293
def test_ed25519_prepare_key_should_be_idempotent(self):
294294
algo = Ed25519Algorithm()
295295

296-
with open(key_path("testkey_ed25519.pub"), "r") as keyfile:
296+
with open(key_path("testkey_ed25519.pub")) as keyfile:
297297
jwt_pub_key_first = algo.prepare_key(keyfile.read())
298298
jwt_pub_key_second = algo.prepare_key(jwt_pub_key_first)
299299

tests/keys/__init__.py

Lines changed: 5 additions & 5 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,7 +48,7 @@ def load_ec_key():
4848
)
4949

5050
def load_ec_pub_key():
51-
with open(os.path.join(BASE_PATH, "jwk_ec_pub.json"), "r") as infile:
51+
with open(os.path.join(BASE_PATH, "jwk_ec_pub.json")) as infile:
5252
keyobj = json.load(infile)
5353

5454
return ec.EllipticCurvePublicNumbers(

0 commit comments

Comments
 (0)