Skip to content

BUG: standard dtypes are not singletons #6295

Closed

Description

@perimosocordiae

I encountered the following while working on scipy#5213:

In [1]: np.int64 is np.typeDict['longlong']
Out[1]: False

In [2]: set((np.int64, np.typeDict['longlong']))
Out[2]: {numpy.int64, numpy.int64}

In [3]: np.__version__
Out[3]: '1.11.0.dev0+60df9b1'

This has implications for things like using dtypes as keys in a dictionary, which is fairly common in the scipy unit tests.

Activity

  1. argriffing commented on Sep 9, 2015

    @argriffing
    Contributor

    Is this a duplicate of #5801?

    >>> np.int64, np.longlong
    (<type 'numpy.int64'>, <type 'numpy.int64'>)
    >>> np.dtype(np.int64).num, np.dtype(np.longlong).num
    (7, 9)
  2. njsmith commented on Sep 9, 2015

    @njsmith
    Member

    To be clear, dtypes aren't intended to be singletons, and in general this
    is fine for dictionaries and sets -- those require objects to be ==, not
    'is'.
    .
    The weird behavior here is a more specific bug related to int64 and
    longlong specifically, as @argriffing notes...
    On Sep 9, 2015 1:42 PM, "argriffing" [email protected] wrote:

    Is this a duplicate of #5801 #5801?

    np.int64, np.longlong
    (<type 'numpy.int64'>, <type 'numpy.int64'>)>>> np.dtype(np.int64).num, np.dtype(np.longlong).num
    (7, 9)

    —
    Reply to this email directly or view it on GitHub
    #6295 (comment).

  3. argriffing commented on Sep 9, 2015

    @argriffing
    Contributor

    Just for reference, here's a longer comparison. I don't really understand how this works, and I'm not sure which parts of this (if any) are considered buggy. Also, I don't understand exactly how it is related to the issue I linked above. According to @jaimefrio in that thread, the issue seems to be that PyLong objects are always converted to NPY_LONGLONG, even when it is the same size as NPY_LONG but I'm not sure if this is the same issue causing confusion here, or even if that issue is the one that @njsmith had in mind when referring to a more specific bug related to int64 and longlong specifically.

    >>> np.int64
    <type 'numpy.int64'>
    >>> np.longlong
    <type 'numpy.int64'>
    >>>
    >>> np.dtype(np.int64).num
    7
    >>> np.dtype(np.longlong).num
    9
    >>>
    >>> np.int64 is np.longlong
    False
    >>> np.int64 == np.longlong
    False
    >>>
    >>> np.dtype(np.int64) is np.dtype(np.longlong)
    False
    >>> np.dtype(np.int64) == np.dtype(np.longlong)
    True
    >>>
    >>> np.int64 == np.dtype(np.longlong)
    True
    >>> np.longlong == np.dtype(np.int64)                                           
    True
  4. jorenham commented on Jul 22, 2026

    @jorenham
    Member

    Your conclusion about int64 and longlong (which are scalar types, not dtypes, btw) not being singletons is indeed not right.

    As said before, what you're seeing here is that np.int64 and np.longlong are two different scalar types that have the same __repr__:

    In [1]: np.int64 is np.longlong
    Out[1]: False
    
    In [2]: type(np.int64()) is type(np.longlong())
    Out[2]: False
    
    In [3]: np.int64(), np.longlong()
    Out[3]: (np.int64(0), np.int64(0))
    

    It's pretty confusing IMO, but doesn't really matter much in practice.

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

Metadata

Metadata

Assignees

No one assigned

    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