Skip to content

Commit

Permalink
lint datetime.timezone.utc -> datetime.UTC
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-genson committed Dec 3, 2024
1 parent de9b847 commit 5840237
Show file tree
Hide file tree
Showing 33 changed files with 71 additions and 73 deletions.
4 changes: 2 additions & 2 deletions mealie/core/release_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import requests

_LAST_RESET = None
_LAST_RESET: datetime.datetime | None = None


@lru_cache(maxsize=1)
Expand Down Expand Up @@ -32,7 +32,7 @@ def get_latest_version() -> str:

global _LAST_RESET

now = datetime.datetime.now(datetime.timezone.utc)
now = datetime.datetime.now(datetime.UTC)

if not _LAST_RESET or now - _LAST_RESET > datetime.timedelta(days=MAX_DAYS_OLD):
_LAST_RESET = now
Expand Down
4 changes: 2 additions & 2 deletions mealie/core/security/providers/auth_provider.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import abc
from datetime import datetime, timedelta, timezone
from datetime import UTC, datetime, timedelta
from typing import Generic, TypeVar

import jwt
Expand Down Expand Up @@ -45,7 +45,7 @@ def create_access_token(data: dict, expires_delta: timedelta | None = None) -> t
to_encode = data.copy()
expires_delta = expires_delta or timedelta(hours=settings.TOKEN_TIME)

expire = datetime.now(timezone.utc) + expires_delta
expire = datetime.now(UTC) + expires_delta

to_encode["exp"] = expire
to_encode["iss"] = ISS
Expand Down
4 changes: 2 additions & 2 deletions mealie/core/security/security.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import secrets
from datetime import datetime, timedelta, timezone
from datetime import UTC, datetime, timedelta
from pathlib import Path

import jwt
Expand Down Expand Up @@ -34,7 +34,7 @@ def create_access_token(data: dict, expires_delta: timedelta | None = None) -> s
to_encode = data.copy()
expires_delta = expires_delta or timedelta(hours=settings.TOKEN_TIME)

expire = datetime.now(timezone.utc) + expires_delta
expire = datetime.now(UTC) + expires_delta

to_encode["exp"] = expire
return jwt.encode(to_encode, settings.SECRET, algorithm=ALGORITHM)
Expand Down
4 changes: 2 additions & 2 deletions mealie/core/settings/settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import os
import secrets
from datetime import datetime, timezone
from datetime import UTC, datetime
from pathlib import Path
from typing import Annotated, Any, NamedTuple

Expand Down Expand Up @@ -160,7 +160,7 @@ def DAILY_SCHEDULE_TIME_UTC(self) -> ScheduleTime:
local_tz = tzlocal()
now = datetime.now(local_tz)
local_time = now.replace(hour=local_hour, minute=local_minute)
utc_time = local_time.astimezone(timezone.utc)
utc_time = local_time.astimezone(UTC)

self.logger.debug(f"Local time: {local_hour}:{local_minute} | UTC time: {utc_time.hour}:{utc_time.minute}")
return ScheduleTime(utc_time.hour, utc_time.minute)
Expand Down
10 changes: 5 additions & 5 deletions mealie/db/models/_model_utils/datetime.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timezone
from datetime import UTC, datetime

from sqlalchemy.types import DateTime, TypeDecorator

Expand All @@ -7,14 +7,14 @@ def get_utc_now():
"""
Returns the current time in UTC.
"""
return datetime.now(timezone.utc)
return datetime.now(UTC)


def get_utc_today():
"""
Returns the current date in UTC.
"""
return datetime.now(timezone.utc).date()
return datetime.now(UTC).date()


class NaiveDateTime(TypeDecorator):
Expand All @@ -35,15 +35,15 @@ def process_bind_param(self, value: datetime | None, dialect):

try:
if value.tzinfo is not None:
value = value.astimezone(timezone.utc)
value = value.astimezone(UTC)
return value.replace(tzinfo=None)
except Exception:
return value

def process_result_value(self, value: datetime | None, dialect):
try:
if value is not None:
value = value.replace(tzinfo=timezone.utc)
value = value.replace(tzinfo=UTC)
except Exception:
pass

Expand Down
4 changes: 2 additions & 2 deletions mealie/db/models/household/shopping_list.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from contextvars import ContextVar
from datetime import datetime, timezone
from datetime import UTC, datetime
from typing import TYPE_CHECKING, Optional

from pydantic import ConfigDict
Expand Down Expand Up @@ -227,7 +227,7 @@ def update_shopping_lists(session: orm.Session, _):
if not shopping_list:
continue

shopping_list.updated_at = datetime.now(timezone.utc)
shopping_list.updated_at = datetime.now(UTC)
local_session.commit()
except Exception:
local_session.rollback()
Expand Down
4 changes: 2 additions & 2 deletions mealie/db/models/household/webhooks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, time, timezone
from datetime import UTC, datetime, time
from typing import TYPE_CHECKING, Optional

from sqlalchemy import Boolean, ForeignKey, String, Time, orm
Expand Down Expand Up @@ -30,7 +30,7 @@ class GroupWebhooksModel(SqlAlchemyBase, BaseMixins):

# New Fields
webhook_type: Mapped[str | None] = mapped_column(String, default="") # Future use for different types of webhooks
scheduled_time: Mapped[time | None] = mapped_column(Time, default=lambda: datetime.now(timezone.utc).time())
scheduled_time: Mapped[time | None] = mapped_column(Time, default=lambda: datetime.now(UTC).time())

# Column is no longer used but is kept for since it's super annoying to
# delete a column in SQLite and it's not a big deal to keep it around
Expand Down
4 changes: 2 additions & 2 deletions mealie/db/models/recipe/recipe.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import date, datetime, timezone
from datetime import UTC, date, datetime
from typing import TYPE_CHECKING

import sqlalchemy as sa
Expand Down Expand Up @@ -207,7 +207,7 @@ def __init__(
if notes:
self.notes = [Note(**n) for n in notes]

self.date_updated = datetime.now(timezone.utc)
self.date_updated = datetime.now(UTC)

# SQLAlchemy events do not seem to register things that are set during auto_init
if name is not None:
Expand Down
4 changes: 2 additions & 2 deletions mealie/db/models/recipe/recipe_timeline.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timezone
from datetime import UTC, datetime
from typing import TYPE_CHECKING

from sqlalchemy import ForeignKey, String
Expand Down Expand Up @@ -48,4 +48,4 @@ def __init__(
timestamp=None,
**_,
) -> None:
self.timestamp = timestamp or datetime.now(timezone.utc)
self.timestamp = timestamp or datetime.now(UTC)
4 changes: 2 additions & 2 deletions mealie/db/models/recipe/shared.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta, timezone
from datetime import UTC, datetime, timedelta
from typing import TYPE_CHECKING
from uuid import uuid4

Expand All @@ -15,7 +15,7 @@


def defaut_expires_at_time() -> datetime:
return datetime.now(timezone.utc) + timedelta(days=30)
return datetime.now(UTC) + timedelta(days=30)


class RecipeShareTokenModel(SqlAlchemyBase, BaseMixins):
Expand Down
4 changes: 2 additions & 2 deletions mealie/repos/repository_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import random
from collections.abc import Iterable
from datetime import datetime, timezone
from datetime import UTC, datetime
from math import ceil
from typing import Any, Generic, TypeVar

Expand Down Expand Up @@ -70,7 +70,7 @@ def household_id(self) -> UUID4 | None:
return self._household_id

def _random_seed(self) -> str:
return str(datetime.now(tz=timezone.utc))
return str(datetime.now(tz=UTC))

def _log_exception(self, e: Exception) -> None:
self.logger.error(f"Error processing query for Repo model={self.model.__name__} schema={self.schema.__name__}")
Expand Down
4 changes: 2 additions & 2 deletions mealie/repos/repository_meals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timezone
from datetime import UTC, datetime

from sqlalchemy import select

Expand All @@ -13,7 +13,7 @@ def get_today(self) -> list[ReadPlanEntry]:
if not self.household_id:
raise Exception("household_id not set")

today = datetime.now(tz=timezone.utc).date()
today = datetime.now(tz=UTC).date()
stmt = select(GroupMealPlan).filter(
GroupMealPlan.date == today, GroupMealPlan.household_id == self.household_id
)
Expand Down
3 changes: 1 addition & 2 deletions mealie/repos/repository_recipes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re as re
from collections.abc import Sequence
from random import randint
from typing import cast
from typing import Self, cast
from uuid import UUID

import sqlalchemy as sa
Expand All @@ -10,7 +10,6 @@
from slugify import slugify
from sqlalchemy import orm
from sqlalchemy.exc import IntegrityError
from typing_extensions import Self

from mealie.db.models.household.household import Household
from mealie.db.models.recipe.category import Category
Expand Down
4 changes: 2 additions & 2 deletions mealie/routes/households/controller_webhooks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timezone
from datetime import UTC, datetime
from functools import cached_property

from fastapi import APIRouter, BackgroundTasks, Depends
Expand Down Expand Up @@ -45,7 +45,7 @@ def rerun_webhooks(self):
"""Manually re-fires all previously scheduled webhooks for today"""

start_time = datetime.min.time()
start_dt = datetime.combine(datetime.now(timezone.utc).date(), start_time)
start_dt = datetime.combine(datetime.now(UTC).date(), start_time)
post_group_webhooks(start_dt=start_dt, group_id=self.group.id, household_id=self.household.id)

@router.get("/{item_id}", response_model=ReadWebhook)
Expand Down
8 changes: 4 additions & 4 deletions mealie/schema/_mealie/datetime_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

import re
from datetime import date, datetime, time, timedelta, timezone
from datetime import UTC, date, datetime, time, timedelta, timezone

date_expr = r"(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})"
time_expr = (
Expand Down Expand Up @@ -39,7 +39,7 @@
r"$"
)

EPOCH = datetime(1970, 1, 1, tzinfo=timezone.utc)
EPOCH = datetime(1970, 1, 1, tzinfo=UTC)
# if greater than this, the number is in ms, if less than or equal it's in seconds
# (in seconds this is 11th October 2603, in ms it's 20th August 1970)
MS_WATERSHED = int(2e10)
Expand Down Expand Up @@ -87,12 +87,12 @@ def from_unix_seconds(seconds: int | float) -> datetime:
while abs(seconds) > MS_WATERSHED:
seconds /= 1000
dt = EPOCH + timedelta(seconds=seconds)
return dt.replace(tzinfo=timezone.utc)
return dt.replace(tzinfo=UTC)


def _parse_timezone(value: str | None, error: type[Exception]) -> None | int | timezone:
if value == "Z":
return timezone.utc
return UTC
elif value is not None:
offset_mins = int(value[-2:]) if len(value) > 3 else 0
offset = 60 * int(value[1:3]) + offset_mins
Expand Down
7 changes: 3 additions & 4 deletions mealie/schema/_mealie/mealie_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

import re
from collections.abc import Sequence
from datetime import datetime, timezone
from datetime import UTC, datetime
from enum import Enum
from typing import ClassVar, Protocol, TypeVar
from typing import ClassVar, Protocol, Self, TypeVar

from humps.main import camelize
from pydantic import UUID4, AliasChoices, BaseModel, ConfigDict, Field, model_validator
from sqlalchemy import Select, desc, func, or_, text
from sqlalchemy.orm import InstrumentedAttribute, Session
from sqlalchemy.orm.interfaces import LoaderOption
from typing_extensions import Self

from mealie.db.models._model_base import SqlAlchemyBase

Expand Down Expand Up @@ -88,7 +87,7 @@ def set_tz_info(self) -> Self:
if not isinstance(val, datetime):
continue
if not val.tzinfo:
setattr(self, field, val.replace(tzinfo=timezone.utc))
setattr(self, field, val.replace(tzinfo=UTC))

return self

Expand Down
2 changes: 1 addition & 1 deletion mealie/schema/household/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def validate_scheduled_time(cls, v):
type: datetime is treated as a value with a timezone
"""
parser_funcs = [
lambda x: parse_datetime(x).astimezone(datetime.timezone.utc).time(),
lambda x: parse_datetime(x).astimezone(datetime.UTC).time(),
parse_time,
]

Expand Down
4 changes: 2 additions & 2 deletions mealie/schema/recipe/recipe_share_token.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta, timezone
from datetime import UTC, datetime, timedelta

from pydantic import UUID4, ConfigDict, Field
from sqlalchemy.orm import selectinload
Expand All @@ -11,7 +11,7 @@


def defaut_expires_at_time() -> datetime:
return datetime.now(timezone.utc) + timedelta(days=30)
return datetime.now(UTC) + timedelta(days=30)


class RecipeShareTokenCreate(MealieModel):
Expand Down
4 changes: 2 additions & 2 deletions mealie/schema/recipe/recipe_timeline_events.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timezone
from datetime import UTC, datetime
from enum import Enum
from pathlib import Path
from typing import Annotated
Expand Down Expand Up @@ -40,7 +40,7 @@ class RecipeTimelineEventIn(MealieModel):
message: str | None = Field(None, alias="eventMessage")
image: Annotated[TimelineEventImage | None, Field(validate_default=True)] = TimelineEventImage.does_not_have_image

timestamp: datetime = datetime.now(timezone.utc)
timestamp: datetime = datetime.now(UTC)
model_config = ConfigDict(use_enum_values=True)


Expand Down
4 changes: 2 additions & 2 deletions mealie/schema/user/user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta, timezone
from datetime import UTC, datetime, timedelta
from pathlib import Path
from typing import Annotated, Any, Generic, TypeVar
from uuid import UUID
Expand Down Expand Up @@ -218,7 +218,7 @@ def is_locked(self) -> bool:
return False

lockout_expires_at = self.locked_at + timedelta(hours=get_app_settings().SECURITY_USER_LOCKOUT_TIME)
return lockout_expires_at > datetime.now(timezone.utc)
return lockout_expires_at > datetime.now(UTC)

def directory(self) -> Path:
return PrivateUser.get_directory(self.id)
Expand Down
4 changes: 2 additions & 2 deletions mealie/services/backups_v2/backup_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def _sqlite(self) -> None:
db_file = self.settings.DB_URL.removeprefix("sqlite:///") # type: ignore

# Create a backup of the SQLite database
timestamp = datetime.datetime.now(datetime.timezone.utc).strftime("%Y.%m.%d")
timestamp = datetime.datetime.now(datetime.UTC).strftime("%Y.%m.%d")
shutil.copy(db_file, self.directories.DATA_DIR.joinpath(f"mealie_{timestamp}.bak.db"))

def _postgres(self) -> None:
Expand All @@ -37,7 +37,7 @@ def backup(self) -> Path:
exclude_ext = {".zip"}
exclude_dirs = {"backups", ".temp"}

timestamp = datetime.datetime.now(datetime.timezone.utc).strftime("%Y.%m.%d.%H.%M.%S")
timestamp = datetime.datetime.now(datetime.UTC).strftime("%Y.%m.%d.%H.%M.%S")

backup_name = f"mealie_{timestamp}.zip"
backup_file = self.directories.BACKUP_DIR / backup_name
Expand Down
6 changes: 3 additions & 3 deletions mealie/services/event_bus_service/event_bus_listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
from abc import ABC, abstractmethod
from collections.abc import Generator
from datetime import datetime, timezone
from datetime import UTC, datetime
from typing import cast
from urllib.parse import parse_qs, urlencode, urlsplit, urlunsplit

Expand Down Expand Up @@ -163,8 +163,8 @@ def get_scheduled_webhooks(self, start_dt: datetime, end_dt: datetime) -> list[R
with self.ensure_session() as session:
stmt = select(GroupWebhooksModel).where(
GroupWebhooksModel.enabled == True, # noqa: E712 - required for SQLAlchemy comparison
GroupWebhooksModel.scheduled_time > start_dt.astimezone(timezone.utc).time(),
GroupWebhooksModel.scheduled_time <= end_dt.astimezone(timezone.utc).time(),
GroupWebhooksModel.scheduled_time > start_dt.astimezone(UTC).time(),
GroupWebhooksModel.scheduled_time <= end_dt.astimezone(UTC).time(),
GroupWebhooksModel.group_id == self.group_id,
GroupWebhooksModel.household_id == self.household_id,
)
Expand Down
Loading

0 comments on commit 5840237

Please sign in to comment.