this post was submitted on 14 May 2024
311 points (91.2% liked)

Programmer Humor

32386 readers
805 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 

Explanation: Python is a programming language. Numpy is a library for python that makes it possible to run large computations much faster than in native python. In order to make that possible, it needs to keep its own set of data types that are different from python's native datatypes, which means you now have two different bool types and two different sets of True and False. Lovely.

Mypy is a type checker for python (python supports static typing, but doesn't actually enforce it). Mypy treats numpy's bool_ and python's native bool as incompatible types, leading to the asinine error message above. Mypy is "technically" correct, since they are two completely different classes. But in practice, there is little functional difference between bool and bool_. So you have to do dumb workarounds like declaring every bool values as bool | np.bool_ or casting bool_ down to bool. Ugh. Both numpy and mypy declared this issue a WONTFIX. Lovely.

(page 2) 11 comments
sorted by: hot top controversial new old
[–] [email protected] 12 points 5 months ago (6 children)

So many people here explaining why Python works that way, but what's the reason for numpy to introduce its own boolean? Is the Python boolean somehow insufficient?

load more comments (6 replies)
[–] [email protected] 42 points 5 months ago* (last edited 5 months ago) (3 children)

Data typing is important. If two types do not have the same in-memory representation but you treat them like they do, you're inviting a lot of potential bugs and security vulnerabilities to save a few characters.

ETA: The WONTFIX is absolutely the correct response here. This would allow devs to shoot themselves in the foot for no real gain, eliminating the benefit of things like mypy. Type safety is your friend and will keep you from making simple mistakes.

load more comments (3 replies)
[–] [email protected] 108 points 5 months ago* (last edited 5 months ago) (2 children)

bool_ via Numpy is its own object, and it's fundamentally different from bool in Python (which is itself a subclass of int, whereas bool_ is not).

They are used similarly, but they're similar in the same way a fork and a spork can both be used to eat spaghetti.

load more comments (2 replies)
[–] [email protected] 1 points 5 months ago* (last edited 5 months ago) (1 children)

I/O Issues are problems that come with the territory for scripting languages like python. Its why I prefer to use bash for scripting instead, because in bash, all I/O are strings. And if there are ever any conflicts, well that's what awk/sed/Perl are for.

[–] [email protected] 2 points 5 months ago

Regex is Turing Complete after all.

[–] [email protected] 40 points 5 months ago (1 children)

Well yeah just because they kinda mean the same thing it doesn't mean that they are the same. I can wholly understand why they won't "fix" your inconvenience.

[–] [email protected] 8 points 5 months ago (1 children)

Unless I'm missing something big here, saying they "kinda mean the same thing" is a hell of an understatement.

[–] [email protected] 12 points 5 months ago (1 children)

They are two different data types with potentially different in-memory representations.

[–] [email protected] 0 points 5 months ago (1 children)

Well, yeah, but they do mean the exact same thing, hopefully: true or false

Although thinking about it, someone above mentioned that the numpy bool_ is an object, so I guess that is really: true or false or null/None

load more comments (1 replies)
[–] [email protected] 18 points 5 months ago* (last edited 5 months ago) (1 children)

This explanation is pretty clear cut

What exactly is your use case for treating np.bool_ and bool as interchangeable? If np.bool_ isn't a subclass of bool according to Python itself, then allowing one to be used where the other is expected just seems like it would prevent mypy from noticing bugs that might arise from code that expects a bool but gets an np.bool_ (or vice versa), and can only handle one of those correctly.

mpy and numpy are opensource. You could always implement the fix you need yourself ?

[–] [email protected] 1 points 5 months ago (5 children)

They've declared it as WONTFIX, so unless you're suggesting that OP creates a fork of numpy, that's not going to work.

load more comments (5 replies)
load more comments
view more: ‹ prev next ›