Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit df1cfde

Browse files
authored
Merge pull request #28755 from QuLogic/tri-typing
2 parents cb9cf3b + fa8b39d commit df1cfde

File tree

3 files changed

+46
-36
lines changed

3 files changed

+46
-36
lines changed

lib/matplotlib/_tri.pyi

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
11
# This is a private module implemented in C++
2-
# As such these type stubs are overly generic, but here to allow these types
3-
# as return types for public methods
4-
from typing import Any, final
2+
from typing import final
3+
4+
import numpy as np
5+
import numpy.typing as npt
56

67
@final
78
class TrapezoidMapTriFinder:
8-
def __init__(self, *args, **kwargs) -> None: ...
9-
def find_many(self, *args, **kwargs) -> Any: ...
10-
def get_tree_stats(self, *args, **kwargs) -> Any: ...
11-
def initialize(self, *args, **kwargs) -> Any: ...
12-
def print_tree(self, *args, **kwargs) -> Any: ...
9+
def __init__(self, triangulation: Triangulation): ...
10+
def find_many(self, x: npt.NDArray[np.float64], y: npt.NDArray[np.float64]) -> npt.NDArray[np.int_]: ...
11+
def get_tree_stats(self) -> list[int | float]: ...
12+
def initialize(self) -> None: ...
13+
def print_tree(self) -> None: ...
1314

1415
@final
1516
class TriContourGenerator:
16-
def __init__(self, *args, **kwargs) -> None: ...
17-
def create_contour(self, *args, **kwargs) -> Any: ...
18-
def create_filled_contour(self, *args, **kwargs) -> Any: ...
17+
def __init__(self, triangulation: Triangulation, z: npt.NDArray[np.float64]): ...
18+
def create_contour(self, level: float) -> tuple[list[float], list[int]]: ...
19+
def create_filled_contour(self, lower_level: float, upper_level: float) -> tuple[list[float], list[int]]: ...
1920

2021
@final
2122
class Triangulation:
22-
def __init__(self, *args, **kwargs) -> None: ...
23-
def calculate_plane_coefficients(self, *args, **kwargs) -> Any: ...
24-
def get_edges(self, *args, **kwargs) -> Any: ...
25-
def get_neighbors(self, *args, **kwargs) -> Any: ...
26-
def set_mask(self, *args, **kwargs) -> Any: ...
23+
def __init__(
24+
self,
25+
x: npt.NDArray[np.float64],
26+
y: npt.NDArray[np.float64],
27+
triangles: npt.NDArray[np.int_],
28+
mask: npt.NDArray[np.bool_] | tuple[()],
29+
edges: npt.NDArray[np.int_] | tuple[()],
30+
neighbors: npt.NDArray[np.int_] | tuple[()],
31+
correct_triangle_orientation: bool,
32+
): ...
33+
def calculate_plane_coefficients(self, z: npt.ArrayLike) -> npt.NDArray[np.float64]: ...
34+
def get_edges(self) -> npt.NDArray[np.int_]: ...
35+
def get_neighbors(self) -> npt.NDArray[np.int_]: ...
36+
def set_mask(self, mask: npt.NDArray[np.bool_] | tuple[()]) -> None: ...

lib/matplotlib/tests/test_triangulation.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,43 +1181,44 @@ def test_tricontourf_decreasing_levels():
11811181
plt.tricontourf(x, y, z, [1.0, 0.0])
11821182

11831183

1184-
def test_internal_cpp_api():
1184+
def test_internal_cpp_api() -> None:
11851185
# Following github issue 8197.
11861186
from matplotlib import _tri # noqa: F401, ensure lazy-loaded module *is* loaded.
11871187

11881188
# C++ Triangulation.
11891189
with pytest.raises(
11901190
TypeError,
11911191
match=r'__init__\(\): incompatible constructor arguments.'):
1192-
mpl._tri.Triangulation()
1192+
mpl._tri.Triangulation() # type: ignore[call-arg]
11931193

11941194
with pytest.raises(
11951195
ValueError, match=r'x and y must be 1D arrays of the same length'):
1196-
mpl._tri.Triangulation([], [1], [[]], (), (), (), False)
1196+
mpl._tri.Triangulation(np.array([]), np.array([1]), np.array([[]]), (), (), (),
1197+
False)
11971198

1198-
x = [0, 1, 1]
1199-
y = [0, 0, 1]
1199+
x = np.array([0, 1, 1], dtype=np.float64)
1200+
y = np.array([0, 0, 1], dtype=np.float64)
12001201
with pytest.raises(
12011202
ValueError,
12021203
match=r'triangles must be a 2D array of shape \(\?,3\)'):
1203-
mpl._tri.Triangulation(x, y, [[0, 1]], (), (), (), False)
1204+
mpl._tri.Triangulation(x, y, np.array([[0, 1]]), (), (), (), False)
12041205

1205-
tris = [[0, 1, 2]]
1206+
tris = np.array([[0, 1, 2]], dtype=np.int_)
12061207
with pytest.raises(
12071208
ValueError,
12081209
match=r'mask must be a 1D array with the same length as the '
12091210
r'triangles array'):
1210-
mpl._tri.Triangulation(x, y, tris, [0, 1], (), (), False)
1211+
mpl._tri.Triangulation(x, y, tris, np.array([0, 1]), (), (), False)
12111212

12121213
with pytest.raises(
12131214
ValueError, match=r'edges must be a 2D array with shape \(\?,2\)'):
1214-
mpl._tri.Triangulation(x, y, tris, (), [[1]], (), False)
1215+
mpl._tri.Triangulation(x, y, tris, (), np.array([[1]]), (), False)
12151216

12161217
with pytest.raises(
12171218
ValueError,
12181219
match=r'neighbors must be a 2D array with the same shape as the '
12191220
r'triangles array'):
1220-
mpl._tri.Triangulation(x, y, tris, (), (), [[-1]], False)
1221+
mpl._tri.Triangulation(x, y, tris, (), (), np.array([[-1]]), False)
12211222

12221223
triang = mpl._tri.Triangulation(x, y, tris, (), (), (), False)
12231224

@@ -1232,9 +1233,9 @@ def test_internal_cpp_api():
12321233
ValueError,
12331234
match=r'mask must be a 1D array with the same length as the '
12341235
r'triangles array'):
1235-
triang.set_mask(mask)
1236+
triang.set_mask(mask) # type: ignore[arg-type]
12361237

1237-
triang.set_mask([True])
1238+
triang.set_mask(np.array([True]))
12381239
assert_array_equal(triang.get_edges(), np.empty((0, 2)))
12391240

12401241
triang.set_mask(()) # Equivalent to Python Triangulation mask=None
@@ -1244,15 +1245,14 @@ def test_internal_cpp_api():
12441245
with pytest.raises(
12451246
TypeError,
12461247
match=r'__init__\(\): incompatible constructor arguments.'):
1247-
mpl._tri.TriContourGenerator()
1248+
mpl._tri.TriContourGenerator() # type: ignore[call-arg]
12481249

12491250
with pytest.raises(
12501251
ValueError,
1251-
match=r'z must be a 1D array with the same length as the x and y '
1252-
r'arrays'):
1253-
mpl._tri.TriContourGenerator(triang, [1])
1252+
match=r'z must be a 1D array with the same length as the x and y arrays'):
1253+
mpl._tri.TriContourGenerator(triang, np.array([1]))
12541254

1255-
z = [0, 1, 2]
1255+
z = np.array([0, 1, 2])
12561256
tcg = mpl._tri.TriContourGenerator(triang, z)
12571257

12581258
with pytest.raises(
@@ -1263,13 +1263,13 @@ def test_internal_cpp_api():
12631263
with pytest.raises(
12641264
TypeError,
12651265
match=r'__init__\(\): incompatible constructor arguments.'):
1266-
mpl._tri.TrapezoidMapTriFinder()
1266+
mpl._tri.TrapezoidMapTriFinder() # type: ignore[call-arg]
12671267

12681268
trifinder = mpl._tri.TrapezoidMapTriFinder(triang)
12691269

12701270
with pytest.raises(
12711271
ValueError, match=r'x and y must be array-like with same shape'):
1272-
trifinder.find_many([0], [0, 1])
1272+
trifinder.find_many(np.array([0]), np.array([0, 1]))
12731273

12741274

12751275
def test_qhull_large_offset():

src/tri/_tri.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,7 @@ TrapezoidMapTriFinder::TriIndexArray
13141314
TrapezoidMapTriFinder::find_many(const CoordinateArray& x,
13151315
const CoordinateArray& y)
13161316
{
1317-
if (x.ndim() != 1 || x.shape(0) != y.shape(0))
1317+
if (x.ndim() != 1 || y.ndim() != 1 || x.shape(0) != y.shape(0))
13181318
throw std::invalid_argument(
13191319
"x and y must be array-like with same shape");
13201320

0 commit comments

Comments
 (0)