Skip to content

Commit 6843774

Browse files
authored
Merge pull request #865 from ericdasse-stonal/fix-warning-python-3.14
Fix DeprecationWarning
2 parents 7d2afc5 + e8808ca commit 6843774

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

newsfragments/865.misc.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Fix DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version.
2+
3+
`datetime.datetime.utcnow` has been deprecated by Python and will soon be removed by Python 3.14 (see warning text below)
4+
5+
The following warning used to pop up everytime someone with a recent version of Python (3.12+) uses this plugin
6+
7+
.. code-block::
8+
9+
.venv/lib/python3.12/site-packages/pytest_postgresql/retry.py:22: 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).
10+
time: datetime = datetime.utcnow()
11+
12+
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html

pytest_postgresql/retry.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
"""Small retry callable in case of specific error occurred."""
22

3-
from datetime import datetime, timedelta
3+
import datetime
4+
import sys
45
from time import sleep
56
from typing import Callable, Type, TypeVar
67

78
T = TypeVar("T")
89

910

1011
def retry(
11-
func: Callable[[], T], timeout: int = 60, possible_exception: Type[Exception] = Exception
12+
func: Callable[[], T],
13+
timeout: int = 60,
14+
possible_exception: Type[Exception] = Exception,
1215
) -> T:
1316
"""Attempt to retry the function for timeout time.
1417
@@ -19,15 +22,27 @@ def retry(
1922
... ::
2023
FATAL: the database system is starting up
2124
"""
22-
time: datetime = datetime.utcnow()
23-
timeout_diff: timedelta = timedelta(seconds=timeout)
25+
time: datetime.datetime = get_current_datetime()
26+
timeout_diff: datetime.timedelta = datetime.timedelta(seconds=timeout)
2427
i = 0
2528
while True:
2629
i += 1
2730
try:
2831
res = func()
2932
return res
3033
except possible_exception as e:
31-
if time + timeout_diff < datetime.utcnow():
34+
if time + timeout_diff < datetime.datetime.utcnow():
3235
raise TimeoutError(f"Failed after {i} attempts") from e
3336
sleep(1)
37+
38+
39+
def get_current_datetime() -> datetime.datetime:
40+
"""Get the current datetime."""
41+
# To ensure the current datetime retrieval is adjusted with the latest
42+
# versions of Python while ensuring retro-compatibility with
43+
# Python 3.8, 3.9 and 3.10, we check what version of Python is
44+
# being used before deciding how to operate
45+
if sys.version_info.major == 3 and sys.version_info.minor > 10:
46+
return datetime.datetime.now(datetime.UTC)
47+
48+
return datetime.datetime.utcnow()

0 commit comments

Comments
 (0)