Skip to content

MNT: replace removed NumPy aliases (np.float/int/object/complex, np.infty) for NumPy >=1.24/2.0 compatibility#92

Open
Andrew Bae (werdnabae) wants to merge 1 commit into
NSLS2:mainfrom
werdnabae:mnt/numpy2-compat-aliases
Open

MNT: replace removed NumPy aliases (np.float/int/object/complex, np.infty) for NumPy >=1.24/2.0 compatibility#92
Andrew Bae (werdnabae) wants to merge 1 commit into
NSLS2:mainfrom
werdnabae:mnt/numpy2-compat-aliases

Conversation

@werdnabae

Copy link
Copy Markdown

Summary

Replace NumPy aliases that have been removed from modern NumPy and now raise AttributeError at runtime:

  • np.float, np.int, np.object, np.complex — removed in NumPy 1.24 (Dec 2022)
  • np.infty — removed in NumPy 2.0

requirements.txt lists numpy unpinned, so installs pull a current NumPy (≥1.24, typically 2.x). With that NumPy, any code path hitting one of these names crashes, e.g.:

AttributeError: module 'numpy' has no attribute 'object'.
`np.object` was a deprecated alias for the builtin `object`. ...

This affects a lot of the package — np.object alone appears ~54 times (XSVS/speckle probability arrays, correlation code, etc.).

What changed

Each removed name is replaced with the exact equivalent recommended by the NumPy 1.20/2.0 migration guides, so the changes are behavior-preserving:

Removed Replacement Note
np.float float was a pure alias for the Python builtin
np.int int "
np.object object "
np.complex complex "
np.infty np.inf identical value

106 substitutions across 23 files.

How it was done (and what was not touched)

  • Applied with a tokenize-based pass, so only real code tokens are rewritten — occurrences inside comments and string literals are left unchanged (e.g. the # removed depreciated np.float note and commented-out code are preserved).
  • np.in1d was intentionally left alone. It is also deprecated, but np.isin is not a drop-in rename (it preserves the input shape rather than flattening), so it deserves a separate, case-by-case PR.
  • Typed names like np.int_, np.float64, np.object_, np.intp are untouched (still valid).

Verification

  • All 23 modified files compile (python -m py_compile).
  • Diff is a clean 1:1 line balance (95 insertions / 95 deletions); no structural changes.

Continues the NumPy cleanup from #81 and #90.

NumPy 1.24 removed the deprecated builtin-type aliases (np.float, np.int,
np.object, np.complex) and NumPy 2.0 removed np.infty. Since requirements.txt
installs an unpinned (current) NumPy, these names raise AttributeError at
runtime, breaking large parts of the package.

Replace them with the exact equivalents the NumPy migration guide recommends:
np.float/np.int/np.object/np.complex -> the Python builtins they aliased, and
np.infty -> np.inf. These substitutions are behavior-preserving.

Done with a tokenize-based pass so only real code is touched; occurrences in
comments and string literals are left unchanged. np.in1d is intentionally NOT
changed here because np.isin differs in output shape and needs case-by-case
review. Continues the cleanup from NSLS2#81 and NSLS2#90.

106 substitutions across 23 files. All modified files compile.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant