Skip to content

Commit

Permalink
Merge pull request #713 from DHI/cleaner_type_hints
Browse files Browse the repository at this point in the history
Use built-in list,tuple,set,dict for type hints
  • Loading branch information
ecomodeller committed Sep 5, 2024
2 parents b8d9cd2 + 5c055e9 commit d22929a
Show file tree
Hide file tree
Showing 26 changed files with 231 additions and 241 deletions.
8 changes: 4 additions & 4 deletions mikeio/_interpolation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations
from typing import Tuple, TYPE_CHECKING, overload
from typing import TYPE_CHECKING, overload
import numpy as np

if TYPE_CHECKING:
Expand Down Expand Up @@ -49,7 +49,7 @@ def interp2d(
data: np.ndarray | DataArray,
elem_ids: np.ndarray,
weights: np.ndarray | None = None,
shape: Tuple[int, ...] | None = None,
shape: tuple[int, ...] | None = None,
) -> np.ndarray: ...


Expand All @@ -58,15 +58,15 @@ def interp2d(
data: Dataset,
elem_ids: np.ndarray,
weights: np.ndarray | None = None,
shape: Tuple[int, ...] | None = None,
shape: tuple[int, ...] | None = None,
) -> Dataset: ...


def interp2d(
data: Dataset | DataArray | np.ndarray,
elem_ids: np.ndarray,
weights: np.ndarray | None = None,
shape: Tuple[int, ...] | None = None,
shape: tuple[int, ...] | None = None,
) -> Dataset | np.ndarray:
"""interp spatially in data (2d only)
Expand Down
4 changes: 2 additions & 2 deletions mikeio/_spectral.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations
from collections.abc import Sequence
from typing import Literal, Tuple
from typing import Literal
import numpy as np
from matplotlib.axes import Axes
from matplotlib.projections.polar import PolarAxes
Expand All @@ -20,7 +20,7 @@ def plot_2dspectrum(
rmin: float | None = None,
rmax: float | None = None,
levels: int | Sequence[float] | None = None,
figsize: Tuple[float, float] = (7, 7),
figsize: tuple[float, float] = (7, 7),
add_colorbar: bool = True,
) -> Axes:
"""
Expand Down
5 changes: 2 additions & 3 deletions mikeio/_time.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations
from datetime import datetime
from dataclasses import dataclass
from typing import List
from collections.abc import Iterable

import pandas as pd
Expand All @@ -18,7 +17,7 @@ def isel(
x: (
int | Iterable[int] | str | datetime | pd.DatetimeIndex | slice | None
) = None,
) -> List[int]:
) -> list[int]:
"""Select time steps from a pandas DatetimeIndex
Parameters
Expand All @@ -28,7 +27,7 @@ def isel(
Returns
-------
List[int]
list[int]
List of indices in the range (0, len(index)
Examples
--------
Expand Down
4 changes: 2 additions & 2 deletions mikeio/_track.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations
from pathlib import Path
from collections.abc import Sequence
from typing import Any, Callable, Tuple
from typing import Any, Callable

import numpy as np
import pandas as pd
Expand All @@ -26,7 +26,7 @@ def _extract_track(
n_elements: int,
method: str,
dtype: Any, # TODO DTypeLike?
data_read_func: Callable[[int, int], Tuple[np.ndarray, float]],
data_read_func: Callable[[int, int], tuple[np.ndarray, float]],
) -> Dataset:
if not isinstance(geometry, GeometryFM2D):
raise NotImplementedError("Only implemented for 2d flexible mesh geometries")
Expand Down
72 changes: 36 additions & 36 deletions mikeio/dataset/_data_plot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations
from typing import Any, Tuple, TYPE_CHECKING
from typing import Any, TYPE_CHECKING

from matplotlib.figure import Figure
import numpy as np
Expand All @@ -23,7 +23,7 @@ def __init__(self, da: "DataArray") -> None:
def __call__(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
**kwargs: Any,
) -> Axes:
"""Plot DataArray according to geometry
Expand Down Expand Up @@ -57,16 +57,16 @@ def __call__(

@staticmethod
def _get_ax(
ax: Axes | None = None, figsize: Tuple[float, float] | None = None
ax: Axes | None = None, figsize: tuple[float, float] | None = None
) -> Axes:
if ax is None:
_, ax = plt.subplots(figsize=figsize)
return ax

@staticmethod
def _get_fig_ax(
ax: Axes | None = None, figsize: Tuple[float, float] | None = None
) -> Tuple[Figure, Axes]:
ax: Axes | None = None, figsize: tuple[float, float] | None = None
) -> tuple[Figure, Axes]:
if ax is None:
fig, ax = plt.subplots(figsize=figsize)
else:
Expand All @@ -76,7 +76,7 @@ def _get_fig_ax(
def hist(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
title: str | None = None,
**kwargs: Any,
) -> Axes:
Expand Down Expand Up @@ -119,7 +119,7 @@ def _hist(self, ax: Axes, **kwargs: Any) -> Any:
def line(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
**kwargs: Any,
) -> Axes:
"""Plot data as lines (timeseries if time is present)"""
Expand Down Expand Up @@ -176,7 +176,7 @@ class _DataArrayPlotterGrid1D(_DataArrayPlotter):
def __call__(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
**kwargs: Any,
) -> Axes:
_, ax = self._get_fig_ax(ax, figsize)
Expand All @@ -188,7 +188,7 @@ def __call__(
def line(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
**kwargs: Any,
) -> Axes:
"""Plot as spatial lines"""
Expand All @@ -198,7 +198,7 @@ def line(
def timeseries(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
**kwargs: Any,
) -> Axes:
"""Plot as timeseries"""
Expand All @@ -210,7 +210,7 @@ def timeseries(
def imshow(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
**kwargs: Any,
) -> Axes:
"""Plot as 2d"""
Expand All @@ -226,7 +226,7 @@ def imshow(
def pcolormesh(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
title: str | None = None,
**kwargs: Any,
) -> Axes:
Expand Down Expand Up @@ -279,15 +279,15 @@ class _DataArrayPlotterGrid2D(_DataArrayPlotter):
def __call__(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
**kwargs: Any,
) -> Axes:
return self.pcolormesh(ax, figsize, **kwargs)

def contour(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
title: str | None = None,
**kwargs: Any,
) -> Axes:
Expand Down Expand Up @@ -316,7 +316,7 @@ def contour(
def contourf(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
title: str | None = None,
label: str | None = None,
**kwargs: Any,
Expand Down Expand Up @@ -347,7 +347,7 @@ def contourf(
def pcolormesh(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
title: str | None = None,
label: str | None = None,
**kwargs: Any,
Expand Down Expand Up @@ -375,12 +375,12 @@ def pcolormesh(
ax.set_title(title)
return ax

def _get_x_y(self) -> Tuple[np.ndarray, np.ndarray]:
def _get_x_y(self) -> tuple[np.ndarray, np.ndarray]:
x = self.da.geometry.x
y = self.da.geometry.y
return x, y

def _get_xn_yn(self) -> Tuple[np.ndarray, np.ndarray]:
def _get_xn_yn(self) -> tuple[np.ndarray, np.ndarray]:
xn = self.da.geometry._centers_to_nodes(self.da.geometry.x)
yn = self.da.geometry._centers_to_nodes(self.da.geometry.y)
return xn, yn
Expand Down Expand Up @@ -427,7 +427,7 @@ class _DataArrayPlotterFM(_DataArrayPlotter):
def __call__(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
**kwargs: Any,
) -> Axes:
"""Plot data as coloured patches"""
Expand All @@ -437,7 +437,7 @@ def __call__(
def patch(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
**kwargs: Any,
) -> Axes:
"""Plot data as coloured patches
Expand All @@ -456,7 +456,7 @@ def patch(
def contour(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
**kwargs: Any,
) -> Axes:
"""Plot data as contour lines
Expand All @@ -475,7 +475,7 @@ def contour(
def contourf(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
**kwargs: Any,
) -> Axes:
"""Plot data as filled contours
Expand All @@ -494,7 +494,7 @@ def contourf(
def mesh(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
**kwargs: Any,
) -> Axes:
"""Plot mesh only
Expand All @@ -511,7 +511,7 @@ def mesh(
def outline(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
**kwargs: Any,
) -> Axes:
"""Plot domain outline (using the boundary_polylines property)
Expand Down Expand Up @@ -582,7 +582,7 @@ class _DataArrayPlotterFMVerticalColumn(_DataArrayPlotter):
def __call__(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
**kwargs: Any,
) -> Axes:
ax = self._get_ax(ax, figsize)
Expand All @@ -591,7 +591,7 @@ def __call__(
def line(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
extrapolate: bool = True,
**kwargs: Any,
) -> Axes:
Expand Down Expand Up @@ -636,7 +636,7 @@ def _line(
def pcolormesh(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
title: str | None = None,
**kwargs: Any,
) -> Axes:
Expand Down Expand Up @@ -679,7 +679,7 @@ class _DataArrayPlotterFMVerticalProfile(_DataArrayPlotter):
def __call__(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
**kwargs: Any,
) -> Axes:
ax = self._get_ax(ax, figsize)
Expand Down Expand Up @@ -712,7 +712,7 @@ class _DataArrayPlotterPointSpectrum(_DataArrayPlotter):
def __call__(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
**kwargs: Any,
) -> Axes:
# ax = self._get_ax(ax, figsize)
Expand Down Expand Up @@ -740,7 +740,7 @@ def contourf(self, **kwargs: Any) -> Axes:
def _plot_freqspectrum(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
**kwargs: Any,
) -> Axes:
ax = self._plot_1dspectrum(self.da.frequencies, ax, figsize, **kwargs) # type: ignore
Expand All @@ -751,7 +751,7 @@ def _plot_freqspectrum(
def _plot_dirspectrum(
self,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
**kwargs: Any,
) -> Axes:
ax = self._plot_1dspectrum(self.da.directions, ax, figsize, **kwargs) # type: ignore
Expand All @@ -764,7 +764,7 @@ def _plot_1dspectrum(
self,
x_values: np.ndarray,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
**kwargs: Any,
) -> Axes:
ax = self._get_ax(ax, figsize)
Expand Down Expand Up @@ -832,7 +832,7 @@ def __init__(self, ds: Dataset) -> None:
self.ds = ds

def __call__(
self, figsize: Tuple[float, float] | None = None, **kwargs: Any
self, figsize: tuple[float, float] | None = None, **kwargs: Any
) -> Axes:
"""Plot multiple DataArrays as time series (only possible dfs0-type data)"""
if self.ds.dims == ("time",):
Expand All @@ -845,8 +845,8 @@ def __call__(

@staticmethod
def _get_fig_ax(
ax: Axes | None = None, figsize: Tuple[float, float] | None = None
) -> Tuple[Figure, Axes]:
ax: Axes | None = None, figsize: tuple[float, float] | None = None
) -> tuple[Figure, Axes]:
import matplotlib.pyplot as plt

if ax is None:
Expand All @@ -860,7 +860,7 @@ def scatter(
x: str | int,
y: str | int,
ax: Axes | None = None,
figsize: Tuple[float, float] | None = None,
figsize: tuple[float, float] | None = None,
**kwargs: Any,
) -> Axes:
"""Plot data from two DataArrays against each other in a scatter plot
Expand Down
Loading

0 comments on commit d22929a

Please sign in to comment.