News for hackers. Stuff that matters.
Re:Python Type Hints – *args and **kwargs by bowsamic on Sunday August 27, 2023 @04:38PM (#37286421)
This is why I hate Python, absolutely none of this is obvious from the design of the language
Re:Python Type Hints – *args and **kwargs by LBTables on Sunday August 27, 2023 @10:31PM (#37289017)
At an even more basic level, the lack of static typing seems like such a tradeoff getting an incredibly huge nuisance in readability and stupid runtime bugs that shouldn't be a thing in exchange for a feature that's rarely useful.

Granted, I'm primarily an embedded developer. Can any Python experts explain to me a highly impactful benefit of dynamic typing?

Re:Python Type Hints – *args and **kwargs by throwaway2037 on Monday August 28, 2023 @12:13AM (#37289680)
For small programs, dynamic typing can be faster to write (not read). As soon as your program grows: "uh oh". Once you add maintenance into the cost equation, dynamic typing is a huge negative.

To be fair: 15 years ago, people were writing a lot of Java code that effectively used dynamic typing by passing around Object references, then casting to some type (unknowable to the reader) when using. (C#: Same.) It was infuriating, and also very difficult to read and maintain. Fortunately, most of that code is gone now in huge enterprise code bases.

Re:Python Type Hints – *args and **kwargs by Already__Taken on Tuesday August 29, 2023 @05:09PM (#37314138)
I'm not sold on this. Often I type the output I want to get, and reverse the code to get there. and that's faster because it's now all auto completing.
Re:Python Type Hints – *args and **kwargs by throwaway2037 on Tuesday August 29, 2023 @08:38PM (#37316137)
Interesting point. What language?
Re:Python Type Hints – *args and **kwargs by Already__Taken on Thursday August 31, 2023 @01:10AM (#37332719)
That's been my experience of powershell and typescript. To a lesser extreme python because its type hints are a bit crap.

Though I can see why you might not agree after trying an extreme like Rust. Sometimes I want to run a broken program to stop the debugger and see what I'm dealing with and rust won't do that.

Re:Python Type Hints – *args and **kwargs by throwaway2037 on Monday August 28, 2023 @12:09AM (#37289655)
To add to your list: During string concatenation, there is no automatic conversion to string. It results in an exception. It is infuriating.

This code:

    "abc" + 123
... will raise this exception:

    TypeError: can only concatenate str (not "int") to str
I have wasted so many hours fixing this same bug, over and over again.
Re:Python Type Hints – *args and **kwargs by Skeime on Monday August 28, 2023 @03:01AM (#37290757)
I strongly disagree. Not converting the int to a string automatically is absolutely the right decision. In all code I write, this TypeError would catch an actual error, because concatenation of strings is just not the right tool for creating "abc123" from "abc" and 123, so I would not use it for that. Hence, if this exception occurs, it indicates that I probably mixed up variables somewhere. Use one of the (admittedly too) many string formatting tools that Python offers, for example an f-string like f"abc{123}". (Also, if you have enough type annotations in your code, the type checker will warn you about these, so you can fix them before they hit testing or even production.)
Re:Python Type Hints – *args and **kwargs by jraph on Monday August 28, 2023 @05:23AM (#37291643)
Interesting. 100% of the times I encountered this TypeError, I actually wanted to create the concatenated string. It never caught an actual error.

Now, I guess I'm not against and explicit cast and I can imagine how the error could catch an actual bug. It's painful when the error stops the execution when the string concatenation was intended, but it is not really an issue anymore with the possibility to type check before the execution.

> concatenation of strings is just not the right tool for creating "abc123" from "abc" and 123

Why? This sounds like an opinion to me. String interpolation of formatting features are nice but I find them quite clunky in such simple cases.

Of course when you have to be careful to call str(val), it's arguably as clunky...

Re:Python Type Hints – *args and **kwargs by Skeime on Monday August 28, 2023 @07:52AM (#37292642)
Of course it’s an opinion. But in my experience, I almost exclusively have some sort of “template” with some parts to fill out, and string interpolation represents this better (in my opinion). This is especially true if the different parts to fill in are of different types.

As I wouldn’t use string concatenation for this purpose, it’s impossible for me to run into a situation where I wanted the concatenated string. (And even if I did, I would be glad for the reminder to change this into an f-string.)

And the bugs that it catches are of the form: I took some user input, expecting it to be a number, but forgot to convert it into one. Then I passed it to a function expecting a number, and it thankfully crashed instead of turning everything else into strings as well.

Maybe this is also a question that informs your view on this: Do you expect "abc" or 123 to be the “variable” part of that expression?

- If "abc" is a literal in the code with 123 coming from a variable, wanting 123 to turn into a string as well is somewhat unterstandable. - However, if 123 is the literal part of the code and "abc" the value of some variable, I would expect to mostly run into this in cases where I am actually doing some math and that the variable is a string just is some accidentally unparsed input.

In what I do, the second case would be more common.