Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address warnings in tests #842

Merged
merged 9 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ Write the date in place of the "Unreleased" in the case a new version is release

# Changelog

## Unreleased

### Maintenance

- Addressed DeprecationWarnings from Python and dependencies

## v0.1.0-b13 (2024-01-09)

### Added
Expand Down
4 changes: 2 additions & 2 deletions tiled/_tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ def test_write_dataframe_internal_via_client(client):

def test_write_xarray_dataset(client):
ds = xarray.Dataset(
{"temp": (["time"], [101, 102, 103])},
coords={"time": (["time"], [1, 2, 3])},
{"temp": (["time"], numpy.array([101, 102, 103]))},
coords={"time": (["time"], numpy.array([1, 2, 3]))},
)
dsc = write_xarray_dataset(client, ds, key="test_xarray_dataset")
assert set(dsc) == {"temp", "time"}
Expand Down
4 changes: 3 additions & 1 deletion tiled/_tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
coords={
"lon": (["x", "y"], lon),
"lat": (["x", "y"], lat),
"time": [1, 2, 3], # using ints here so HDF5 can export
"time": numpy.array(
[1, 2, 3]
), # using ints here so HDF5 can export
},
)
),
Expand Down
4 changes: 2 additions & 2 deletions tiled/_tests/test_inlined_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
{
"dataset": DatasetAdapter.from_dataset(
xarray.Dataset(
data_vars={"temperature": ("time", [100, 99, 98])},
coords={"time": [1, 2, 3]},
data_vars={"temperature": ("time", numpy.array([100, 99, 98]))},
coords={"time": numpy.array([1, 2, 3])},
)
),
},
Expand Down
4 changes: 3 additions & 1 deletion tiled/_tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ 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=numpy.array([1.3, 7.5]), shape=(10,)
)
client.write_sparse(key="e", coords=coo.coords, data=coo.data, shape=coo.shape)

# nested
Expand Down
18 changes: 14 additions & 4 deletions tiled/_tests/test_writing.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,14 @@ def test_write_dataframe_dict(tree):
@pytest.mark.parametrize(
"coo",
[
sparse.COO(coords=[[2, 5]], data=[1.3, 7.5], shape=(10,)),
sparse.COO(coords=[[0, 1], [2, 3]], data=[3.8, 4.0], shape=(4, 4)),
sparse.COO(
coords=numpy.array([[2, 5]]), data=numpy.array([1.3, 7.5]), shape=(10,)
),
sparse.COO(
coords=numpy.array([[0, 1], [2, 3]]),
data=numpy.array([3.8, 4.0]),
shape=(4, 4),
),
],
)
def test_write_sparse_full(tree, coo):
Expand Down Expand Up @@ -317,7 +323,9 @@ def test_write_sparse_chunked(tree):
assert numpy.array_equal(
result_array.todense(),
sparse.COO(
coords=[[2, 4, N + 0, N + 1]], data=[3.1, 2.8, 6.7, 1.2], shape=(10,)
coords=numpy.array([[2, 4, N + 0, N + 1]]),
data=numpy.array([3.1, 2.8, 6.7, 1.2]),
shape=(10,),
).todense(),
)

Expand Down Expand Up @@ -543,7 +551,9 @@ async def test_write_in_container(tree):
client.delete("a")

a = client.create_container("a")
coo = sparse.COO(coords=[[2, 5]], data=[1.3, 7.5], shape=(10,))
coo = sparse.COO(
coords=numpy.array([[2, 5]]), data=numpy.array([1.3, 7.5]), shape=(10,)
)
b = a.write_sparse(coords=coo.coords, data=coo.data, shape=coo.shape, key="b")
b.read()
a.delete("b")
Expand Down
6 changes: 3 additions & 3 deletions tiled/adapters/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import operator
import sys
from collections import Counter
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -286,7 +286,7 @@ def metadata_stale_at(self) -> Optional[datetime]:
"""
if self.metadata_stale_after is None:
return None
return self.metadata_stale_after + datetime.utcnow()
return self.metadata_stale_after + datetime.now(timezone.utc)

@property
def entries_stale_at(self) -> Optional[datetime]:
Expand All @@ -298,7 +298,7 @@ def entries_stale_at(self) -> Optional[datetime]:
"""
if self.entries_stale_after is None:
return None
return self.entries_stale_after + datetime.utcnow()
return self.entries_stale_after + datetime.now(timezone.utc)

def new_variation(
self,
Expand Down
25 changes: 14 additions & 11 deletions tiled/authn_database/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import hashlib
import uuid as uuid_module
from datetime import datetime
from datetime import datetime, timezone

from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
Expand Down Expand Up @@ -77,12 +77,12 @@ async def purge_expired(db, cls):
"""
Remove expired entries.
"""
now = datetime.utcnow()
now = datetime.now(timezone.utc)
num_expired = 0
statement = (
select(cls)
.filter(cls.expiration_time.is_not(None))
.filter(cls.expiration_time < now)
.filter(cls.expiration_time.replace(tzinfo=timezone.utc) < now)
)
result = await db.execute(statement)
for obj in result.scalars():
Expand Down Expand Up @@ -144,10 +144,9 @@ async def lookup_valid_session(db, session_id):
).scalar()
if session is None:
return None
if (
session.expiration_time is not None
and session.expiration_time < datetime.utcnow()
):
if session.expiration_time is not None and session.expiration_time.replace(
tzinfo=timezone.utc
) < datetime.now(timezone.utc):
await db.delete(session)
await db.commit()
return None
Expand All @@ -171,7 +170,8 @@ 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.replace(tzinfo=timezone.utc)
< datetime.now(timezone.utc)
):
await db.delete(pending_session)
await db.commit()
Expand All @@ -189,7 +189,8 @@ 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.replace(tzinfo=timezone.utc)
< datetime.now(timezone.utc)
):
await db.delete(pending_session)
await db.commit()
Expand Down Expand Up @@ -228,7 +229,7 @@ async def lookup_valid_api_key(db, secret):
Look up an API key. Ensure that it is valid.
"""

now = datetime.utcnow()
now = datetime.now(timezone.utc)
hashed_secret = hashlib.sha256(secret).digest()
api_key = (
await db.execute(
Expand All @@ -244,7 +245,9 @@ async def lookup_valid_api_key(db, secret):
if api_key is None:
# No match
validated_api_key = None
elif (api_key.expiration_time is not None) and (api_key.expiration_time < now):
elif (api_key.expiration_time is not None) and (
api_key.expiration_time.replace(tzinfo=timezone.utc) < now
):
# Match is expired. Delete it.
await db.delete(api_key)
await db.commit()
Expand Down
7 changes: 4 additions & 3 deletions tiled/client/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,8 @@ def write_sparse(
Write a sparse.COO array.

>>> import sparse
>>> coo = sparse.COO(coords=[[2, 5]], data=[1.3, 7.5], shape=(10,))
>>> from numpy import array
>>> coo = sparse.COO(coords=array([[2, 5]]), data=array([1.3, 7.5]), shape=(10,))
>>> c.write_sparse(coords=coo.coords, data=coo.data, shape=coo.shape)

This only supports a single chunk. For chunked upload, use lower-level methods.
Expand All @@ -891,8 +892,8 @@ def write_sparse(
>>> x = c.new("sparse", [data_source])
# Upload the data in each chunk.
# Coords are given with in the reference frame of each chunk.
>>> x.write_block(coords=[[2, 4]], data=[3.1, 2.8], block=(0,))
>>> x.write_block(coords=[[0, 1]], data=[6.7, 1.2], block=(1,))
>>> x.write_block(coords=array([[2, 4]]), data=array([3.1, 2.8]), block=(0,))
>>> x.write_block(coords=array([[0, 1]]), data=array([6.7, 1.2]), block=(1,))
"""
from ..structures.sparse import COOStructure

Expand Down
2 changes: 1 addition & 1 deletion tiled/examples/xdi.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def read_xdi(data_uri, structure=None, metadata=None, specs=None, access_policy=

# TODO validate

df = pd.read_table(file, delim_whitespace=True, names=col_labels)
df = pd.read_table(file, sep=r"\s+", names=col_labels)

return DataFrameAdapter.from_pandas(
df,
Expand Down
3 changes: 1 addition & 2 deletions tiled/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ async def lookup_file(path, try_app=True):
return FileResponse(
full_path,
stat_result=stat_result,
method="GET",
status_code=HTTP_200_OK,
)

Expand All @@ -270,9 +269,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
18 changes: 11 additions & 7 deletions tiled/server/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import secrets
import uuid as uuid_module
import warnings
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from pathlib import Path
from typing import Optional

Expand Down Expand Up @@ -83,7 +83,7 @@

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


class Mode(enum.Enum):
Expand Down Expand Up @@ -587,9 +587,9 @@ async def route(
f"{get_base_url(request)}/auth/provider/{provider}/device_code?code={code}"
)
return templates.TemplateResponse(
request,
"device_code_form.html",
{
"request": request,
"code": code,
"action": action,
},
Expand Down Expand Up @@ -627,9 +627,9 @@ async def route(
if pending_session is None:
message = "Invalid user code. It may have been mistyped, or the pending request may have expired."
return templates.TemplateResponse(
request,
"device_code_form.html",
{
"request": request,
"code": code,
"action": action,
"message": message,
Expand All @@ -639,9 +639,9 @@ async def route(
user_session_state = await authenticator.authenticate(request)
if not user_session_state:
return templates.TemplateResponse(
request,
"device_code_failure.html",
{
"request": request,
"message": (
"User code was correct but authentication with third party failed. "
"Ask administrator to see logs for details."
Expand All @@ -660,9 +660,9 @@ async def route(
db.add(pending_session)
await db.commit()
return templates.TemplateResponse(
request,
"device_code_success.html",
{
"request": request,
"interval": DEVICE_CODE_POLLING_INTERVAL,
},
)
Expand Down Expand Up @@ -1038,7 +1038,11 @@ async def slide_session(refresh_token, settings, db):
now = utcnow()
# This token is *signed* so we know that the information came from us.
# If the Session is forgotten or revoked or expired, do not allow refresh.
if (session is None) or session.revoked or (session.expiration_time < now):
if (
(session is None)
or session.revoked
or (session.expiration_time.replace(tzinfo=timezone.utc) < now)
):
# Do not leak (to a potential attacker) whether this has been *revoked*
# specifically. Give the same error as if it had expired.
raise HTTPException(
Expand Down
4 changes: 2 additions & 2 deletions tiled/server/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import types
import uuid
from collections import defaultdict
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from hashlib import md5
from typing import Any

Expand Down Expand Up @@ -225,7 +225,7 @@ 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.now(timezone.utc) + timedelta(days=1_000_000)
must_revalidate = getattr(tree, "must_revalidate", True)
for key, entry in items:
resource = await construct_resource(
Expand Down
5 changes: 2 additions & 3 deletions tiled/server/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import re
import warnings
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from functools import partial
from pathlib import Path
from typing import Any, List, Optional
Expand Down Expand Up @@ -151,7 +151,7 @@ async def about(
},
meta={"root_path": request.scope.get("root_path") or "" + "/api"},
).model_dump(),
expires=datetime.utcnow() + timedelta(seconds=600),
expires=datetime.now(timezone.utc) + timedelta(seconds=600),
)


Expand Down Expand Up @@ -1646,7 +1646,6 @@ async def get_asset(
return FileResponseWithRange(
full_path,
stat_result=stat_result,
method="GET",
status_code=status_code,
headers={"Content-Disposition": f'attachment; filename="{filename}"'},
range=range,
Expand Down
Loading