Skip to content

Commit 92040c7

Browse files
committed
fix: update datetime usage for compatibility with Python 3.12 and 3.13
- Replace deprecated datetime.utcnow() with datetime.now(UTC) for better compatibility. - Adjusted import statements and usage in jwt.py and test_jwt.py for consistency.
1 parent 9e6ae24 commit 92040c7

File tree

2 files changed

+9
-22
lines changed

2 files changed

+9
-22
lines changed

jose/jwt.py

+2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88

99
try:
1010
from datetime import UTC, datetime, timedelta
11+
1112
utc_now = datetime.now(UTC) # Preferred in Python 3.13+
1213
except ImportError:
1314
from datetime import datetime, timedelta, timezone
15+
1416
utc_now = datetime.now(timezone.utc) # Preferred in Python 3.12 and below
1517
UTC = timezone.utc
1618

tests/test_jwt.py

+7-22
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
try:
55
from datetime import UTC, datetime, timedelta
6+
67
utc_now = datetime.now(UTC) # Preferred in Python 3.13+
78
except ImportError:
89
from datetime import datetime, timedelta, timezone
10+
911
utc_now = datetime.now(timezone.utc) # Preferred in Python 3.12 and below
1012
UTC = timezone.utc
1113

@@ -92,9 +94,7 @@ def return_encoded_array(token, key, algorithms, verify=True):
9294

9395
jws.verify = return_encoded_array
9496

95-
with pytest.raises(
96-
JWTError, match="Invalid payload string: must be a json object"
97-
):
97+
with pytest.raises(JWTError, match="Invalid payload string: must be a json object"):
9898
jwt.decode(token, "secret", ["HS256"])
9999
finally:
100100
jws.verify = old_jws_verify
@@ -154,35 +154,20 @@ def test_deterministic_headers(self):
154154

155155
# manually decode header to compare it to known good
156156
decoded_headers1 = base64url_decode(encoded_headers1.encode("utf-8"))
157-
assert (
158-
decoded_headers1
159-
== b"""{"alg":"HS256","another_key":"another_value","kid":"my-key-id","typ":"JWT"}"""
160-
)
157+
assert decoded_headers1 == b"""{"alg":"HS256","another_key":"another_value","kid":"my-key-id","typ":"JWT"}"""
161158

162159
def test_encode(self, claims, key):
163160
expected = (
164-
(
165-
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9"
166-
".eyJhIjoiYiJ9"
167-
".xNtk2S0CNbCBZX_f67pFgGRugaP1xi2ICfet3nwOSxw"
168-
),
169-
(
170-
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"
171-
".eyJhIjoiYiJ9"
172-
".jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8"
173-
),
161+
("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" ".eyJhIjoiYiJ9" ".xNtk2S0CNbCBZX_f67pFgGRugaP1xi2ICfet3nwOSxw"),
162+
("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" ".eyJhIjoiYiJ9" ".jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8"),
174163
)
175164

176165
encoded = jwt.encode(claims, key)
177166

178167
assert encoded in expected
179168

180169
def test_decode(self, claims, key):
181-
token = (
182-
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"
183-
".eyJhIjoiYiJ9"
184-
".jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8"
185-
)
170+
token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" ".eyJhIjoiYiJ9" ".jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8"
186171

187172
decoded = jwt.decode(token, key)
188173

0 commit comments

Comments
 (0)