Skip to content

Use equality instead of hasing for dtype helpers #111

Description

@honno

@asmeurer noted in #110 (comment) that NumPy proper (i.e. numpy, as opposed to numpy.array_api) wasn't working with the test suite due to the following code. The problem was that the dtype attribute of NumPy-proper arrays were being used in conjuction with namespaced dtypes—these two are different objects, and thus have different hashes so they look like different keys in a dict.

>>> import numpy as np
>>> dtypes_map = {np.int64: "foo"}
>>> dtypes_map[np.asarray(0).dtype]
KeyError

This behaviour is isn't specifically ruled out in the spec, as the spec only says dtypes need equality, which NumPy-proper does conform to.

>>> np.asarray(0).dtype == np.int64
True

So the test suite should phase out assumptions of namespaced dtypes and array dtypes sharing the same hash, instead relying on equality.

Activity

  1. self-assigned this
    on Apr 13, 2022
  2. asmeurer commented on Apr 13, 2022

    @asmeurer
    Member

    Could also be something to fix upstream. Technically a == b should imply hash(a) == hash(b), so NumPy isn't really following Python standard practices here.

  3. asmeurer commented on Apr 13, 2022

    @asmeurer
    Member

    I found numpy/numpy#17864 which seems to be about this. Making the change here is still a good idea because the spec doesn't require hashing to begin with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions