Skip to content

Commit

Permalink
Add asserts to satisfy mypy
Browse files Browse the repository at this point in the history
Node and Edge tables are not allowed missing, though we say they are in TableModel from which they inherit.

(cherry picked from commit a263cee)
  • Loading branch information
visr committed Feb 2, 2024
1 parent 9b6dbbe commit 8d7af62
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/ribasim/ribasim/geometry/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ class Edge(SpatialTableModel[EdgeSchema]):
"""

def get_where_edge_type(self, edge_type: str) -> NDArray[np.bool_]:
assert self.df is not None
return (self.df.edge_type == edge_type).to_numpy()

def plot(self, **kwargs) -> Axes:
ax = kwargs.get("ax", None)
color_flow = kwargs.get("color_flow", None)
color_control = kwargs.get("color_control", None)
assert self.df is not None

if ax is None:
_, ax = plt.subplots()
Expand Down
5 changes: 5 additions & 0 deletions python/ribasim/ribasim/geometry/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def geometry_from_connectivity(
edge_geometry : np.ndarray
Array of shapely LineStrings.
"""
assert self.df is not None
geometry = self.df["geometry"]
from_points = shapely.get_coordinates(geometry.loc[from_id])
to_points = shapely.get_coordinates(geometry.loc[to_id])
Expand Down Expand Up @@ -109,6 +110,7 @@ def connectivity_from_geometry(
from_node_id : np.ndarray of int
to_node_id : np.ndarray of int
"""
assert self.df is not None
node_index = self.df.index
node_xy = shapely.get_coordinates(self.df.geometry.values)
edge_xy = shapely.get_coordinates(lines)
Expand Down Expand Up @@ -143,6 +145,7 @@ def plot_allocation_networks(self, ax=None, zorder=None) -> Any:

contains_main_network = False
contains_subnetworks = False
assert self.df is not None

for allocation_subnetwork_id, df_subnetwork in self.df.groupby(
"allocation_network_id"
Expand Down Expand Up @@ -223,6 +226,7 @@ def plot(self, ax=None, zorder=None) -> Any:
"User": "g",
"": "k",
}
assert self.df is not None

for nodetype, df in self.df.groupby("type"):
assert isinstance(nodetype, str)
Expand All @@ -237,6 +241,7 @@ def plot(self, ax=None, zorder=None) -> Any:
label=nodetype,
)

assert self.df is not None
geometry = self.df["geometry"]
for text, xy in zip(self.df.index, np.column_stack((geometry.x, geometry.y))):
ax.annotate(text=text, xy=xy, xytext=(2.0, 2.0), textcoords="offset points")
Expand Down
2 changes: 2 additions & 0 deletions python/ribasim/ribasim/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ def print_discrete_control_record(self, path: FilePath) -> None:
node_attrs, node_instances = zip(*self.nodes().items())
node_clss = [node_cls.get_input_type() for node_cls in node_instances]
truth_dict = {"T": ">", "F": "<"}
assert self.network.node.df is not None
assert self.network.edge.df is not None

Check warning on line 472 in python/ribasim/ribasim/model.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/model.py#L471-L472

Added lines #L471 - L472 were not covered by tests

if self.discrete_control.condition.df is None:
raise ValueError("This model has no control input.")
Expand Down

0 comments on commit 8d7af62

Please sign in to comment.