News for hackers. Stuff that matters.
Re:Pyre: A performant type-checker for Python 3 by globular-toast on Monday May 10, 2021 @04:13PM (#27110341)
Anyone considering using type hints should listen to this talk: https://www.infoq.com/presentations/dynamic-static-typing/

I personally think it's mostly a waste of time to do type hinting for the purpose of catching errors in a strongly typed language like Python. Type errors just aren't practically a problem in dynamic languages. Doing types for performance is a great reason to do it, though.

Re:Pyre: A performant type-checker for Python 3 by nickm12 on Monday May 10, 2021 @05:15PM (#27110980)
I've been on teams at multiple companies that have used type annotations for Python. Developers have generally loved adopting type annotations because it (1) makes the code easier to understand (2) makes the code easier to refactor (3) improves integration with IDEs and (4) catches real bugs.

I disagree that type errors "aren't practically a problem". NoneType errors and AttributeErrors turn up all the time in Python.

Re:Pyre: A performant type-checker for Python 3 by globular-toast on Monday May 10, 2021 @06:33PM (#27111704)
Clearly didn't watch the talk then. He found that about 1-2% of errors in real world projects were type-related errors.
Re:Pyre: A performant type-checker for Python 3 by nickm12 on Tuesday May 11, 2021 @05:57PM (#27124083)
[40 minutes into the talk]: Looks like the study scanned 3.6 million github issues across 1.7 million repositories and looked for the number of bugs that were TypeError, AttributeError, or NameError and found it was 2.7% of issues.

I'm not a fan of this kind of methodology, because there are so many different use cases for github issue tracker (including feature requests, documentation requests, etc.) that I think the denominator here is overestimated. I'd rather have a study that looked at 1000 issues in depth with a rigorous rubric than one that grepped through millions of issues.

Also, looking only at the issues doesn't count the ones that were caught in development or testing. I like the notion of "moving your bugs to the left"[1] and would much rather find these type errors at build time than later.

Finally as I said in my other comment, avoiding type errors is not the only benefit of type checking. Making code easier to understand and refactor (including programmatic refactoring) are other benefits that are not captured only by counting bugs.

[1] https://samwho.dev/blog/move-your-bugs-to-the-left/

Re:Pyre: A performant type-checker for Python 3 by joshuamorton on Monday May 10, 2021 @07:16PM (#27112090)
Fwiw this absolutely doesn't match my experience with pytype, both on my own work and, as I understand it, "at scale".
Re:Pyre: A performant type-checker for Python 3 by globular-toast on Tuesday May 11, 2021 @04:53AM (#27115803)
Ah yes... good old anecdotal evidence.
Re:Pyre: A performant type-checker for Python 3 by joshuamorton on Tuesday May 11, 2021 @12:32PM (#27120178)
The org I'm in regularly publishes peer reviewed papers on engineering productivity (including on this particular subject!). It's far less anecdotal than a random talk by a random dude.
Re:Pyre: A performant type-checker for Python 3 by globular-toast on Tuesday May 11, 2021 @12:59PM (#27120477)
Could you kindly link to one of those papers?
Re:Pyre: A performant type-checker for Python 3 by joshuamorton on Tuesday May 11, 2021 @02:32PM (#27121716)
https://storage.googleapis.com/pub-tools-public-publication-... for ane example not directly related to python (a migration from stronger than python to even stronger caught some, though a small number of errors).

I've also written about this publicly before, e.g. https://mail.python.org/archives/list/[email protected]/..., where I found a number of errors by tweaking pytype in a way to make it stronger. It's difficult to quantify what percentage of bugs would have been found anyway, but pretty much any time the type systems I work with improve, it uncovers new bugs.

Re:Pyre: A performant type-checker for Python 3 by nickm12 on Friday May 14, 2021 @05:18PM (#27159821)
I got around to watching the entire talk. It's not convincing to me. The presentation tacitly assumes that the only benefit to type systems is catching type errors and ignores the benefits to code readability and tooling. It also assumes that time spend developing and time spend debugging are fungible and I find that a bit simplistic; I would gladly trade debugging time for development time, even if the ratio is uneven. Finally, it doesn't even talk about the runtime performance penalty that dynamic languages typically incur.

Aside: It's worth commenting more on "code readability" because it cuts both ways. It can absolutely be the case that a complex, manifest type system can be a hindrance to understandably and productivity due to the "noise" of all the types that need to be written. I think struggling with C++ and (to a lesser extent) Java typing is part of the reason the industry really embraced dynamic languages in the 2000s. Python and Ruby were like a breath of fresh air. But eventually we built large systems with these languages and discovered the pain that comes from having no type-safety.

The trend I've seen in the last decade is to embrace type inference, with explicit type declarations on function signatures and otherwise only when needed for disambiguation. To me, this is the sweet spot, giving the benefits of static typing with much of the cleanliness and concision of dynamic languages.

Re:Pyre: A performant type-checker for Python 3 by marcusbuffett on Tuesday May 11, 2021 @11:22AM (#27119180)
I have to really question how the study was conducted if only 1-2% of python errors were considered type errors. Even if the only type error you consider is None vs not None, you'd get more than that. Certainly those errors account for more than 1% of the errors I see in my own professional experience using Python. I'm curious what his reasoning was so I'll watch the talk, but there's something screwy there.
Re:Pyre: A performant type-checker for Python 3 by globular-toast on Tuesday May 11, 2021 @01:03PM (#27120530)
It's a good talk even if just to learn more about types. It's not even specifically about python.

The result might be surprising, but those are the most interesting results! I note that, so far, nobody has countered this with any evidence, only anecdotes and "surely not!"

Re:Pyre: A performant type-checker for Python 3 by aflag on Monday May 10, 2021 @08:53PM (#27112872)
Fixing 1-2% of the errors in real world projects is nice, isn’t it? Not to mention catching the non real world errors before even running any tests.
Re:Pyre: A performant type-checker for Python 3 by mumblemumble on Monday May 10, 2021 @04:44PM (#27110688)
I agree with you that the benefits of static typing are sometimes greatly exaggerated. But I also think that using type hints and type checkers in Python isn't quite the same thing as static typing.

For starters, type checking can't actually guarantee you won't have type errors at run time. Because, unlike in a statically typed language, in Python, anyone can always choose to just not use type hinting. Whenever that happens, as far as the type checker is concerned, anything goes.

But type hints are very useful as hints. They help with editor tooling, which can make it easier to navigate an unfamiliar codebase. They provide extra information that makes the code easier to read. And they give me an opt-in form of type linting that allows me to set up regions of code where I don't have to take quite so much personal responsibility for ensuring that arguments are compatible with parameters. In short, it's not a correctness prover; it's an energy saver.

Re:Pyre: A performant type-checker for Python 3 by flowerlad on Monday May 10, 2021 @06:29PM (#27111657)
> I agree with you that the benefits of static typing are sometimes greatly exaggerated.

For large, multi-developer projects, it is impossible to exaggerate the benefits of static typing.

Re:Pyre: A performant type-checker for Python 3 by nickm12 on Tuesday May 11, 2021 @03:56PM (#27122736)
I don't like the term "type hints" because typecheckers like mypy are genuinely proving type correctness. Yes, if you don't annotate part of your program then you can get a type error in that part of the program, likewise, if you pass a value of the wrong type, you can get a type error. But if you are using a typechecker, you are guaranteed that if your inputs are well-typed, then you won't have a type error, which is a stronger guarantee than "hints" implies.
Re:Pyre: A performant type-checker for Python 3 by mumblemumble on Tuesday May 11, 2021 @07:37PM (#27124970)
I like the term "type hint" because that really is exactly what they are.

Typecheckers like mypy are (1) optional, and (2) are only as good as the type hints themselves. Unlike with a language that's actually statically typed, the relation of the type annotations to the actual types of the data operates largely on the honor system, so the type checking is more a validation of internal consistency than full-on static type safety. Which is fine, and well within Python's "We're all adults here" ethos. It's just that it's still not anywhere near the level of rigor you get out of the type checker in a language like OCaml.

Re:Pyre: A performant type-checker for Python 3 by nickm12 on Thursday May 13, 2021 @01:36AM (#27138788)
It's really not the honor system. If you verify with a type checker, you are proving that various type errors are impossible, provided that the caller respects the type system. The rigor is exactly the same, it's just that the type system allows for "gradual typing" so that it doesn't need to be strict as in a fully-typed language.
Re:Pyre: A performant type-checker for Python 3 by bobbylarrybobby on Monday May 10, 2021 @05:33PM (#27111145)
It's not just about type errors. (Good) autocomplete and documentation are only available when types are either explicitly labeled or correctly inferred. It's important that I be able to 1. type in just the first few letters of a method name and press <tab> to complete it, and 2. hover over a method's name and get docs for the method. Both of these are only reliable if the type of the caller is known.
Re:Pyre: A performant type-checker for Python 3 by globular-toast on Monday May 10, 2021 @06:37PM (#27111742)
But we're commenting on a type checker which implies this is being done to catch errors.
Re:Pyre: A performant type-checker for Python 3 by mvzvm on Monday May 10, 2021 @06:29PM (#27111660)
It's a problem the moment it has to interact with the outside world, especially DBs/serialization/... really any kind of I/O.
Re:Pyre: A performant type-checker for Python 3 by globular-toast on Monday May 10, 2021 @06:39PM (#27111750)
So Python can't do IO? Django doesn't exist? What are you talking about exactly?
Re:Pyre: A performant type-checker for Python 3 by mvzvm on Monday May 10, 2021 @11:05PM (#27113793)
I'm talking about type safety. Types matter, a lot, and python is a PITA to use without a system like pyre.