I'm attempting to map numpy dtypes to associated values using a dictionary lookup. I observe the following counterintuitive behavior:
dtype = np.uint16
x = np.array([0, 1, 2], dtype=dtype)
assert x.dtype == dtype
d = {np.uint8: 8, np.uint16: 16, np.uint32: 32, np.float32: 32}
print(dtype in d) # prints True
print(x.dtype in d) # prints False
Using other dtypes produces similar results.
So we have that np.uint16 == x.dtype, but the former is found in the dictionary's keys while the latter is not. Any explanation and/or simple workaround would be appreciated.