Live data from Hacker News

The benefits of static typing without static typing in Python

pawelmhm.github.io

51–60 of 68 posts

Re: The benefits of static typing without static typing in Python

#51
post #9
post #4

The main advantage of static typing to me is that it enables automatic refactorings. Without types, it's impossible for tools to refactor your code safely without the supervision of a human. This leads to developers being afraid of refactoring and, ultimately, code bases rot and become huge piles of spaghetti that nobody wants to touch. With a statically typed language, I'm never afraid to refactor whenever I see an…

I hear this argument a lot, and I'm sure static typing is helpful when refactoring, but I have found that nothing is as important as tests when refactoring. I'd rather refactor dynamically typed code with good test coverage than statically typed code without good tests.

I thought this until I started using a good type system. Java-style type systems don't replace many tests, but have you worked in Haskell / Scala / similar?

Re: The benefits of static typing without static typing in Python

#52

Ugh. I have hardly any type problems in my Ruby code. I've gotten very good at recognizing implicit state and capturing it in a properly instantiated object with a well-named class. I daresay that if you don't have this skill, a type system isn't going to help you much and you're going to get nasty bugs anyway. The problem in Ruby is nils, and you'd have the same problem with the same solution in a static language; c…

Nils are a problem in languages like C and Java, but more powerful type systems completely eradicate the issue. You might enjoy taking a look at Crystal. It has Ruby-like syntax and a type system that makes nil-checks completely unnecessary.

Nils are only a problem when you're dealing with unclean data. The only way to eradicate the issue is to only deal with clean data. Otherwise you have to code the system to be able to handle it, whether through the type system or in another way. A type system helps, but so does discipline. I'll trade the productivity gains of dynamic typing over a babysitter any day.

A type system is just OOP with added math. The benefits don't outweigh the hassle. I've tried Crystal. Did not like. It was the language that taught me that there's more to Ruby than nice syntax.

Re: The benefits of static typing without static typing in Python

#53

Having type annotations that are ignored at runtime would cause problems when you interact with code without type annotations, no? PHP has a (unfortunately quite limited) set of type annotations, but the interpreter actually enforces them.

How could adding ignored type annotations cause problems? Any problems that arise would have to be there without the type annotations...?

It can make debugging harder because "impossible" things happen - when a variable is annotated as a string you may not think to consider the possibility that it's actually an integer. But yes, optional typing is mostly just useless rather than actively harmful.

Re: The benefits of static typing without static typing in Python

#54
post #49
post #6

Optional typing like the one we see here is not uncommon in other dynamic languages: For instance, Clojure has core.typed and prismatic schema, which approach the problem in ways related to what the article shows. However, while optional typing gives you some benefits over purely dynamic typing, the fact that it's all bolted-on causes a variety of problems. First, there's the fact that you'll have code with types int…

It's more helpful to think about classes of errors and what the type system prevents. A simple type system prevents type mismatch errors. A more complex type system like Haskell's might be able to encode interfaces and other rules but might also has it's limits. The trade-off is usually that the more classes of errors you remove, the more complex the type-system becomes. Over the lifetime of your program, how much ti…

For the record, I've never really had a problem with GHC having long compilation times. If anything it's remarkably fast for what it does (IMO, it's the closest thing to a "sufficiently smart compiler" currently available).

Re: The benefits of static typing without static typing in Python

#55
The Python PEP 0484 approach, unchecked type hints, is strange, and probably a bad idea. There are good arguments for entirely dynamic typing, or entirely static typing, or optional static typing. Those have all been used successfully in other languages. But optional static typing without checking is new. (It may have been tried in some forgotten language, but it never made it into a mainstream one.) This is likely to create bugs, rather than fix them. The type you see looking at the code may not be the type being used there. This will confuse maintenance programmers. Worse, the compiler itself can't rely on the type info for optimization purposes. This limits optimizations in PyPy. Type checking is supposed to be performed by third party programs.

The syntax is backwards compatible, and this doesn't fit the language well. Forward references to types are handled via a really tacky kludge: "When a type hint contains names that have not been defined yet, that definition may be expressed as a string literal, to be resolved later." So, sometimes you write

    def foo(item: 'Tree'):
instead of

    def foo(item: Tree):
It's not stated in what scope 'Tree' is evaluated. So don't reuse type names.

Some type info is in comments:

    with frobnicate() as foo:  # type: int
        # Here foo is an int
Also, Python is getting header files (called "stub" files), like C. In most modern languages, such as Go and Rust, that's all handled automatically by storing type info in the object files. But in future Python, that will be manual.

There's function overloading, sort of. There's "@overload", but it doesn't really do anything at run time yet.

This whole thing is a collection of hacks in search of an architecture. This is the messiest type system of any mainstream language. Well designed type systems are hard enough. This is not one of them.

If this hadn't come from Python's little tin god, it would have been laughed out of the Python community. Stop him before he kills again.

Re: The benefits of static typing without static typing in Python

#56
post #37

Earlier quoted context omitted.

In addition to what the others said, I'd much rather prefer good static typing (i.e. Haskell). Especially in the prototyping phase, I do a lot of iterating and refactoring - and I mean A LOT, because I've found that this is the best way for a good design to fall out. Sometimes like 20-30% of the code get refactored, replacing core data structures and control flow. If I ever had to rely on unit tests for this, I'd sho…

What about tech that generated tests automatically from code and annotations? How do you think that would affect your work?

Well, this is basically what types are :)

Re: The benefits of static typing without static typing in Python

#57
post #51
post #9

Earlier quoted context omitted.

I hear this argument a lot, and I'm sure static typing is helpful when refactoring, but I have found that nothing is as important as tests when refactoring. I'd rather refactor dynamically typed code with good test coverage than statically typed code without good tests.

I thought this until I started using a good type system. Java-style type systems don't replace many tests, but have you worked in Haskell / Scala / similar?

Thanks, that's an insightful comment as on the statically typed side I have experience primarily in Java/c#/c++ etc. which is really what I had in mind when making the comparison. This makes me even more curious to explore Haskell one day.

Re: The benefits of static typing without static typing in Python

#58

Earlier quoted context omitted.

Nils are a problem in languages like C and Java, but more powerful type systems completely eradicate the issue. You might enjoy taking a look at Crystal. It has Ruby-like syntax and a type system that makes nil-checks completely unnecessary.

Nils are only a problem when you're dealing with unclean data. The only way to eradicate the issue is to only deal with clean data. Otherwise you have to code the system to be able to handle it, whether through the type system or in another way. A type system helps, but so does discipline. I'll trade the productivity gains of dynamic typing over a babysitter any day. A type system is just OOP with added math. The ben…

[deleted]

Re: The benefits of static typing without static typing in Python

#59
post #56

Earlier quoted context omitted.

What about tech that generated tests automatically from code and annotations? How do you think that would affect your work?

Well, this is basically what types are :)

No, not imperative types. Tests run specific data through one or more functions for a variety of reasons. Each run will have some successful or flawed effect. Different values of the same type of data can have different results. So, generating test values from specs and types automatically is different from merely typing the data or functions.

And, again, I'm talking traditional types like in Java or C++ rather than dependent types and other esoteric stuff.

Re: The benefits of static typing without static typing in Python

#60
post #44

Earlier quoted context omitted.

It is better with certain languages, and it is better when you encode business logic into your type system.

I work on Python and the Pandas data frame is one of the core data structure that is used in the implementation. I have been thinking of how I can translate this code in a more robust type checking language such as Haskell, without loosing some of the conveniences Pandas provides. Do you have any suggestions or pointers to this? I would love to encode the business logic into types. But, I am always afraid I might end…

Sadly, that's a misconception about types that languages with poor type systems have caused many to have. There's nothing about pandas that shouldn't work in Haskell.

Edit: In fact, there's this library! https://github.com/acowley/Frames

Post reply on HN