Live data from Hacker News

The benefits of static typing without static typing in Python

pawelmhm.github.io

31–40 of 68 posts

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

#32

Has anyone ever tried to implement optional typing in python with just generators? It seems like a generator like @Signature(...input types, output type) would solve this problem with limited language changes, and would work in python 2/3?

Do you mean decorators? A solution like that exists and has been used for a while. It really is only usable for run-time type-checks. See here: https://github.com/dobarkod/typedecorator

With type annotations you can have static guarantees, which decorators will not be able to provide, as decorator methods will be called and resolved only during runtime. Objects imported from `types` hopefully will not.

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

#34
post #7
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 haven't done large-scale refactoring work before. How does typing help with refactoring? Is it because you know exactly what's being passed into a function?

Often enough, in Haskell, you can boldly make the primary change you want to make, fix all of the ensuing type errors in the rest of the code, and your refactored code will run as expected. The compiler will more or less tell you what you missed.

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

#35
post #32

Has anyone ever tried to implement optional typing in python with just generators? It seems like a generator like @Signature(...input types, output type) would solve this problem with limited language changes, and would work in python 2/3?

Do you mean decorators? A solution like that exists and has been used for a while. It really is only usable for run-time type-checks. See here: https://github.com/dobarkod/typedecorator With type annotations you can have static guarantees, which decorators will not be able to provide, as decorator methods will be called and resolved only during runtime. Objects imported from `types` hopefully will not.

Yeah, that's what I meant to type, thanks.

https://github.com/dobarkod/typedecorator is interesting -- I was imagining type signatures more like haskell's:

  addNumber :: Int -> Int -> Int
translating to something like

  @TypeSignature([int, int, int])
  
  def add_number(a,b):
      ....
It's just as possible to have the compile-time program that's doing the checking process the decorators.

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

#36
The SBCL Common Lisp compiler detects some of those type errors at compile time - especially for declared types:

    ;     (> SECRET GUESS)
    ;
    ; caught WARNING:
    ;   Derived type of GUESS is
    ;     (VALUES STRING &OPTIONAL),
    ;   conflicting with its asserted type
    ;     REAL.
    ;   See also:
    ;     The SBCL Manual, Node "Handling of Types"
    ;
    ; compilation unit finished
    ;   caught 1 WARNING condition
    ;   printed 8 notes
Common Lisp allows optional type declarations. SBCL (this feature it has inherited from CMUCL) uses those as compile time type assertions.

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

#37
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.

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 shoot myself, mainly because it would make me 1/2 times slower (because I'd have to reimplement all unit tests every time) and this would really break my flow.

Also, relying on unit tests for refactoring means that having anything short of 100% code coverage (i.e. 70-80% isn't enough) defeats the purpose of the whole thing anyway - and I've yet to see 100% test coverage in a real-world project. I realize this isn't an argument in the context of your reply (it was either test coverage or static types), but still, something to consider.

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

#38

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.

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

#39
Aren't type annotations in python just documentation that's designed to look like it's not? Seems like it would make more sense to just create a documentation standard than develop a brand new syntax for documentation. Another idea that makes more sense to me is to use @decorators. I really don't understand why this syntactic hack is supposed to be a good idea.

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

#40

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…

Yes, a type system is very much going to help you, even if you have that skill of "capturing implicit state". Because then a function you call, written by somebody else without that skill, may rely on global state in unexpected ways and still blow up in production.

A good type system doesn't allow you to call impure code from a pure function. It just won't compile. So your skill becomes useless because it's automatically verified by the compiler.

Post reply on HN