Skip to content

TypeError related to PEP 695 with NumPy 2.4 #1403

@mattwthompson

Description

@mattwthompson

Description

Basic usage has broken because of NumPy 2.4, released Dec 20 2025.

Reproduction

$ micromamba create --name numpy-2.2 "openff-interchange" "python=3.13" "numpy=2.2" -yq
$ micromamba create --name numpy-2.4 "openff-interchange" "python=3.13" "numpy=2.4" -yq
$ micromamba run -n numpy-2.2 python -c "from openff.interchange import *"
$ micromamba run -n numpy-2.4 python -c "from openff.interchange import *"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    from openff.interchange import *
  File "<frozen importlib._bootstrap>", line 1410, in _handle_fromlist
  File "<frozen importlib._bootstrap>", line 1412, in _handle_fromlist
  File "/Users/mattthompson/mamba/envs/numpy-2.4/lib/python3.13/site-packages/openff/interchange/__init__.py", line 33, in __getattr__
    return importlib.import_module(module).__dict__[name]
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
  File "/Users/mattthompson/mamba/envs/numpy-2.4/lib/python3.13/importlib/__init__.py", line 88, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mattthompson/mamba/envs/numpy-2.4/lib/python3.13/site-packages/openff/interchange/components/interchange.py", line 21, in <module>
    from openff.interchange.common._nonbonded import ElectrostaticsCollection, vdWCollection
  File "/Users/mattthompson/mamba/envs/numpy-2.4/lib/python3.13/site-packages/openff/interchange/common/_nonbonded.py", line 9, in <module>
    from openff.interchange.components.potentials import Collection
  File "/Users/mattthompson/mamba/envs/numpy-2.4/lib/python3.13/site-packages/openff/interchange/components/potentials.py", line 226, in <module>
    class Collection(_BaseModel):
    ...<169 lines>...
            return self is other
  File "/Users/mattthompson/mamba/envs/numpy-2.4/lib/python3.13/site-packages/openff/interchange/components/potentials.py", line 349, in Collection
    ) -> ArrayLike | "Array":
         ~~~~~~~~~~^~~~~~~~~
TypeError: unsupported operand type(s) for |: 'typing.TypeAliasType' and 'str'

All that's needed to reproduce it is a switch between 2.2 and 2.4:

$ micromamba run -n numpy-2.4 micromamba list > 2.4.txt
$ micromamba run -n numpy-2.2 micromamba list > 2.2.txt
$ (openff-interchange-dev) [openff-interchange-feedstock] diff 2.2.txt 2.4.txt                                                                                     d03f78f
1c1
< List of packages in environment: "/Users/mattthompson/mamba/envs/numpy-2.2"
---
> List of packages in environment: "/Users/mattthompson/mamba/envs/numpy-2.4"
190c190
<   numpy                          2.2.6         py313h41a2e72_0                    conda-forge
---
>   numpy                          2.4.0         py313h16eae64_0                    conda-forge
213c213
<   parmed                         4.3.0         py313h70a7ac0_2                    conda-forge
---
>   parmed                         4.3.0         py313h70a7ac0_1                    conda-forge

This is not due to ParmEd's lack of NumPy 2.3 support; the error message is different and the code doesn't work on any 2.3+:

$ micromamba run -n numpy-2.2 python -c "import parmed"
$ micromamba run -n numpy-2.4 python -c "import parmed"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    import parmed
  File "/Users/mattthompson/mamba/envs/numpy-2.4/lib/python3.13/site-packages/parmed/__init__.py", line 23, in <module>
    from . import amber, charmm, gromacs, dlpoly, namd, openmm, rosetta, tinker, entos
  File "/Users/mattthompson/mamba/envs/numpy-2.4/lib/python3.13/site-packages/parmed/amber/__init__.py", line 14, in <module>
    from .netcdffiles import NetCDFTraj, NetCDFRestart
  File "/Users/mattthompson/mamba/envs/numpy-2.4/lib/python3.13/site-packages/parmed/amber/netcdffiles.py", line 25, in <module>
    from ..utils.netcdf import netcdf_file as NetCDFFile
  File "/Users/mattthompson/mamba/envs/numpy-2.4/lib/python3.13/site-packages/parmed/utils/netcdf.py", line 46, in <module>
    from numpy.compat import asbytes, asstr
ModuleNotFoundError: No module named 'numpy.compat'

Software versions

  • Which operating system and version are you using? macOS
  • How did you install Interchange? conda
  • What is the output of running conda list?

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingdependenciesPull requests that update a dependency fileupstreamBlocked by upstream changes

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions