Skip to content

Commit add4bcc

Browse files
committed
Suppress NumPy2 warning from casting 1-element array to scalar
1 parent 78deb3d commit add4bcc

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

pyomo/repn/ampl.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,16 @@ def check_constant(self, ans, obj):
11371137
# attempt to convert the value to a float before
11381138
# proceeding.
11391139
#
1140+
# Note that as of NumPy 1.25, blindly casting a
1141+
# 1-element ndarray to a float will generate a
1142+
# deprecation warning. We will explicitly test for
1143+
# that, but want to do the test without triggering the
1144+
# numpy import
1145+
for cls in ans.__class__.__mro__:
1146+
if cls.__name__ == 'ndarray' and cls.__module__ == 'numpy':
1147+
if len(ans) == 1:
1148+
ans = ans[0]
1149+
break
11401150
# TODO: we should check bool and warn/error (while bool is
11411151
# convertible to float in Python, they have very
11421152
# different semantic meanings in Pyomo).

0 commit comments

Comments
 (0)