Skip to content

Commit 59e0eb1

Browse files
authored
Handle boolean numpy arrays (#260)
Numpy 2.x has removed `numpy.bool`. One should instead use `bool` or `np.bool_`. https://numpy.org/devdocs/release/1.20.0-notes.html#using-the-aliases-of-builtin-types-like-np-int-is-deprecated We've been using this fix at https://github.com/colmap/colmap for 1 year without issue.
1 parent 44a32c0 commit 59e0eb1

File tree

1 file changed

+28
-16
lines changed
  • pybind11_stubgen/parser/mixins

1 file changed

+28
-16
lines changed

pybind11_stubgen/parser/mixins/fix.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -512,22 +512,28 @@ class FixNumpyArrayDimAnnotation(IParser):
512512
__annotated_name = QualifiedName.from_str("Annotated")
513513
numpy_primitive_types: set[QualifiedName] = set(
514514
map(
515-
lambda name: QualifiedName.from_str(f"numpy.{name}"),
515+
QualifiedName.from_str,
516516
(
517-
"uint8",
518-
"int8",
519-
"uint16",
520-
"int16",
521-
"uint32",
522-
"int32",
523-
"uint64",
524-
"int64",
525-
"float16",
526-
"float32",
527-
"float64",
528-
"complex32",
529-
"complex64",
530-
"longcomplex",
517+
"bool",
518+
*map(
519+
lambda name: f"numpy.{name}",
520+
(
521+
"uint8",
522+
"int8",
523+
"uint16",
524+
"int16",
525+
"uint32",
526+
"int32",
527+
"uint64",
528+
"int64",
529+
"float16",
530+
"float32",
531+
"float64",
532+
"complex32",
533+
"complex64",
534+
"longcomplex",
535+
),
536+
),
531537
),
532538
)
533539
)
@@ -696,9 +702,15 @@ def parse_annotation_str(
696702
):
697703
return result
698704

705+
name = scalar_with_dims.name
706+
# Pybind annotates a bool Python type, which cannot be used with
707+
# numpy.dtype because it does not inherit from numpy.generic.
708+
# Only numpy.bool_ works reliably with both NumPy 1.x and 2.x.
709+
if str(name) == "bool":
710+
name = QualifiedName.from_str("numpy.bool_")
699711
dtype = ResolvedType(
700712
name=QualifiedName.from_str("numpy.dtype"),
701-
parameters=[ResolvedType(name=scalar_with_dims.name)],
713+
parameters=[ResolvedType(name=name)],
702714
)
703715

704716
shape = self.parse_annotation_str("Any")

0 commit comments

Comments
 (0)