yt-project / unyt

Handle, manipulate, and convert data with units in Python
https://unyt.readthedocs.io
BSD 3-Clause "New" or "Revised" License
364 stars 49 forks source link

BUG: (NEP 18) spurious errors when mixing array-like with unyt_array with non-default registry #462

Closed neutrinoceros closed 1 year ago

neutrinoceros commented 1 year ago

Description

array functions (NEP 18) are supposed to treat pure numpy arrays as dimensionless, but the way this treatment is implemented in unyt 3.0.0 isn't robust enough and it chokes when passed Python lists, or unyt_arrays that use a registry other than the default one.

What I Did

The following 3 examples should be produce similar results and certainly not raise errors

import numpy as np
from unyt.unit_object import Unit
from unyt.unit_registry import UnitRegistry

# this is ok
np.concatenate([np.array([1]), [2]*Unit()])

# but this isn't
np.concatenate([[1], [2]*Unit()])

# and neither is this
np.concatenate([np.array([1]), [2]*Unit(registry=UnitRegistry())])
neutrinoceros commented 1 year ago

There are really two bugs in here, and I'll try to address them separately