Skip to content

Commit 99ba7a6

Browse files
committed
Fix mypy errors
1 parent 9f6515c commit 99ba7a6

File tree

5 files changed

+30
-9
lines changed

5 files changed

+30
-9
lines changed

src/emsarray/conventions/ugrid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ def edge_count(self) -> int:
10061006
return self.dataset.sizes[self.edge_dimension]
10071007

10081008
# By computing the edge_node array we can determine how many edges exist
1009-
return self.edge_node_array.shape[0]
1009+
return cast(int, self.edge_node_array.shape[0])
10101010

10111011
@property
10121012
def face_count(self) -> int:

src/emsarray/operations/cache.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"""
2020
import hashlib
2121
import marshal
22+
from typing import cast
2223

2324
import numpy
2425
import xarray
@@ -146,7 +147,7 @@ def make_cache_key(dataset: xarray.Dataset, hash: "hashlib._Hash | None" = None)
146147
and should not be relied upon.
147148
"""
148149
if hash is None:
149-
hash = hashlib.blake2b(digest_size=32)
150+
hash = cast("hashlib._Hash", hashlib.blake2b(digest_size=32))
150151

151152
dataset.ems.hash_geometry(hash)
152153

src/emsarray/operations/geometry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pathlib
88
from collections.abc import Generator, Iterable, Iterator
99
from contextlib import contextmanager
10-
from typing import IO, Any, TypeVar
10+
from typing import IO, Any, Generic, TypeVar
1111

1212
import geojson
1313
import shapefile
@@ -19,7 +19,7 @@
1919
T = TypeVar('T')
2020

2121

22-
class _dumpable_iterator(list):
22+
class _dumpable_iterator(Generic[T], list):
2323
"""
2424
Wrap an iterator / generator so it can be used in `json.dumps()`.
2525
No guarantees that it works for anything else!

src/emsarray/operations/triangulate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,15 @@ def _add_triangles(face_index: int, vertex_triangles: numpy.ndarray) -> None:
158158
vertex_triangles = _triangulate_polygons_by_length(same_length_polygons)
159159

160160
for face_index, triangles in zip(same_length_face_indices, vertex_triangles):
161-
_add_triangles(face_index, triangles)
161+
_add_triangles(int(face_index), triangles)
162162

163163
# Triangulate each concave polygon using a slower manual method.
164164
# Anecdotally concave polygons are very rare,
165165
# so using a slower method isn't an issue.
166166
for face_index in polygon_is_concave:
167167
polygon = polygons[face_index]
168168
triangles = _triangulate_concave_polygon(polygon)
169-
_add_triangles(face_index, triangles)
169+
_add_triangles(int(face_index), triangles)
170170

171171
# Check that we have handled each triangle we expected.
172172
assert current_face == total_triangles

src/emsarray/utils.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def fix_time_units_for_ems(
228228
"""
229229

230230
with netCDF4.Dataset(dataset_path, 'r+') as dataset:
231-
variable = dataset.variables[variable_name]
231+
variable = dataset.variables[str(variable_name)]
232232

233233
units = cast(str, variable.getncattr('units'))
234234
calendar = cast(str, variable.getncattr('calendar') or DEFAULT_CALENDAR)
@@ -668,7 +668,11 @@ def wind_dimension(
668668
return xarray.DataArray(data=new_data, dims=new_dims)
669669

670670

671-
def datetime_from_np_time(np_time: numpy.datetime64) -> datetime.datetime:
671+
def datetime_from_np_time(
672+
np_time: numpy.datetime64,
673+
*,
674+
tz: datetime.tzinfo = datetime.timezone.utc,
675+
) -> datetime.datetime:
672676
"""
673677
Convert a numpy :class:`~numpy.datetime64`
674678
to a python :class:`~datetime.datetime`.
@@ -682,8 +686,24 @@ def datetime_from_np_time(np_time: numpy.datetime64) -> datetime.datetime:
682686
A conversion that truncates data is not reported as an error.
683687
If you're using numpy datetime64 with attosecond accuracy,
684688
the Python datetime formatting methods are insufficient for your needs anyway.
689+
690+
Parameters
691+
==========
692+
np_time : numpy.datetime64
693+
The numpy datetime64 to convert to a Python datetime.
694+
tz : datetime.tzinfo
695+
The timezone that the numpy datetime is in.
696+
Defaults to UTC, as xarray will convert all time variables to UTC when
697+
opening files.
698+
699+
Returns
700+
=======
701+
datetime.datetime
702+
A timezone aware Python datetime.datetime instance.
685703
"""
686-
return datetime.datetime.fromtimestamp(np_time.item() / 10**9)
704+
epoc = numpy.datetime64('1970-01-01')
705+
timestamp = (np_time - epoc).astype('timedelta64[ns]')
706+
return datetime.datetime.fromtimestamp(timestamp.astype(float) / 1e9, tz=tz)
687707

688708

689709
class RequiresExtraException(Exception):

0 commit comments

Comments
 (0)