Skip to content

Commit ceaac36

Browse files
sobolevnasherf
andauthored
Do not use utc_now on module level (#372)
* Do not use `utc_now` on module level Looks like that this value is never used anywhere, and it should not be, because it is created once on a module import time. I found this while working on python/typeshed#13514 * Fix tests as well * fix lint import issue * try to fix black lint errors * fix again --------- Co-authored-by: Asher Foa <[email protected]>
1 parent 0b4af73 commit ceaac36

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

jose/jwt.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
import json
22
from calendar import timegm
3+
from datetime import datetime, timedelta
34

45
try:
56
from collections.abc import Mapping
67
except ImportError:
78
from collections import Mapping
89

910
try:
10-
from datetime import UTC, datetime, timedelta
11-
12-
utc_now = datetime.now(UTC) # Preferred in Python 3.13+
11+
from datetime import UTC # Preferred in Python 3.13+
1312
except ImportError:
14-
from datetime import datetime, timedelta, timezone
13+
from datetime import timezone
1514

16-
utc_now = datetime.now(timezone.utc) # Preferred in Python 3.12 and below
17-
UTC = timezone.utc
15+
UTC = timezone.utc # Preferred in Python 3.12 and below
1816

1917
from jose import jws
2018

tests/test_jwt.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import base64
22
import json
3+
from datetime import datetime, timedelta
34

45
try:
5-
from datetime import UTC, datetime, timedelta
6-
7-
utc_now = datetime.now(UTC) # Preferred in Python 3.13+
6+
from datetime import UTC # Preferred in Python 3.13+
87
except ImportError:
9-
from datetime import datetime, timedelta, timezone
8+
from datetime import timezone # Preferred in Python 3.12 and below
109

11-
utc_now = datetime.now(timezone.utc) # Preferred in Python 3.12 and below
1210
UTC = timezone.utc
1311

14-
1512
import pytest
1613

1714
from jose import jws, jwt
@@ -514,14 +511,16 @@ def test_unverified_claims_object(self, claims, key):
514511
[
515512
("aud", "aud"),
516513
("ait", "ait"),
517-
("exp", utc_now + timedelta(seconds=3600)),
518-
("nbf", utc_now - timedelta(seconds=5)),
514+
("exp", lambda: datetime.now(UTC) + timedelta(seconds=3600)),
515+
("nbf", lambda: datetime.now(UTC) - timedelta(seconds=5)),
519516
("iss", "iss"),
520517
("sub", "sub"),
521518
("jti", "jti"),
522519
],
523520
)
524521
def test_require(self, claims, key, claim, value):
522+
if callable(value):
523+
value = value()
525524
options = {"require_" + claim: True, "verify_" + claim: False}
526525

527526
token = jwt.encode(claims, key)

0 commit comments

Comments
 (0)