Skip to content

Commit

Permalink
Run pyupgrade across project to use modern Python 3 conventions (#491)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jdufresne committed Jun 8, 2020
1 parent 7b2b4ff commit 65bca5c
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 122 deletions.
16 changes: 7 additions & 9 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
#
# PyJWT documentation build configuration file, created by
# sphinx-quickstart on Thu Oct 22 18:11:10 2015.
#
Expand Down Expand Up @@ -51,9 +49,9 @@
master_doc = "index"

# General information about the project.
project = u"PyJWT"
copyright = u"2015, José Padilla"
author = u"José Padilla"
project = "PyJWT"
copyright = "2015, José Padilla"
author = "José Padilla"

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
Expand Down Expand Up @@ -244,8 +242,8 @@ def get_version(package):
(
master_doc,
"PyJWT.tex",
u"PyJWT Documentation",
u"José Padilla",
"PyJWT Documentation",
"José Padilla",
"manual",
)
]
Expand Down Expand Up @@ -275,7 +273,7 @@ def get_version(package):

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

# If true, show URL addresses after external links.
# man_show_urls = False
Expand All @@ -290,7 +288,7 @@ def get_version(package):
(
master_doc,
"PyJWT",
u"PyJWT Documentation",
"PyJWT Documentation",
author,
"PyJWT",
"One line description of project.",
Expand Down
1 change: 0 additions & 1 deletion jwt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# flake8: noqa

"""
Expand Down
2 changes: 1 addition & 1 deletion jwt/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def decode_payload(args):
if sys.stdin.isatty():
token = sys.stdin.readline().strip()
else:
raise IOError("Cannot read from stdin: terminal not a TTY")
raise OSError("Cannot read from stdin: terminal not a TTY")

token = token.encode("utf-8")
data = decode(token, key=args.key, verify=args.verify)
Expand Down
8 changes: 3 additions & 5 deletions jwt/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
has_crypto = False
has_ed25519 = False

requires_cryptography = set(
[
requires_cryptography = {
"RS256",
"RS384",
"RS512",
Expand All @@ -58,8 +57,7 @@
"PS384",
"PS512",
"EdDSA",
]
)
}


def get_default_algorithms():
Expand Down Expand Up @@ -104,7 +102,7 @@ def get_default_algorithms():
return default_algorithms


class Algorithm(object):
class Algorithm:
"""
The interface for an algorithm used to sign and verify tokens.
"""
Expand Down
4 changes: 2 additions & 2 deletions jwt/api_jws.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
pass


class PyJWS(object):
class PyJWS:
header_typ = "JWT"

def __init__(self, algorithms=None, options=None):
Expand Down Expand Up @@ -182,7 +182,7 @@ def _load(self, jwt):

if not issubclass(type(jwt), binary_type):
raise DecodeError(
"Invalid token type. Token must be a {0}".format(binary_type)
"Invalid token type. Token must be a {}".format(binary_type)
)

try:
Expand Down
6 changes: 3 additions & 3 deletions jwt/api_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def encode(
payload, separators=(",", ":"), cls=json_encoder
).encode("utf-8")

return super(PyJWT, self).encode(
return super().encode(
json_payload, key, algorithm, headers, json_encoder
)

Expand Down Expand Up @@ -98,7 +98,7 @@ def decode(
else:
options.setdefault("verify_signature", verify)

decoded = super(PyJWT, self).decode(
decoded = super().decode(
jwt, key=key, algorithms=algorithms, options=options, **kwargs
)

Expand Down Expand Up @@ -138,7 +138,7 @@ def _validate_claims(
if options[opt]:
require_options.append(opt_claim)
warnings.warn(
"The {0} parameter is deprecated. Please add {1} to"
"The {} parameter is deprecated. Please add {} to"
" the require list in options instead".format(opt, opt_claim),
DeprecationWarning,
)
Expand Down
6 changes: 2 additions & 4 deletions jwt/help.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function

import json
import platform
import sys
Expand Down Expand Up @@ -27,15 +25,15 @@ def info():
"system": platform.system(),
"release": platform.release(),
}
except IOError:
except OSError:
platform_info = {"system": "Unknown", "release": "Unknown"}

implementation = platform.python_implementation()

if implementation == "CPython":
implementation_version = platform.python_version()
elif implementation == "PyPy":
implementation_version = "%s.%s.%s" % (
implementation_version = "{}.{}.{}".format(
sys.pypy_version_info.major,
sys.pypy_version_info.minor,
sys.pypy_version_info.micro,
Expand Down
42 changes: 21 additions & 21 deletions tests/contrib/test_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ class TestPycryptoAlgorithms:
def test_rsa_should_parse_pem_public_key(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

with open(key_path("testkey2_rsa.pub.pem"), "r") as pem_key:
with open(key_path("testkey2_rsa.pub.pem")) as pem_key:
algo.prepare_key(pem_key.read())

def test_rsa_should_accept_unicode_key(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

with open(key_path("testkey_rsa"), "r") as rsa_key:
with open(key_path("testkey_rsa")) as rsa_key:
algo.prepare_key(force_unicode(rsa_key.read()))

def test_rsa_should_reject_non_string_key(self):
Expand All @@ -67,10 +67,10 @@ def test_rsa_sign_should_generate_correct_signature_value(self):
)
)

with open(key_path("testkey_rsa"), "r") as keyfile:
with open(key_path("testkey_rsa")) as keyfile:
jwt_key = algo.prepare_key(keyfile.read())

with open(key_path("testkey_rsa.pub"), "r") as keyfile:
with open(key_path("testkey_rsa.pub")) as keyfile:
jwt_pub_key = algo.prepare_key(keyfile.read())

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

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

with open(key_path("testkey_rsa.pub"), "r") as keyfile:
with open(key_path("testkey_rsa.pub")) as keyfile:
jwt_pub_key = algo.prepare_key(keyfile.read())

result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
Expand All @@ -117,7 +117,7 @@ def test_rsa_verify_should_return_true_if_signature_valid(self):
)
)

with open(key_path("testkey_rsa.pub"), "r") as keyfile:
with open(key_path("testkey_rsa.pub")) as keyfile:
jwt_pub_key = algo.prepare_key(keyfile.read())

result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
Expand All @@ -126,7 +126,7 @@ def test_rsa_verify_should_return_true_if_signature_valid(self):
def test_rsa_prepare_key_should_be_idempotent(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

with open(key_path("testkey_rsa.pub"), "r") as keyfile:
with open(key_path("testkey_rsa.pub")) as keyfile:
jwt_pub_key_first = algo.prepare_key(keyfile.read())
jwt_pub_key_second = algo.prepare_key(jwt_pub_key_first)

Expand All @@ -146,7 +146,7 @@ def test_ec_should_reject_non_string_key(self):
def test_ec_should_accept_unicode_key(self):
algo = ECAlgorithm(ECAlgorithm.SHA256)

with open(key_path("testkey_ec"), "r") as ec_key:
with open(key_path("testkey_ec")) as ec_key:
algo.prepare_key(force_unicode(ec_key.read()))

def test_ec_sign_should_generate_correct_signature_value(self):
Expand All @@ -162,10 +162,10 @@ def test_ec_sign_should_generate_correct_signature_value(self):
)
)

with open(key_path("testkey_ec"), "r") as keyfile:
with open(key_path("testkey_ec")) as keyfile:
jwt_key = algo.prepare_key(keyfile.read())

with open(key_path("testkey_ec.pub"), "r") as keyfile:
with open(key_path("testkey_ec.pub")) as keyfile:
jwt_pub_key = algo.prepare_key(keyfile.read())

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

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

with open(key_path("testkey_ec.pub"), "r") as keyfile:
with open(key_path("testkey_ec.pub")) as keyfile:
jwt_pub_key = algo.prepare_key(keyfile.read())

result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
Expand All @@ -206,7 +206,7 @@ def test_ec_verify_should_return_true_if_signature_valid(self):
)
)

with open(key_path("testkey_ec.pub"), "r") as keyfile:
with open(key_path("testkey_ec.pub")) as keyfile:
jwt_pub_key = algo.prepare_key(keyfile.read())

result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
Expand All @@ -215,7 +215,7 @@ def test_ec_verify_should_return_true_if_signature_valid(self):
def test_ec_prepare_key_should_be_idempotent(self):
algo = ECAlgorithm(ECAlgorithm.SHA256)

with open(key_path("testkey_ec.pub"), "r") as keyfile:
with open(key_path("testkey_ec.pub")) as keyfile:
jwt_pub_key_first = algo.prepare_key(keyfile.read())
jwt_pub_key_second = algo.prepare_key(jwt_pub_key_first)

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

with open(key_path("testkey_ed25519"), "r") as keyfile:
with open(key_path("testkey_ed25519")) as keyfile:
jwt_key = algo.prepare_key(keyfile.read())

with open(key_path("testkey_ed25519.pub"), "r") as keyfile:
with open(key_path("testkey_ed25519.pub")) as keyfile:
jwt_pub_key = algo.prepare_key(keyfile.read())

def test_ed25519_should_accept_unicode_key(self):
algo = Ed25519Algorithm()

with open(key_path("testkey_ed25519"), "r") as ec_key:
with open(key_path("testkey_ed25519")) as ec_key:
algo.prepare_key(force_unicode(ec_key.read()))

def test_ed25519_sign_should_generate_correct_signature_value(self):
Expand All @@ -254,10 +254,10 @@ def test_ed25519_sign_should_generate_correct_signature_value(self):

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

with open(key_path("testkey_ed25519"), "r") as keyfile:
with open(key_path("testkey_ed25519")) as keyfile:
jwt_key = algo.prepare_key(keyfile.read())

with open(key_path("testkey_ed25519.pub"), "r") as keyfile:
with open(key_path("testkey_ed25519.pub")) as keyfile:
jwt_pub_key = algo.prepare_key(keyfile.read())

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

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

with open(key_path("testkey_ed25519.pub"), "r") as keyfile:
with open(key_path("testkey_ed25519.pub")) as keyfile:
jwt_pub_key = algo.prepare_key(keyfile.read())

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

with open(key_path("testkey_ed25519.pub"), "r") as keyfile:
with open(key_path("testkey_ed25519.pub")) as keyfile:
jwt_pub_key = algo.prepare_key(keyfile.read())

result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
Expand All @@ -293,7 +293,7 @@ def test_ed25519_verify_should_return_true_if_signature_valid(self):
def test_ed25519_prepare_key_should_be_idempotent(self):
algo = Ed25519Algorithm()

with open(key_path("testkey_ed25519.pub"), "r") as keyfile:
with open(key_path("testkey_ed25519.pub")) as keyfile:
jwt_pub_key_first = algo.prepare_key(keyfile.read())
jwt_pub_key_second = algo.prepare_key(jwt_pub_key_first)

Expand Down
10 changes: 5 additions & 5 deletions tests/keys/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def decode_value(val):


def load_hmac_key():
with open(os.path.join(BASE_PATH, "jwk_hmac.json"), "r") as infile:
with open(os.path.join(BASE_PATH, "jwk_hmac.json")) as infile:
keyobj = json.load(infile)

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

def load_rsa_key():
with open(os.path.join(BASE_PATH, "jwk_rsa_key.json"), "r") as infile:
with open(os.path.join(BASE_PATH, "jwk_rsa_key.json")) as infile:
return RSAAlgorithm.from_jwk(infile.read())

def load_rsa_pub_key():
with open(os.path.join(BASE_PATH, "jwk_rsa_pub.json"), "r") as infile:
with open(os.path.join(BASE_PATH, "jwk_rsa_pub.json")) as infile:
return RSAAlgorithm.from_jwk(infile.read())

def load_ec_key():
with open(os.path.join(BASE_PATH, "jwk_ec_key.json"), "r") as infile:
with open(os.path.join(BASE_PATH, "jwk_ec_key.json")) as infile:
keyobj = json.load(infile)

return ec.EllipticCurvePrivateNumbers(
Expand All @@ -48,7 +48,7 @@ def load_ec_key():
)

def load_ec_pub_key():
with open(os.path.join(BASE_PATH, "jwk_ec_pub.json"), "r") as infile:
with open(os.path.join(BASE_PATH, "jwk_ec_pub.json")) as infile:
keyobj = json.load(infile)

return ec.EllipticCurvePublicNumbers(
Expand Down
Loading

0 comments on commit 65bca5c

Please sign in to comment.