question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: np.str_ and np.bytes_ object have a confusing __repr__ and __str__

See original GitHub issue
>>> s = np.str_("\0\0")
>>> list(s)
['\0', '\0']
>>> len(s)
2
>>> s
''  # wat
>>> b = np.bytes_(2)
>>> list(b)
[0, 0]
>>> len(b)
2
>>> b
b''  # wat

Caused by

https://github.com/numpy/numpy/blob/f15026ce782f3b8c291cde5363b86f4f1bc88ad8/numpy/core/src/multiarray/scalartypes.c.src#L389-L425

It looks to me like we should just remove our overloads of these functions, they don’t make any sense.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
sebergcommented, Feb 1, 2020

True, although that code will probably give the warning (although if you have many of that you would not notice). Its true from a subclassing perspective… It seems we either have to sacrifice consistent subclassing or consistency with arrays? Since this is NumPy scalar, which is almost an array, I think I still lean a bit towards 3. But basically there is no good solution and either 3. or 4. seem better than the current state to me.

1reaction
sebergcommented, Feb 1, 2020

I am for Option 3.

Any option should have the same result as np.array(string, dtype="S")[()] and np.array(str, "S") and np.string_(str) should be as similar as possible (at the moment at least, due to PyArray_Return).

Since it can be a bug for someone who needs the zero bytes, I think a warning makes sense. The only question is whether the warning for some reason is prohibitively noisy. I do not expect that. It could point out that void can sometimes be a way to spell bytes of fixed length.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why do I have to use np.string_? Why can't I ... - Stack Overflow
Simply because np.string is not defined. There's nothing special syntactically about the _, it's just a naming decision that's followed ...
Read more >
NumPy 1.20.0 Release Notes
For a long time, np.int has been an alias of the builtin int . This is repeatedly a cause of confusion for newcomers,...
Read more >
Bug descriptions — spotbugs 4.7.3 documentation
This document lists the standard bug patterns reported by SpotBugs. ... As most comparators have little or no state, making them serializable is...
Read more >
str() vs repr() in Python - GeeksforGeeks
The print statement and str() built-in function uses __str__ to display the string representation of the object while the repr() built-in ...
Read more >
Built-in Functions — Python 3.11.1 documentation
Convert an integer number to a binary string prefixed with “0b”. The result is a valid Python expression. If x is not a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found