Skip to content

Commit e62df13

Browse files
author
Michael Davis
committed
refactor option merging, add myself to AUTHORS, s/dict()/{}
1 parent 143feed commit e62df13

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

AUTHORS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ Patches and Suggestions
2121
- Mark Adams <mark@markadams.me>
2222

2323
- Wouter Bolsterlee <uws@xs4all.nl>
24+
25+
- Michael Davis <mike.philip.davis@gmail.com> <mike.davis@workiva.com>

jwt/api.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,7 @@ def __init__(self, algorithms=None, options=None):
3636
'verify_aud': True,
3737
}
3838

39-
try:
40-
merged_options = dict()
41-
for k, v in self.default_options.items():
42-
merged_options[k] = options.get(k, v)
43-
self.options = merged_options
44-
except AttributeError as e:
45-
raise TypeError('options must be a dictionary: %s' % e)
39+
self.options = self._merge_options(self.default_options, options)
4640

4741
def register_algorithm(self, alg_id, alg_obj):
4842
"""
@@ -254,15 +248,17 @@ def _validate_claims(self, payload, audience=None, issuer=None, leeway=0,
254248
if payload.get('iss') != issuer:
255249
raise InvalidIssuerError('Invalid issuer')
256250

257-
def _merge_options(self, override_options=None):
251+
def _merge_options(self, default_options=None, override_options=None):
252+
if not default_options:
253+
default_options = {}
254+
258255
if not override_options:
259256
override_options = {}
260257

261258
try:
262-
merged_options = dict()
263-
for k, v in self.options.items():
264-
merged_options[k] = override_options.get(k, v)
265-
except AttributeError as e:
259+
merged_options = self.default_options.copy()
260+
merged_options.update(override_options)
261+
except (AttributeError, ValueError) as e:
266262
raise TypeError('options must be a dictionary: %s' % e)
267263

268264
return merged_options

tests/test_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ def test_override_options(self):
8181
expected_options['verify_nbf'] = False
8282
self.assertEqual(expected_options, self.jwt.options)
8383

84-
def test_non_existant_options_dont_exist(self):
84+
def test_non_default_options_persist(self):
8585
self.jwt = PyJWT(options={'verify_iat': False, 'foobar': False})
8686
expected_options = self.jwt.default_options
8787
expected_options['verify_iat'] = False
88+
expected_options['foobar'] = False
8889
self.assertEqual(expected_options, self.jwt.options)
89-
self.assertNotIn('foobar', self.jwt.options)
9090

9191
def test_options_must_be_dict(self):
9292
self.assertRaises(TypeError, PyJWT, options=object())

0 commit comments

Comments
 (0)