Skip to content

Commit b7c728e

Browse files
committed
Use abstract base classes instead of hard coded types
1 parent d1380fb commit b7c728e

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

flask_jwt_extended/config.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import datetime
22
from warnings import warn
33

4+
# In Python 2.7 collections.abc is a part of the collections module.
5+
try:
6+
from collections.abc import Sequence, Set
7+
except ImportError: # pragma: no cover
8+
from collections import Sequence, Set
9+
410
from flask import current_app
511

612
# Older versions of pyjwt do not have the requires_cryptography set. Also,
@@ -44,9 +50,8 @@ def token_location(self):
4450
locations = current_app.config['JWT_TOKEN_LOCATION']
4551
if isinstance(locations, str):
4652
locations = (locations,)
47-
elif not isinstance(locations, (tuple, list, frozenset, set)):
48-
raise RuntimeError('JWT_TOKEN_LOCATION must be a tuple, a list, a frozenset '
49-
'or a set')
53+
elif not isinstance(locations, (Sequence, Set)):
54+
raise RuntimeError('JWT_TOKEN_LOCATION must be a sequence or a set')
5055
elif not locations:
5156
raise RuntimeError('JWT_TOKEN_LOCATION must contain at least one '
5257
'of "headers", "cookies", "query_string", or "json"')
@@ -207,9 +212,8 @@ def blacklist_checks(self):
207212
check_type = current_app.config['JWT_BLACKLIST_TOKEN_CHECKS']
208213
if isinstance(check_type, str):
209214
check_type = (check_type,)
210-
elif not isinstance(check_type, (tuple, list, frozenset, set)):
211-
raise RuntimeError('JWT_BLACKLIST_TOKEN_CHECKS must be a tuple, a list, a '
212-
'frozenset or a set')
215+
elif not isinstance(check_type, (Sequence, Set)):
216+
raise RuntimeError('JWT_BLACKLIST_TOKEN_CHECKS must be a sequence or a set')
213217
for item in check_type:
214218
if item not in ('access', 'refresh'):
215219
err = 'JWT_BLACKLIST_TOKEN_CHECKS must be "access" or "refresh"'

tests/test_config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ def test_invalid_config_options(app):
254254
with pytest.raises(RuntimeError):
255255
config.token_location
256256

257+
app.config['JWT_TOKEN_LOCATION'] = range(99)
258+
with pytest.raises(RuntimeError):
259+
config.token_location
260+
257261
app.config['JWT_TOKEN_LOCATION'] = ['headers', 'cookies', 'banana']
258262
with pytest.raises(RuntimeError):
259263
config.token_location
@@ -291,6 +295,10 @@ def test_invalid_config_options(app):
291295
with pytest.raises(RuntimeError):
292296
config.blacklist_checks
293297

298+
app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = range(99)
299+
with pytest.raises(RuntimeError):
300+
config.blacklist_checks
301+
294302
app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access', 'banana']
295303
with pytest.raises(RuntimeError):
296304
config.blacklist_checks

0 commit comments

Comments
 (0)