News for hackers. Stuff that matters.
Re:Statically-typed error handling in Python using Mypy by jtdev on Sunday December 08, 2019 @03:11PM (#21737985)
>> “ I think you save time in the long run not dealing with all the type errors.”

Have you written and deployed production code in the languages you mention above? Did you encounter a substantial number of type errors?

Re:Statically-typed error handling in Python using Mypy by staticassertion on Sunday December 08, 2019 @03:29PM (#21738082)
There is no good definition of a type error. It certainly goes beyond TypeError(Exception).

Is a '.close()' method being called twice a type error? It is in a language that can express states in the type system.

Is SQL injection a type error? It is when you use refined types in your sql library interface.

The vast, vast majority of errors I run into are errors that, with effort and the right type system, I could turn into type errors.

The point being that asking "would those be type errors?" is a really big question that is, when answered simply, "yes".

Re:Statically-typed error handling in Python using Mypy by mixmastamyk on Sunday December 08, 2019 @05:20PM (#21738807)
There aren't any mainstream languages that have the ability to detect the ultra high-level errors you describe. Maybe typescript is getting better.

Pyflakes and unit tests will detect the great majority of potential errors. Mypy gets you closer to zero.

Re:Statically-typed error handling in Python using Mypy by staticassertion on Sunday December 08, 2019 @05:42PM (#21738937)
Rust can express state machines just fine, as well as refinement types (Python can do refinement types too), but yes, I agree that there's room for languages to build more ergonomic but advanced type systems.
Re:Statically-typed error handling in Python using Mypy by yawaramin on Sunday December 08, 2019 @05:52PM (#21739005)
There are very usable, well-supported languages out there right now that do have such type systems. Saying they're out of consideration because they're not mainstream is a circular argument.
Re:Statically-typed error handling in Python using Mypy by coldtea on Sunday December 08, 2019 @07:32PM (#21739543)
>Saying they're out of consideration because they're not mainstream is a circular argument.

No, it's a pragmatic argument. There's no circularity it.

If they're not mainstream, then they're neither "very usable" or "well-supported" to any extend that a mainstream language would be.

Re:Statically-typed error handling in Python using Mypy by yawaramin on Monday December 09, 2019 @09:23PM (#21749036)
Here's the circularity:

- There are no mainstream languages with powerful and convenient type systems

- But there are less mainstream ones

- But I won't use those

- There are no mainstream languages with powerful and convenient type systems

Re:Statically-typed error handling in Python using Mypy by coldtea on Tuesday December 10, 2019 @12:01PM (#21754266)
That's circularity alright, but not as in a "circular argument" though. It's a feedback loop.

That's however a totally legitimate engineering choice.

Engineers picking on a language shouldn't bet on less mainstream incomplete environments with less tooling and libs and options and devs, just so that they can raise them into the mainstream.

That might be a "tragedy of the commons" thing, but it's not an engineering obligation to be an early adopter.

Re:Statically-typed error handling in Python using Mypy by yawaramin on Thursday December 12, 2019 @09:09PM (#21778574)
They're not obligated to by early adopters, but then you hear comments like:

> There aren't any mainstream languages that have the ability to detect the ultra high-level errors you describe.

And it's like, well there are great languages that can do that, but if you restrict yourself to that tiny 'mainstream' subset, you'll never know it.

And moreover I think engineers (or rather, companies) are way too conservative about this stuff–picking 'mainstream' tech can be a touch-and-go proposition at any time. Just because something is mainstream, doesn't mean it's the right choice for your project.

Re:Statically-typed error handling in Python using Mypy by viraptor on Sunday December 08, 2019 @06:43PM (#21739287)
close() is partially handled by any language with scoped resources. There's lots of them.

Injections are handled by any language with taint, although that became less popular. Perl had it. Anything with typed orm also has something similar (for example Esqueleto in Haskell)

Re:Statically-typed error handling in Python using Mypy by mantap on Sunday December 08, 2019 @08:23PM (#21739742)
Both of those errors can also be addressed by better code, e.g. you can use lambdas to make sure things get autoclosed if there's no with/using construct. And you can avoid SQL injection by using query string interpolation instead of string concatenation, or parameter bindings.
Re:Statically-typed error handling in Python using Mypy by staticassertion on Monday December 09, 2019 @01:50AM (#21740863)
I don't really see your point. Types can prove the absence of those bugs. "Writing good code" is not... anything.
Re:Statically-typed error handling in Python using Mypy by mantap on Monday December 09, 2019 @05:31AM (#21741587)
Neither of the examples given need types if good interfaces to those functions are chosen. Since types are part of the interface then why wouldn't you just change the interface to not exhibit those bugs rather than using types to prop up a bad one?
Re:Statically-typed error handling in Python using Mypy by staticassertion on Monday December 09, 2019 @12:57PM (#21744757)
How would an interface guarantee that close is not called twice, or that you have escaped a string exactly once?
Re:Statically-typed error handling in Python using Mypy by mantap on Monday December 09, 2019 @11:06PM (#21749512)
I have rarely needed to escape a SQL string since the days of PHP4, nowadays it is standard to use parameter binding such as "SELECT * FROM table WHERE row = ?" - it's also faster since the query doesn't need to be recompiled every time, but if you really have a desire to escape SQL then you can write a string interpolation function that does it automatically e.g. sql_format("SELECT * FROM table WHERE foo = %s", s). Indeed, JavaScript supports this via custom templated string literals.

As for closing resources, you can use a pattern such as using(open("file.txt"), (f) => { ... }) if your language doesn't already support such a construct.

Re:Statically-typed error handling in Python using Mypy by staticassertion on Monday December 09, 2019 @11:49PM (#21749683)
Escaping multiple times would be unnecessary with a type based system, by definition.

Sure, no one should need to do that anymore, it's just an example. There are many other cases that are similar, however.

`using` is a language construct, not something that is part of an interface. It also does not prevent close from being called twice.

Re:Statically-typed error handling in Python using Mypy by patrec on Sunday December 08, 2019 @03:41PM (#21738150)
In my case: Yes and yes. Wildly guessing, about 90% of the errors I find at runtime with dynamic languages would would be caught by a sane type system at compile time. If I'm comparing a completely mis-engineered statically typed language such as C++, the compilation overhead may be so bad that for code I'm writing right now feedback can still be faster with the dynamically typed language. But if we're talking interfacing a few bits of unfamiliar and not great dynamically typed code: well I've literally spent several weeks on a project that would have taken me half a day with even bad static typing.
Re:Statically-typed error handling in Python using Mypy by mixmastamyk on Sunday December 08, 2019 @05:14PM (#21738762)
Pyflakes and a test or two will weed out trivial errors right away. Getting to zero errors takes more work however and that's where mypy has a place.
Re:Statically-typed error handling in Python using Mypy by viraptor on Sunday December 08, 2019 @06:48PM (#21739315)
Unfortunately tests in dynamic languages often don't help. I've run into too many issues where dependencies were mocked with parameters the dev thought were expected. (Or were expected in previous version of some library) With time, it all falls apart without real interface checks.
Re:Statically-typed error handling in Python using Mypy by mixmastamyk on Monday December 09, 2019 @12:51PM (#21744708)
Mypy can apply to tests as well. I don't have any noticeably buggy Python programs and have written more than I can count. All this tooling is a great help but is no substitute for minimum of competence and attention.

It's when you want to add features to a codebase you don't understand, when these features shine.