Skip to content

Commit

Permalink
Replace deprecated datetime.utcnow with datetime.now (#1014)
Browse files Browse the repository at this point in the history
* refactor: replace deprecated datetime.utcnow with datetime.now
* refactor: use datetime.now for CachedItem timestamp's default factory
  • Loading branch information
adeelsohailahmed authored Oct 1, 2024
1 parent 5110b08 commit b0c9037
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
8 changes: 4 additions & 4 deletions beanie/odm/cache.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import collections
import datetime
from datetime import timedelta
from datetime import timedelta, timezone
from typing import Any, Optional

from pydantic import BaseModel, Field


class CachedItem(BaseModel):
timestamp: datetime.datetime = Field(
default_factory=datetime.datetime.utcnow
default_factory=lambda: datetime.datetime.now(tz=timezone.utc)
)
value: Any

Expand All @@ -21,9 +21,9 @@ def __init__(self, capacity: int, expiration_time: timedelta):

def get(self, key) -> Optional[CachedItem]:
try:
item = self.cache.pop(key)
item: CachedItem = self.cache.pop(key)
if (
datetime.datetime.utcnow() - item.timestamp
datetime.datetime.now(tz=timezone.utc) - item.timestamp
> self.expiration_time
):
return None
Expand Down
4 changes: 2 additions & 2 deletions beanie/odm/documents.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
import warnings
from datetime import datetime
from datetime import datetime, timezone
from enum import Enum
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -1245,7 +1245,7 @@ async def delete(
skip_actions: Optional[List[Union[ActionDirections, str]]] = None,
**pymongo_kwargs,
) -> Optional[DeleteResult]:
self.deleted_at = datetime.utcnow()
self.deleted_at = datetime.now(tz=timezone.utc)
await self.save()
return None

Expand Down
6 changes: 3 additions & 3 deletions tests/odm/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from random import randint
from typing import List

Expand Down Expand Up @@ -121,7 +121,7 @@ def point():
async def preset_documents(point):
docs = []
for i in range(10):
timestamp = datetime.utcnow() - timedelta(days=i)
timestamp = datetime.now(tz=timezone.utc) - timedelta(days=i)
integer_1: int = i // 3
integer_2: int = i // 2
float_num = integer_1 + 0.3
Expand Down Expand Up @@ -177,7 +177,7 @@ def sample_doc_not_saved(point):
]
)
return Sample(
timestamp=datetime.utcnow(),
timestamp=datetime.now(tz=timezone.utc),
increment=0,
integer=0,
float_num=0,
Expand Down
11 changes: 8 additions & 3 deletions tests/odm/query/test_find.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ async def test_find_many_with_session(preset_documents, session):

async def test_bson_encoders_filed_types():
custom = DocumentWithBsonEncodersFiledsTypes(
color="7fffd4", timestamp=datetime.datetime.utcnow()
color="7fffd4",
timestamp=datetime.datetime.now(tz=datetime.timezone.utc),
)
c = await custom.insert()
c_fromdb = await DocumentWithBsonEncodersFiledsTypes.find_one(
Expand All @@ -321,8 +322,12 @@ async def test_bson_encoders_filed_types():


async def test_find_by_datetime(preset_documents):
datetime_1 = datetime.datetime.utcnow() - datetime.timedelta(days=7)
datetime_2 = datetime.datetime.utcnow() - datetime.timedelta(days=2)
datetime_1 = datetime.datetime.now(
tz=datetime.timezone.utc
) - datetime.timedelta(days=7)
datetime_2 = datetime.datetime.now(
tz=datetime.timezone.utc
) - datetime.timedelta(days=2)
docs = await Sample.find(
Sample.timestamp >= datetime_1,
Sample.timestamp <= datetime_2,
Expand Down
3 changes: 2 additions & 1 deletion tests/odm/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def test_pydantic_object_id_bytes_input():

async def test_bson_encoders_filed_types():
custom = DocumentWithBsonEncodersFiledsTypes(
color="7fffd4", timestamp=datetime.datetime.utcnow()
color="7fffd4",
timestamp=datetime.datetime.now(tz=datetime.timezone.utc),
)
encoded = get_dict(custom)
assert isinstance(encoded["timestamp"], str)
Expand Down

0 comments on commit b0c9037

Please sign in to comment.