Skip to content

Commit

Permalink
Replace merge_dict() with builtin dict unpacking generalizations (#555)
Browse files Browse the repository at this point in the history
Merging two dict is support since Python 3.5 using the ** syntax.

See:

- https://docs.python.org/3.9/whatsnew/3.5.html#whatsnew-pep-448
- https://www.python.org/dev/peps/pep-0448/
  • Loading branch information
jdufresne committed Dec 18, 2020
1 parent d0b9dc7 commit 523a6f2
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 21 deletions.
12 changes: 6 additions & 6 deletions jwt/api_jws.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
InvalidSignatureError,
InvalidTokenError,
)
from .utils import base64url_decode, base64url_encode, merge_dict
from .utils import base64url_decode, base64url_encode


class PyJWS:
Expand All @@ -34,10 +34,9 @@ def __init__(self, algorithms=None, options=None):
if key not in self._valid_algs:
del self._algorithms[key]

if not options:
if options is None:
options = {}

self.options = merge_dict(self._get_default_options(), options)
self.options = {**self._get_default_options(), **options}

@staticmethod
def _get_default_options():
Expand Down Expand Up @@ -137,8 +136,9 @@ def decode(
complete: bool = False,
**kwargs,
):

merged_options = merge_dict(self.options, options)
if options is None:
options = {}
merged_options = {**self.options, **options}
verify_signature = merged_options["verify_signature"]

if verify_signature and not algorithms:
Expand Down
3 changes: 1 addition & 2 deletions jwt/api_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
InvalidIssuerError,
MissingRequiredClaimError,
)
from .utils import merge_dict


class PyJWT(PyJWS):
Expand Down Expand Up @@ -103,7 +102,7 @@ def decode(
raise DecodeError("Invalid payload string: must be a json object")

if options["verify_signature"]:
merged_options = merge_dict(self.options, options)
merged_options = {**self.options, **options}
self._validate_claims(payload, merged_options, **kwargs)

if complete:
Expand Down
13 changes: 0 additions & 13 deletions jwt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,6 @@ def from_base64url_uint(val):
return int("".join(["%02x" % byte for byte in buf]), 16)


def merge_dict(original, updates):
if not updates:
return original

try:
merged_options = original.copy()
merged_options.update(updates)
except (AttributeError, ValueError) as e:
raise TypeError("original and updates must be a dictionary: %s" % e)

return merged_options


def number_to_bytes(num, num_bytes):
padded_hex = "%0*x" % (2 * num_bytes, num)
big_endian = binascii.a2b_hex(padded_hex.encode("ascii"))
Expand Down

0 comments on commit 523a6f2

Please sign in to comment.