Skip to content
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
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.8
- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: 3.8
python-version: 3.9

- name: Lint
run: |
Expand Down
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,10 @@ repos:
args:
- --convention=numpy
- --add-select=D417

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.982
hooks:
- id: mypy
additional_dependencies: [types-all]
exclude: conf.py$
1 change: 1 addition & 0 deletions news/66.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added type hints to the entire code base and a *mypy* hook to the *pre-commit* linters to identify potential typing issues.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ force_grid_wrap = 0
combine_as_imports = true
line_length = 88

[tool.mypy]
ignore_missing_imports = true

[tool.towncrier]
directory = "news"
package = "sequence"
Expand Down
9 changes: 5 additions & 4 deletions sequence/_grid.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""Define the grid used for creating *Sequence* models."""
import os
import sys

import numpy as np
from landlab import RasterModelGrid

try:
if sys.version_info >= (3, 11):
import tomllib
except ModuleNotFoundError:
else:
import tomli as tomllib


Expand Down Expand Up @@ -41,7 +42,7 @@ def __init__(self, n_cols: int, spacing: float = 100.0):
self.at_grid["sea_level__elevation"] = 0.0

@classmethod
def from_toml(cls, filepath: os.PathLike[str]):
def from_toml(cls, filepath: os.PathLike[str]) -> "SequenceModelGrid":
"""Load a :class:`~SequenceModelGrid` from a *toml*-formatted file.

Parameters
Expand All @@ -53,7 +54,7 @@ def from_toml(cls, filepath: os.PathLike[str]):
return SequenceModelGrid.from_dict(tomllib.load(fp)["sequence"]["grid"])

@classmethod
def from_dict(cls, params: dict):
def from_dict(cls, params: dict) -> "SequenceModelGrid":
"""Create a :class:`~SequenceModelGrid` from a `dict`.

If possible, this alternate constructor simply passes
Expand Down
37 changes: 24 additions & 13 deletions sequence/bathymetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
This module contains *Landlab* components to read bathymetry into a
`SequenceModelGrid`.
"""
from os import PathLike
from typing import Union

import numpy as np
from landlab import Component
from numpy.typing import NDArray
from scipy import interpolate

from ._grid import SequenceModelGrid


class BathymetryReader(Component):
"""Landlab component that reads bathymetry from a file."""
Expand All @@ -26,7 +32,12 @@ class BathymetryReader(Component):
}
}

def __init__(self, grid, filepath=None, kind="linear", **kwds):
def __init__(
self,
grid: SequenceModelGrid,
filepath: Union[str, PathLike[str]],
kind: str = "linear",
):
"""Generate a bathymetric profile from a file.

Parameters
Expand All @@ -40,7 +51,7 @@ def __init__(self, grid, filepath=None, kind="linear", **kwds):
'nearest', 'zero', 'slinear', 'quadratic', 'cubic').
Default is 'linear'.
"""
super().__init__(grid, **kwds)
super().__init__(grid)

data = np.loadtxt(filepath, delimiter=",", comments="#")
self._bathymetry = interpolate.interp1d(
Expand All @@ -56,18 +67,18 @@ def __init__(self, grid, filepath=None, kind="linear", **kwds):
self.grid.add_zeros("topographic__elevation", at="node")

@property
def x(self):
def x(self) -> NDArray[np.floating]:
"""Return the x-coordinates of the grid."""
return self.grid.x_of_node[self.grid.nodes_at_bottom_edge]

@property
def z(self):
def z(self) -> NDArray[np.floating]:
"""Return the elevations along the grid."""
return self.grid.at_node["topographic__elevation"][
self.grid.nodes_at_bottom_edge
]

def run_one_step(self, dt=None):
def run_one_step(self, dt: float = None) -> None:
"""Update the grid's bathymetry.

Parameters
Expand All @@ -81,14 +92,14 @@ def run_one_step(self, dt=None):


def _create_initial_profile(
x,
sl_plain=0.0008,
init_shore=19750.0,
hgt=15.0,
alpha=1 / 2000.0,
sl_sh=0.001,
wavebase=60.0,
):
x: NDArray[np.floating],
sl_plain: float = 0.0008,
init_shore: float = 19750.0,
hgt: float = 15.0,
alpha: float = 1 / 2000.0,
sl_sh: float = 0.001,
wavebase: float = 60.0,
) -> NDArray[np.floating]:

# check shoreline is in array, else put in center of array
if x[-1] < init_shore:
Expand Down
Loading