Skip to content

Commit e86964d

Browse files
Copilotjpfeuffer
andcommitted
Fix type hint syntax - use ndim=N not numpy.ndim[N]
- Corrected docstring type hints to use proper ndim=1/ndim=2 syntax - Generated code now has valid type annotations - All tests pass (10 passed, 1 skipped) Co-authored-by: jpfeuffer <8102638+jpfeuffer@users.noreply.github.com>
1 parent d1e3b72 commit e86964d

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

autowrap/ConversionProvider.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,14 +2076,14 @@ def matching_python_type_full(self, cpp_type: CppType) -> str:
20762076
(tt,) = cpp_type.template_args
20772077

20782078
if self._is_nested_vector(cpp_type):
2079-
# For 2D arrays, use NDArray type hint
2079+
# For 2D arrays, use proper NDArray type hint syntax
20802080
(inner_tt,) = tt.template_args
20812081
dtype = self._get_numpy_dtype(inner_tt)
2082-
return f"numpy.ndarray[numpy.{dtype}_t, numpy.ndim[2]]"
2082+
return f"numpy.ndarray[numpy.{dtype}_t, ndim=2]"
20832083
else:
2084-
# For 1D arrays, use NDArray type hint
2084+
# For 1D arrays, use proper NDArray type hint syntax
20852085
dtype = self._get_numpy_dtype(tt)
2086-
return f"numpy.ndarray[numpy.{dtype}_t, numpy.ndim[1]]"
2086+
return f"numpy.ndarray[numpy.{dtype}_t, ndim=1]"
20872087

20882088
def type_check_expression(self, cpp_type: CppType, argument_var: str) -> str:
20892089
"""Check if argument is a numpy array (strict - no lists)."""

tests/test_files/numpy_vector/numpy_vector_wrapper.pyi

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,49 @@ class NumpyVectorTest:
1717
"""
1818
...
1919

20-
def getConstRefVector(self) -> numpy.ndarray[numpy.float64_t, numpy.ndim[1]]:
20+
def getConstRefVector(self) -> numpy.ndarray[numpy.float64_t, ndim=1]:
2121
"""
2222
Cython signature: const libcpp_vector_as_np[double] & getConstRefVector()
2323
"""
2424
...
2525

26-
def getMutableRefVector(self) -> numpy.ndarray[numpy.float64_t, numpy.ndim[1]]:
26+
def getMutableRefVector(self) -> numpy.ndarray[numpy.float64_t, ndim=1]:
2727
"""
2828
Cython signature: libcpp_vector_as_np[double] & getMutableRefVector()
2929
"""
3030
...
3131

32-
def getValueVector(self, size: int ) -> numpy.ndarray[numpy.float64_t, numpy.ndim[1]]:
32+
def getValueVector(self, size: int ) -> numpy.ndarray[numpy.float64_t, ndim=1]:
3333
"""
3434
Cython signature: libcpp_vector_as_np[double] getValueVector(size_t size)
3535
"""
3636
...
3737

38-
def sumVector(self, data: numpy.ndarray[numpy.float64_t, numpy.ndim[1]] ) -> float:
38+
def sumVector(self, data: numpy.ndarray[numpy.float64_t, ndim=1] ) -> float:
3939
"""
4040
Cython signature: double sumVector(libcpp_vector_as_np[double] data)
4141
"""
4242
...
4343

44-
def sumIntVector(self, data: numpy.ndarray[numpy.int32_t, numpy.ndim[1]] ) -> int:
44+
def sumIntVector(self, data: numpy.ndarray[numpy.int32_t, ndim=1] ) -> int:
4545
"""
4646
Cython signature: int sumIntVector(libcpp_vector_as_np[int] data)
4747
"""
4848
...
4949

50-
def createFloatVector(self, size: int ) -> numpy.ndarray[numpy.float32_t, numpy.ndim[1]]:
50+
def createFloatVector(self, size: int ) -> numpy.ndarray[numpy.float32_t, ndim=1]:
5151
"""
5252
Cython signature: libcpp_vector_as_np[float] createFloatVector(size_t size)
5353
"""
5454
...
5555

56-
def create2DVector(self, rows: int , cols: int ) -> numpy.ndarray[numpy.float64_t, numpy.ndim[2]]:
56+
def create2DVector(self, rows: int , cols: int ) -> numpy.ndarray[numpy.float64_t, ndim=2]:
5757
"""
5858
Cython signature: libcpp_vector_as_np[libcpp_vector_as_np[double]] create2DVector(size_t rows, size_t cols)
5959
"""
6060
...
6161

62-
def sum2DVector(self, data: numpy.ndarray[numpy.float64_t, numpy.ndim[2]] ) -> float:
62+
def sum2DVector(self, data: numpy.ndarray[numpy.float64_t, ndim=2] ) -> float:
6363
"""
6464
Cython signature: double sum2DVector(libcpp_vector_as_np[libcpp_vector_as_np[double]] data)
6565
"""

tests/test_files/numpy_vector/numpy_vector_wrapper.pyx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ cdef class NumpyVectorTest:
5353

5454
def getConstRefVector(self):
5555
"""
56-
getConstRefVector(self) -> numpy.ndarray[numpy.float64_t, numpy.ndim[1]]
56+
getConstRefVector(self) -> numpy.ndarray[numpy.float64_t, ndim=1]
5757
"""
5858
_r = self.inst.get().getConstRefVector()
5959
# Convert C++ vector to numpy array COPY (Python owns data)
@@ -65,7 +65,7 @@ cdef class NumpyVectorTest:
6565

6666
def getMutableRefVector(self):
6767
"""
68-
getMutableRefVector(self) -> numpy.ndarray[numpy.float64_t, numpy.ndim[1]]
68+
getMutableRefVector(self) -> numpy.ndarray[numpy.float64_t, ndim=1]
6969
"""
7070
_r = self.inst.get().getMutableRefVector()
7171
# Convert C++ vector to numpy array COPY (Python owns data)
@@ -77,7 +77,7 @@ cdef class NumpyVectorTest:
7777

7878
def getValueVector(self, size ):
7979
"""
80-
getValueVector(self, size: int ) -> numpy.ndarray[numpy.float64_t, numpy.ndim[1]]
80+
getValueVector(self, size: int ) -> numpy.ndarray[numpy.float64_t, ndim=1]
8181
"""
8282
assert isinstance(size, int) and size >= 0, 'arg size wrong type'
8383

@@ -91,7 +91,7 @@ cdef class NumpyVectorTest:
9191

9292
def sumVector(self, numpy.ndarray[numpy.float64_t, ndim=1] data ):
9393
"""
94-
sumVector(self, data: numpy.ndarray[numpy.float64_t, numpy.ndim[1]] ) -> float
94+
sumVector(self, data: numpy.ndarray[numpy.float64_t, ndim=1] ) -> float
9595
"""
9696
assert isinstance(data, numpy.ndarray), 'arg data wrong type'
9797
# Convert 1D numpy array to C++ vector (fast memcpy)
@@ -107,7 +107,7 @@ cdef class NumpyVectorTest:
107107

108108
def sumIntVector(self, numpy.ndarray[numpy.int32_t, ndim=1] data ):
109109
"""
110-
sumIntVector(self, data: numpy.ndarray[numpy.int32_t, numpy.ndim[1]] ) -> int
110+
sumIntVector(self, data: numpy.ndarray[numpy.int32_t, ndim=1] ) -> int
111111
"""
112112
assert isinstance(data, numpy.ndarray), 'arg data wrong type'
113113
# Convert 1D numpy array to C++ vector (fast memcpy)
@@ -123,7 +123,7 @@ cdef class NumpyVectorTest:
123123

124124
def createFloatVector(self, size ):
125125
"""
126-
createFloatVector(self, size: int ) -> numpy.ndarray[numpy.float32_t, numpy.ndim[1]]
126+
createFloatVector(self, size: int ) -> numpy.ndarray[numpy.float32_t, ndim=1]
127127
"""
128128
assert isinstance(size, int) and size >= 0, 'arg size wrong type'
129129

@@ -137,7 +137,7 @@ cdef class NumpyVectorTest:
137137

138138
def create2DVector(self, rows , cols ):
139139
"""
140-
create2DVector(self, rows: int , cols: int ) -> numpy.ndarray[numpy.float64_t, numpy.ndim[2]]
140+
create2DVector(self, rows: int , cols: int ) -> numpy.ndarray[numpy.float64_t, ndim=2]
141141
"""
142142
assert isinstance(rows, int) and rows >= 0, 'arg rows wrong type'
143143
assert isinstance(cols, int) and cols >= 0, 'arg cols wrong type'
@@ -158,7 +158,7 @@ cdef class NumpyVectorTest:
158158

159159
def sum2DVector(self, numpy.ndarray[numpy.float64_t, ndim=2] data ):
160160
"""
161-
sum2DVector(self, data: numpy.ndarray[numpy.float64_t, numpy.ndim[2]] ) -> float
161+
sum2DVector(self, data: numpy.ndarray[numpy.float64_t, ndim=2] ) -> float
162162
"""
163163
assert isinstance(data, numpy.ndarray), 'arg data wrong type'
164164
# Convert 2D numpy array to nested C++ vector

0 commit comments

Comments
 (0)