Skip to content

BUG: np.int_ and np.intc / np.longlong have the same __name__ but are different #9799

Closed

Description

@drhagen

On windows 7:

>>> np.cint, np.cint is np.int32
(numpy.int32, False)
>>> np.int_, np.int_ is np.int32
(numpy.int32, True)

On 64-bit linux:

>>> np.int_, np.int_ is np.int64
(numpy.int64, True)
>>> np.longlong, np.longlong is np.int64
(numpy.int64, False)

(original title: intc displays wrong class name)

The string representation of numpy.intc is <class 'numpy.int32'>. This would fine if this were true--just a name pointing to the same object. But at least on Windows 7, numpy.intc is not the same object as numpy.int32; it's not even a subclass. This is very confusing for debugging objects of this type, as objects inexplicably fail an isinstance test.

Could one of these two small changes be made:

  • Either make intc just be another name pointing to the object behind int32
  • Or make intc print out <class 'numpy.intc'>

Activity

  1. drhagen commented on Oct 1, 2017

    @drhagen
    ContributorAuthor

    This is related to #6295 and others linked there.

  2. eric-wieser commented on Oct 1, 2017

    @eric-wieser
    Member

    This isn't because the dtypes aren't singletons, as the linked issue describes. The problem is that int32 refers to long, and cint refers to int - but on windows long and int are both 32-bit:

    In [14]: np.dtype(np.intc).char
    Out[14]: 'i'  # int
    
    In [15]: np.dtype(np.int32).char
    Out[15]: 'l'  # long

    Really, the issue is that we have a leaky abstraction, where internally we work in terms of char, short, int, long, long long, but to the user we show int8 int16 int32 int64. On any platform, two of these are going to have the same name.

  3. eric-wieser commented on Oct 1, 2017

    @eric-wieser
    Member

    Either make intc just be another name pointing to the object behind int32

    Because np.intc is defined to mean a C int, this would require np.int32 to refer to int as well. This is fine, but now you break np.int_ (long) in the same way as cint is currently broken, which also prints as np.int32

    Or make intc print out <class 'numpy.intc'>

    This is tricky, because now np.int32 will print out like that too

  4. drhagen commented on Oct 1, 2017

    @drhagen
    ContributorAuthor

    Or make intc print out <class 'numpy.intc'>

    This is tricky, because now np.int32 will print out like that too

    It seems strange that these objects would be so tightly coupled that the __str__ method couldn't be swapped out, but not so tightly coupled that the object itself can't be swapped out. But I don't know anything about the C code, so I'll just accept that there is something difficult going on.

    I did find that numpy.intp is numpy.int64 returns True.

  5. eric-wieser commented on Oct 1, 2017

    @eric-wieser
    Member

    would be so tightly coupled that the str method couldn't be swapped ou

    If all you want to do is change the string, here's your task: write a function to replace the one in the pseudocode below.

    def get_name(c_name):
        return 'int{}'.format(number_of_bits_in(c_name))   # number_of_bits_in is platform dependent
    
    get_name("char")
    get_name("short")
    get_name("int")  # this is intc
    get_name("long")  # this is int_
    get_name("long long")
    

    Can you propose a better way to generate names? One option would be to simply return c_name, but I'd argue that seeing the number of bits in the repr is useful

  6. drhagen commented on Oct 2, 2017

    @drhagen
    ContributorAuthor

    Maybe this happens to late and this is the problem, but somewhere the name it is supposed to ultimately have on the numpy module is assigned (e.g. setattr(numpy, 'intc', intc_object)). It would be nice is the same code that assigned the attribute name also assigned or reassigned the __repr__ string. Something like: setattr(numpy, 'intc', update_repr(intc_object, 'intc'))

  7. changed the title [-]`intc` displays wrong class name[/-] [+]BUG: np.long and np.intc / np.longlong have the same `repr`[/+] on Dec 2, 2017
  8. changed the title [-]BUG: np.long and np.intc / np.longlong have the same `repr`[/-] [+]BUG: np.int_ and np.intc / np.longlong have the same `repr`[/+] on Dec 2, 2017
  9. changed the title [-]BUG: np.int_ and np.intc / np.longlong have the same `repr`[/-] [+]BUG: np.int_ and np.intc / np.longlong have the same __name__ but are different[/+] on Dec 2, 2017
  10. eric-wieser commented on Dec 2, 2017

    @eric-wieser
    Member

    I've updated your description so that we can point to this as the canonical bug report

  11. 2 remaining items

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions