Skip to content

Commit

Permalink
fix: use datetime.now(timezone.utc) instead of utcnow()
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael0202 committed Sep 17, 2024
1 parent 459f6f4 commit 2203ecf
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 37 deletions.
8 changes: 4 additions & 4 deletions robotoff/app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ class ImagePredictionImporterResource:
@jsonschema.validate(schema.IMAGE_PREDICTION_IMPORTER_SCHEMA)
def on_post(self, req: falcon.Request, resp: falcon.Response):
server_type = get_server_type_from_req(req)
timestamp = datetime.datetime.utcnow()
timestamp = datetime.datetime.now(datetime.timezone.utc)
inserts = []
media = req.get_media()

Expand Down Expand Up @@ -1077,7 +1077,7 @@ def on_put(self, req: falcon.Request, resp: falcon.Response, logo_id: int):
annotated_logos = update_logo_annotations(
[(type_, value, logo)],
username=auth.get_username() or "unknown",
completed_at=datetime.datetime.utcnow(),
completed_at=datetime.datetime.now(datetime.timezone.utc),
)
server_type = ServerType[logo.image_prediction.image.server_type]
generate_insights_from_annotated_logos(
Expand Down Expand Up @@ -1151,7 +1151,7 @@ def on_post(self, req: falcon.Request, resp: falcon.Response):
)
server_type = get_server_type_from_req(req)
annotations = media["annotations"]
completed_at = datetime.datetime.utcnow()
completed_at = datetime.datetime.now(datetime.timezone.utc)
annotation_logos = []

with db.atomic():
Expand Down Expand Up @@ -1209,7 +1209,7 @@ def on_post(self, req: falcon.Request, resp: falcon.Response):

auth = parse_auth(req)
username = None if auth is None else auth.get_username()
completed_at = datetime.datetime.utcnow()
completed_at = datetime.datetime.now(datetime.timezone.utc)

target_value_tag = get_tag(target_value)
source_value_tag = get_tag(source_value)
Expand Down
2 changes: 1 addition & 1 deletion robotoff/cli/logos.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def import_logos(

for batch in chunked(tqdm.tqdm(jsonl_iter(data_path)), batch_size):
with db.atomic():
timestamp = datetime.datetime.utcnow()
timestamp = datetime.datetime.now(datetime.timezone.utc)
for item in batch:
barcode = item["barcode"]
product_id = ProductIdentifier(barcode, server_type)
Expand Down
2 changes: 1 addition & 1 deletion robotoff/insights/annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def _annotate(

insight.username = username
insight.annotation = annotation
insight.completed_at = datetime.datetime.utcnow()
insight.completed_at = datetime.datetime.now(datetime.timezone.utc)

if annotation in (1, 2) and update:
# Save insight before processing the annotation
Expand Down
2 changes: 1 addition & 1 deletion robotoff/insights/extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def run_object_detection_model(
return None
return existing_image_prediction

timestamp = datetime.datetime.utcnow()
timestamp = datetime.datetime.now(datetime.timezone.utc)
results = ObjectDetectionModelRegistry.get(model_name.value).detect_from_image(
image, output_image=False, triton_uri=triton_uri, threshold=threshold
)
Expand Down
4 changes: 2 additions & 2 deletions robotoff/insights/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def generate_insights(
It calls the `generate_candidates` method, specific to each insight
type (and implemented in sub-classes).
"""
timestamp = datetime.datetime.utcnow()
timestamp = datetime.datetime.now(datetime.timezone.utc)

product = product_store[product_id]
references = get_existing_insight(cls.get_type(), product_id)
Expand Down Expand Up @@ -1776,7 +1776,7 @@ def import_product_predictions(
:return: a (imported, deleted) tuple: the number of predictions imported
and deleted in DB.
"""
timestamp = datetime.datetime.utcnow()
timestamp = datetime.datetime.now(datetime.timezone.utc)

deleted = 0
if delete_previous_versions:
Expand Down
2 changes: 1 addition & 1 deletion robotoff/logos.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def save_nearest_neighbors(
logo_embedding.logo.nearest_neighbors = {
"distances": distances,
"logo_ids": logo_ids,
"updated_at": datetime.datetime.utcnow().isoformat(),
"updated_at": datetime.datetime.now(datetime.timezone.utc).isoformat(),
}
updated.append(logo_embedding.logo)

Expand Down
5 changes: 3 additions & 2 deletions robotoff/scheduler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def process_insights() -> None:
.where(
ProductInsight.annotation.is_null(),
ProductInsight.process_after.is_null(False),
ProductInsight.process_after <= datetime.datetime.utcnow(),
ProductInsight.process_after
<= datetime.datetime.now(datetime.timezone.utc),
)
.iterator()
):
Expand Down Expand Up @@ -99,7 +100,7 @@ def refresh_insights(with_deletion: bool = True) -> None:
# Only OFF is currently supported
server_type = ServerType.off

datetime_threshold = datetime.datetime.utcnow().replace(
datetime_threshold = datetime.datetime.now(datetime.timezone.utc).replace(
hour=0, minute=0, second=0, microsecond=0
)
dataset_datetime = datetime.datetime.fromtimestamp(
Expand Down
4 changes: 2 additions & 2 deletions robotoff/workers/tasks/import_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def run_upc_detection(product_id: ProductIdentifier, image_url: str) -> None:
"class": prediction_class.value,
},
max_confidence=None,
timestamp=datetime.datetime.utcnow(),
timestamp=datetime.datetime.now(datetime.timezone.utc),
)

# no prediction neccessary if the image is not a UPC Image
Expand Down Expand Up @@ -684,7 +684,7 @@ def extract_ingredients_job(
model_name=ingredient_list.MODEL_NAME,
model_version=ingredient_list.MODEL_VERSION,
data=ingredient_prediction_data,
timestamp=datetime.datetime.utcnow(),
timestamp=datetime.datetime.now(datetime.timezone.utc),
max_confidence=(
max(entity.score for entity in entities) if entities else None
),
Expand Down
2 changes: 1 addition & 1 deletion scripts/insert_image_predictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def insert_batch(
model_version: str,
server_type: ServerType,
) -> int:
timestamp = datetime.datetime.utcnow()
timestamp = datetime.datetime.now(datetime.timezone.utc)
logger.info("Loading seen set...")
seen_set = get_seen_set(server_type)
logger.info("Seen set loaded")
Expand Down
18 changes: 9 additions & 9 deletions tests/integration/insights/test_process_insights.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

import pytest

Expand Down Expand Up @@ -34,7 +34,7 @@ def _create_insight(**kwargs):
"type": "category",
"value_tag": "en:Salmons",
"automatic_processing": True,
"process_after": datetime.utcnow() - timedelta(minutes=12),
"process_after": datetime.now(timezone.utc) - timedelta(minutes=12),
"n_votes": 3,
"server_type": DEFAULT_SERVER_TYPE.name,
},
Expand All @@ -50,7 +50,7 @@ def test_process_insight_category(mocker, peewee_db):
)
mock = mocker.patch("robotoff.off.update_product")
# a processed insight exists
date0 = datetime.utcnow() - timedelta(minutes=10)
date0 = datetime.now(timezone.utc) - timedelta(minutes=10)
with peewee_db:
id0, code0 = _create_insight(type="category", completed_at=date0, annotation=1)
# an insight to be processed
Expand All @@ -63,7 +63,7 @@ def test_process_insight_category(mocker, peewee_db):
# insight 1 processed
insight = ProductInsight.get(id=id1)
assert insight.completed_at is not None
assert insight.completed_at <= datetime.utcnow()
assert insight.completed_at <= datetime.now(timezone.utc)
assert insight.annotation == 1
# update_product calledfor item 1
mock.assert_called_once_with(
Expand Down Expand Up @@ -93,7 +93,7 @@ def test_process_insight_category_existing(mocker, peewee_db):
with peewee_db:
insight = ProductInsight.get(id=id1)
assert insight.completed_at is not None
assert insight.completed_at <= datetime.utcnow()
assert insight.completed_at <= datetime.now(timezone.utc)
assert insight.annotation == 1
# but update_product wasn't called
mock.assert_not_called()
Expand All @@ -112,7 +112,7 @@ def test_process_insight_non_existing_product(mocker, peewee_db):
with peewee_db:
insight = ProductInsight.get(id=id1)
assert insight.completed_at is not None
assert insight.completed_at <= datetime.utcnow()
assert insight.completed_at <= datetime.now(timezone.utc)
assert insight.annotation == 1
# but update_product wasn't called
mock.assert_not_called()
Expand All @@ -136,9 +136,9 @@ def raise_for_salmons(params, *args, **kwargs):
# add another insight that should pass
id2, code2 = _create_insight(type="category", value_tag="en:Tuna")
# run process
start = datetime.utcnow()
start = datetime.now(timezone.utc)
process_insights()
end = datetime.utcnow()
end = datetime.now(timezone.utc)

with peewee_db:
# insight1 not marked processed
Expand Down Expand Up @@ -198,7 +198,7 @@ def test_process_insight_same_product(mocker, peewee_db):
for id_ in [id1, id2, id3]:
insight = ProductInsight.get(id=id_)
assert insight.completed_at is not None
assert insight.completed_at <= datetime.utcnow()
assert insight.completed_at <= datetime.now(timezone.utc)
assert insight.annotation == 1
# update_product was called twice
assert mock.call_count == 2
Expand Down
10 changes: 5 additions & 5 deletions tests/integration/test_annotate_image.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import base64
from datetime import datetime
import datetime
from typing import Any

import falcon.testing
Expand Down Expand Up @@ -145,7 +145,7 @@ def test_logo_annotation_brand(client, peewee_db, monkeypatch, mocker, fake_taxo
annotation_type=None,
)
mocker.patch("robotoff.app.api.enqueue_job", return_value=None)
start = datetime.utcnow()
start = datetime.datetime.now(datetime.timezone.utc)
result = client.simulate_post(
"/api/v1/images/logos/annotate",
json={
Expand All @@ -154,7 +154,7 @@ def test_logo_annotation_brand(client, peewee_db, monkeypatch, mocker, fake_taxo
},
headers=_AUTH_HEADER,
)
end = datetime.utcnow()
end = datetime.datetime.now(datetime.timezone.utc)
assert result.status_code == 200
assert result.json == {"annotated": 1}

Expand All @@ -180,7 +180,7 @@ def test_logo_annotation_label(client, peewee_db, monkeypatch, fake_taxonomy, mo
barcode=barcode, source_image=source_image, annotation_type=None
)
mocker.patch("robotoff.app.api.enqueue_job", return_value=None)
start = datetime.utcnow()
start = datetime.datetime.now(datetime.timezone.utc)
result = client.simulate_post(
"/api/v1/images/logos/annotate",
json={
Expand All @@ -191,7 +191,7 @@ def test_logo_annotation_label(client, peewee_db, monkeypatch, fake_taxonomy, mo
},
headers=_AUTH_HEADER,
)
end = datetime.utcnow()
end = datetime.datetime.now(datetime.timezone.utc)
assert result.status_code == 200
assert result.json == {"annotated": 1}
with peewee_db:
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import base64
import datetime
import uuid
from datetime import datetime

import pytest
import requests
Expand Down Expand Up @@ -568,7 +568,7 @@ def test_annotate_insight_anonymous_then_authenticated(client, mocker, peewee_db
# we still have the vote, but we also have an authenticated validation
assert insight.items() > {"username": "a", "n_votes": 1, "annotation": 1}.items()
assert insight.get("completed_at") is not None
assert insight.get("completed_at") <= datetime.utcnow()
assert insight.get("completed_at") <= datetime.datetime.now(datetime.timezone.utc)
# update was done
add_category.assert_called_once_with(
DEFAULT_PRODUCT_ID,
Expand Down
15 changes: 10 additions & 5 deletions tests/integration/test_logos.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
import datetime

import robotoff.insights.importer
import robotoff.taxonomy
Expand All @@ -23,7 +23,12 @@ def _fake_store(monkeypatch, product_id: ProductIdentifier):
"code": product_id.barcode, # needed to validate brand/label
# needed to validate image
"images": {
"2": {"rev": 1, "uploaded_t": datetime.utcnow().timestamp()}
"2": {
"rev": 1,
"uploaded_t": datetime.datetime.now(
datetime.timezone.utc
).timestamp(),
}
},
}
)
Expand Down Expand Up @@ -55,13 +60,13 @@ def test_generate_insights_from_annotated_logos_job(peewee_db, monkeypatch, mock
username=username,
)

start = datetime.utcnow()
start = datetime.datetime.now(datetime.timezone.utc)
generate_insights_from_annotated_logos_job(
[ann.id],
OFFAuthentication(username=username, password=username),
server_type=DEFAULT_SERVER_TYPE,
)
end = datetime.utcnow()
end = datetime.datetime.now(datetime.timezone.utc)
# we generate a prediction

with peewee_db:
Expand Down Expand Up @@ -115,4 +120,4 @@ def test_generate_insights_from_annotated_logos_job(peewee_db, monkeypatch, mock
assert insight.annotation == 1
assert insight.annotated_result == 2
assert insight.server_type == DEFAULT_SERVER_TYPE.name
assert isinstance(insight.completed_at, datetime)
assert isinstance(insight.completed_at, datetime.datetime)
2 changes: 1 addition & 1 deletion tests/unit/insights/test_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ def get_insight_update(cls, candidates, references):
"images": {
"8": {
"uploaded_t": (
datetime.datetime.utcnow()
datetime.datetime.now(datetime.timezone.utc)
- datetime.timedelta(days=600)
).timestamp()
}
Expand Down

0 comments on commit 2203ecf

Please sign in to comment.