this post was submitted on 14 May 2024
10 points (77.8% liked)

Programmer Humor

32475 readers
1525 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.

you are viewing a single comment's thread
view the rest of the comments
[–] danielquinn@lemmy.ca 6 points 6 months ago* (last edited 6 months ago) (2 children)

Honestly, after having served on a Very Large Project with Mypy everywhere, I can categorically say that I hate it. Types are great, type checking is great, but applying it to a language designed without types in mind is a recipe for pain.

[–] folkrav@lemmy.ca 4 points 6 months ago

Adding types on an untyped project is hell. Greenfield stuff is usually pretty smooth sailing as far as I’m concerned…

[–] acannan@programming.dev 2 points 6 months ago (1 children)

In my experience, mypy + pydantic is a recipe for success, especially for large python projects

[–] scrion@lemmy.world 1 points 6 months ago (1 children)

I wholeheartedly agree. The ability to describe (in code) and validate all data, from config files to each and every message being exchanged is invaluable.

I'm actively looking for alternatives in other languages now.

[–] expr@programming.dev 1 points 6 months ago (1 children)

You're just describing parsing in statically-typed languages, to be honest. Adding all of this stuff to Python is just (poorly) reinventing the wheel.

Python's a great language for writing small scripts (one of my favorite for the task, in fact), but it's not really suitable for serious, large scale production usage.

[–] renzev@lemmy.world 1 points 6 months ago

Gradual typing isn't reinventing the wheel, it's a new paradigm. Statically typed code is easier to write and harder to debug. Dynamically typed code is harder to debug, but easier to write. With gradual typing, the idea is that you can first write dynamic code (easier to write), and then -- wait for it -- GRADUALLY turn it into static code by adding type hints (easier to debug). It separates the typing away from the writing, meaning that the programmer doesn't have to multitask as much. If you know what you're doing, mypy really does let you eat your cake and keep it too.