News for hackers. Stuff that matters.
Re:Python at Scale: Strict Modules by orf on Friday December 18, 2020 @05:07AM (#25465629)
Python allows gradual typing everywhere. Adding “Dict[str, Any]” gives you value and let’s you catch bugs. Not as much as a more specific signature, but far more than having no annotations.
Re:Python at Scale: Strict Modules by matesz on Friday December 18, 2020 @05:14AM (#25465662)
> Adding “Dict[str, Any]” gives you value and let’s you catch bugs

What types of bugs are you speaking of? Can you share an example?

Remember about constructive feedback method - what, why and how.

Re:Python at Scale: Strict Modules by pas on Friday December 18, 2020 @05:23AM (#25465716)
The classic bugs are passing the wrong type of argument and calling a method on None.

PyCharm did static analysis and caught a lot of these, but it wasn't always that obvious. With a type hint, it's easy for the IDE to spot things. It's easy to integrate mypy into the CI process, etc.

We inherited a py2.7 codebase, written by one dev. It uses a library written also by that dev. We had absolutely no idea what worked how. Sure, looking at the code gave us a very rough big picture, but adding typing helps with the details.

Re:Python at Scale: Strict Modules by matesz on Friday December 18, 2020 @05:33AM (#25465778)
> We inherited a py2.7 codebase, written by one dev. It uses a library written also by that dev. We had absolutely no idea what worked how. Sure, looking at the code gave us a very rough big picture, but adding typing helps with the details.

I see how this could be helpful for legacy codebase - add typings, run mypy and see if it breaks. Good point - thanks.

How many lines of code that inherited codebase had btw?

Re:Python at Scale: Strict Modules by pas on Friday December 18, 2020 @07:46PM (#25473893)
Quite small in LOCs less than 10K. (A horribly non-idiomatic custom ETL lib, and a big ball of MVC stuff using pyramid and sqlalchemy/zope.transaction.)
Re:Python at Scale: Strict Modules by ArchieMaclean on Friday December 18, 2020 @05:24AM (#25465729)
e.g. say you're writing some code that does something like this:

account = queryDatabase(email)

updatedUser = update(account)

ping(account)

queryDatabase returns a dictionary. Later on, someone decides that a single username can have multiple accounts, so someone changes queryDatabase to return a list of dictionaries instead of a single dictionary. Now the above code is broken, since update expects a single dictionary. Of course, you could grep through the whole codebase to try and find all the problems, but it would be easier and less prone to human error to rely on the type system to fail, especially if the change was something less contrived, and if changes like this were happening all the time.

The effect of this could be limited by other means (e.g. naming the function better) but type systems are just another way of adding robustness to a program.

Re:Python at Scale: Strict Modules by orf on Friday December 18, 2020 @05:18AM (#25465689)
You’re asking how type annotations can help you catch bugs. I’m not particularly inclined to synthesise a bunch of examples (especially after the highly condescending initial wording of your comment), it seems fairly obvious and I’m sure you’re smart enough to figure it out.
Re:Python at Scale: Strict Modules by matesz on Friday December 18, 2020 @05:26AM (#25465742)
Can you show me this "initial highly condescending wording of your comment"?
Re:Python at Scale: Strict Modules by orf on Friday December 18, 2020 @05:32AM (#25465776)
I think we’ll leave this thread here, but if in the future you need to ask something from someone in good faith I would work on refining your wording a bit.
Re:Python at Scale: Strict Modules by matesz on Friday December 18, 2020 @07:36AM (#25466463)
> I’m sure you’re smart enough to figure it out

You are making it personal (well, actually trying to insult), where I was asking you to provide more value.