News for hackers. Stuff that matters.
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.