Skip to content

Commit

Permalink
Address various deprecations.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielballan committed Sep 19, 2024
1 parent c7caf8b commit d6c166d
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion tiled/_tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def populate_internal(client):
awkward.Array([1, [2, 3]]), key="d", metadata={"color": "red"}, specs=["alpha"]
)
# sparse
coo = sparse.COO(coords=[[2, 5]], data=[1.3, 7.5], shape=(10,))
coo = sparse.COO(coords=numpy.array([[2, 5]]), data=[1.3, 7.5], shape=(10,))
client.write_sparse(key="e", coords=coo.coords, data=coo.data, shape=coo.shape)

# nested
Expand Down
12 changes: 6 additions & 6 deletions tiled/authn_database/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime
import hashlib
import uuid as uuid_module
from datetime import datetime

from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
Expand Down Expand Up @@ -77,7 +77,7 @@ async def purge_expired(db, cls):
"""
Remove expired entries.
"""
now = datetime.utcnow()
now = datetime.datetime.now(datetime.UTC)
num_expired = 0
statement = (
select(cls)
Expand Down Expand Up @@ -146,7 +146,7 @@ async def lookup_valid_session(db, session_id):
return None
if (
session.expiration_time is not None
and session.expiration_time < datetime.utcnow()
and session.expiration_time < datetime.datetime.now(datetime.UTC)
):
await db.delete(session)
await db.commit()
Expand All @@ -171,7 +171,7 @@ async def lookup_valid_pending_session_by_device_code(db, device_code):
return None
if (
pending_session.expiration_time is not None
and pending_session.expiration_time < datetime.utcnow()
and pending_session.expiration_time < datetime.datetime.now(datetime.UTC)
):
await db.delete(pending_session)
await db.commit()
Expand All @@ -189,7 +189,7 @@ async def lookup_valid_pending_session_by_user_code(db, user_code):
return None
if (
pending_session.expiration_time is not None
and pending_session.expiration_time < datetime.utcnow()
and pending_session.expiration_time < datetime.datetime.now(datetime.UTC)
):
await db.delete(pending_session)
await db.commit()
Expand Down Expand Up @@ -228,7 +228,7 @@ async def lookup_valid_api_key(db, secret):
Look up an API key. Ensure that it is valid.
"""

now = datetime.utcnow()
now = datetime.datetime.now(datetime.UTC)
hashed_secret = hashlib.sha256(secret).digest()
api_key = (
await db.execute(
Expand Down
2 changes: 1 addition & 1 deletion tiled/client/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def __init__(

# verify parameter is dropped, as there is no SSL in ASGI mode
client = TestClient(
app=app,
transport=httpx.ASGITransport(app=app),
raise_server_exceptions=raise_server_exceptions,
)
client.timeout = timeout
Expand Down
2 changes: 1 addition & 1 deletion tiled/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ async def index(
principal=Security(get_current_principal, scopes=[]),
):
return templates.TemplateResponse(
request,
"index.html",
{
"request": request,
# This is used to construct the link to the React UI.
"root_url": get_root_url(request),
# If defined, this adds a Binder link to the page.
Expand Down
12 changes: 7 additions & 5 deletions tiled/server/authentication.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import datetime
import enum
import hashlib
import secrets
import uuid as uuid_module
import warnings
from datetime import datetime, timedelta
from pathlib import Path
from typing import Optional

Expand Down Expand Up @@ -67,7 +67,7 @@
from .utils import API_KEY_COOKIE_NAME, get_authenticators, get_base_url

ALGORITHM = "HS256"
UNIT_SECOND = timedelta(seconds=1)
UNIT_SECOND = datetime.timedelta(seconds=1)

# Max API keys and Sessions allowed to Principal.
# This is here for at least two reasons:
Expand All @@ -77,13 +77,13 @@
API_KEY_LIMIT = 100
SESSION_LIMIT = 200

DEVICE_CODE_MAX_AGE = timedelta(minutes=15)
DEVICE_CODE_MAX_AGE = datetime.timedelta(minutes=15)
DEVICE_CODE_POLLING_INTERVAL = 5 # seconds


def utcnow():
"UTC now with second resolution"
return datetime.utcnow().replace(microsecond=0)
return datetime.datetime.now(datetime.UTC).replace(microsecond=0)


class Mode(enum.Enum):
Expand Down Expand Up @@ -759,7 +759,9 @@ async def generate_apikey(db, principal, apikey_params, request):
),
)
if apikey_params.expires_in is not None:
expiration_time = utcnow() + timedelta(seconds=apikey_params.expires_in)
expiration_time = utcnow() + datetime.timedelta(
seconds=apikey_params.expires_in
)
else:
expiration_time = None
# The standard 32 byes of entropy,
Expand Down
6 changes: 4 additions & 2 deletions tiled/server/core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import collections.abc
import dataclasses
import datetime
import itertools
import math
import operator
Expand All @@ -8,7 +9,6 @@
import types
import uuid
from collections import defaultdict
from datetime import datetime, timedelta
from hashlib import md5
from typing import Any

Expand Down Expand Up @@ -225,7 +225,9 @@ async def construct_entries_response(
keys = tree.keys()[offset : offset + limit] # noqa: E203
items = [(key, None) for key in keys]
# This value will not leak out. It just used to seed comparisons.
metadata_stale_at = datetime.utcnow() + timedelta(days=1_000_000)
metadata_stale_at = datetime.datetime.now(datetime.UTC) + datetime.timedelta(
days=1_000_000
)
must_revalidate = getattr(tree, "must_revalidate", True)
for key, entry in items:
resource = await construct_resource(
Expand Down
4 changes: 2 additions & 2 deletions tiled/server/router.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import dataclasses
import datetime
import inspect
import os
import re
import warnings
from datetime import datetime, timedelta
from functools import partial
from pathlib import Path
from typing import Any, List, Optional
Expand Down Expand Up @@ -149,7 +149,7 @@ async def about(
},
meta={"root_path": request.scope.get("root_path") or "" + "/api"},
).model_dump(),
expires=datetime.utcnow() + timedelta(seconds=600),
expires=datetime.datetime.now(datetime.UTC) + datetime.timedelta(seconds=600),
)


Expand Down

0 comments on commit d6c166d

Please sign in to comment.