From 71e4bcab8abd23c4e00dd5b35c8f014a3a9f1bb2 Mon Sep 17 00:00:00 2001 From: Balthazar Rouberol Date: Wed, 1 Nov 2023 16:21:19 +0100 Subject: [PATCH] Resolve deprecation warning related to using `datetime.utcnow` in py3.12 Starting from python 3.12.0, we started seeing some deprecation warning related to use of `datetime.datetime.utcnow`: ``` /Users/.../lib/python3.12/site-packages/fastapi_jwt/jwt.py:135: DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC). now = datetime.utcnow() ``` This PR should provide the same level of functionality without firing a deprecation warning, by implementing the suggested change. --- fastapi_jwt/jwt.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fastapi_jwt/jwt.py b/fastapi_jwt/jwt.py index 65b4d1f..ff4f318 100644 --- a/fastapi_jwt/jwt.py +++ b/fastapi_jwt/jwt.py @@ -1,5 +1,5 @@ from abc import ABC -from datetime import datetime, timedelta +from datetime import datetime, timedelta, UTC from typing import Any, Dict, Optional, Set from uuid import uuid4 @@ -132,7 +132,7 @@ def _generate_payload( unique_identifier: str, token_type: str, ) -> Dict[str, Any]: - now = datetime.utcnow() + now = datetime.now(UTC) return { "subject": subject.copy(), # main subject