Live data from Hacker News

Open-sourcing MonkeyType – Let your Python code type-hint itself

engineering.instagram.com

141–150 of 237 posts

Re: Open-sourcing MonkeyType – Let your Python code type-hint itself

#141

I come from a statically typed background (C++), but have been doing a lot of analytics in python in the past two years. It is frustrating not to have compile time guarantees when dealing with mathematical programs, because some things have to be a particular type (i.e. matrices of compatible dimensions). The result is a copious use of asserts, but it feels bad when you know that if you did this in a functional langu…

>(i.e. matrices of compatible dimensions)

What language do you use where you can get these kinds of guarantees? As far as I know very few languages provide those kinds of dependent types statically.

Re: Open-sourcing MonkeyType – Let your Python code type-hint itself

#142
post #41

Given that people have asked why not use a statically typed language, seems appropriate to mention that it's possible to write pythonic-looking C++: http://preshing.com/20141202/cpp-has-become-more-pythonic/ I have been using C++ a lot lately but really wish there were more tools for reflection at compile time, e.g., ability to iterate over all the members of a class. Other than that, I'm really loving C++17's auto t…

Please show me C++ equivalent of sorted([(k.weight, k.name) for k in somelist], reverse=True)

Note: my C++ is extremely rusty.

Right now, I think that's approximately,

    vector> output;
    transform(
        somelist.begin(), somelist.end(),
        back_inserter(output),
        [](const auto &f) { return make_tuple(f.weight, f.name); }
    );
    sort(output.rbegin(), output.rend());
Ranges, I believe, would reduce this a lot, possibly even to a single line. If I am reading the docs on it correctly, something like,

    vector(somelist | view::transform([](const auto &f) { return make_tuple(f.weight, f.name); })) | action::sort;
I think.

Two notes, however:

1. I feel like most of the desire for a static language is to know what type something is. Is C++ exactly as brief as Python? No, as I think you've demonstrated. But I think you're a lot more likely to know the type of something. Rarely do I think I find that Python has annotations, and annotations can be wrong.

2. C++ is, in general, I feel, much more explicit about where copies occur. I elided one of the copies in your example, opting instead for an in-place sort (but this is trivial to fix in the Python).

Re: Open-sourcing MonkeyType – Let your Python code type-hint itself

#143
post #134

Earlier quoted context omitted.

Please show me C++ equivalent of sorted([(k.weight, k.name) for k in somelist], reverse=True)

I'll bite. std::vector > in_order; std::transform(data.begin(), data.end(), std::back_inserter(in_order), [](auto& item) {return std::make_pair(item.weight, item.name);}); std::sort(in_order.begin(), in_order.end(), std::greater ()); While I won't claim it to be as elegant as Python, it doesn't seem too ugly. Does anybody have an idea about how `auto` could be utilized to avoid the long vector type declaration? For t…

Templates, auto, and type toys can give you some serious build-your-own syntactic sugar: https://repl.it/repls/PleasingLovingQueenslandheeler

Pulling things out of tuples isn't great, though.

Re: Open-sourcing MonkeyType – Let your Python code type-hint itself

#144

Earlier quoted context omitted.

I think the general consensus is that that is not true. Python's dynamic nature is a clear advantage it has over statically typed languages. Add the fact that you can elect to tune down the dynamism when it makes sense to with very little impact on your existing stack makes Python the technological superior choice for the majority of applications.

Absolutely not. Python is not the technological superior choice for the majority of the applications. If you think so then your experience in different domains and application types must be very limited. The preconcept that dynamic languages are more productive is just an illusion because you can easily take shortcuts that will hamper your progress in the future. A proper typed language with HM type inference has the…

Empirically, there are a lot more million line python codebases than F# or haskell codebases, in fact I can name multiple million line python codebases, and 0 F# or haskell codebases. Given that, logic would indicate some sort of failure on the part of haskell and F#, or they would see wider adoption among the large codebases where they are so useful.

Do you disagree?

Re: Open-sourcing MonkeyType – Let your Python code type-hint itself

#145
post #41

Given that people have asked why not use a statically typed language, seems appropriate to mention that it's possible to write pythonic-looking C++: http://preshing.com/20141202/cpp-has-become-more-pythonic/ I have been using C++ a lot lately but really wish there were more tools for reflection at compile time, e.g., ability to iterate over all the members of a class. Other than that, I'm really loving C++17's auto t…

Please show me C++ equivalent of sorted([(k.weight, k.name) for k in somelist], reverse=True)

Here (http://rextester.com/QUIK26485):

  somelist | transformed([](auto &&k) { return make_pair(k.weight, k.name); }) | sorted | reversed;

Re: Open-sourcing MonkeyType – Let your Python code type-hint itself

#146

As both a dynamic and static type enthusiast, back typing dynamic code is extremely problematic. Fluent use of a dynamic language will use and create constructs that are nearly un-typeable. If you want to make typed code, start typed. If you code with implicit types, use a good type inferred language (ML, F#, etc). If you want to use type checking in Python, use the annotations and MyPy from the beginning. That said,…

>Fluent use of a dynamic language will use and create constructs that are nearly un-typeable.

Can you give examples? In practice, I haven't noticed this being an issue.

Re: Open-sourcing MonkeyType – Let your Python code type-hint itself

#147

As both a dynamic and static type enthusiast, back typing dynamic code is extremely problematic. Fluent use of a dynamic language will use and create constructs that are nearly un-typeable. If you want to make typed code, start typed. If you code with implicit types, use a good type inferred language (ML, F#, etc). If you want to use type checking in Python, use the annotations and MyPy from the beginning. That said,…

>Fluent use of a dynamic language will use and create constructs that are nearly un-typeable.

Can you give examples? In practice, I haven't noticed this being an issue. A lot of python code already had/has docstrings explaining the types, these annotations just formalize that a bit more.

Re: Open-sourcing MonkeyType – Let your Python code type-hint itself

#148
post #113
post #103

Earlier quoted context omitted.

Most of the c++ and c# code I see lately has so many things declared as auto, it's hard for me to figure things out too.

Would be nice if IDEs had a key combo to fill in the actual type for "auto"s!

That would be nice!

Re: Open-sourcing MonkeyType – Let your Python code type-hint itself

#149

Earlier quoted context omitted.

Please show me C++ equivalent of sorted([(k.weight, k.name) for k in somelist], reverse=True)

Note: my C++ is extremely rusty. Right now, I think that's approximately, vector > output; transform( somelist.begin(), somelist.end(), back_inserter(output), [](const auto &f) { return make_tuple(f.weight, f.name); } ); sort(output.rbegin(), output.rend()); Ranges, I believe, would reduce this a lot , possibly even to a single line. If I am reading the docs on it correctly, something like, vector (somelist | view::t…

> vector

Really? O.o How could this possibly work?

Re: Open-sourcing MonkeyType – Let your Python code type-hint itself

#150

I come from a statically typed background (C++), but have been doing a lot of analytics in python in the past two years. It is frustrating not to have compile time guarantees when dealing with mathematical programs, because some things have to be a particular type (i.e. matrices of compatible dimensions). The result is a copious use of asserts, but it feels bad when you know that if you did this in a functional langu…

>(i.e. matrices of compatible dimensions) What language do you use where you can get these kinds of guarantees? As far as I know very few languages provide those kinds of dependent types statically.

In graphics programming, you have specific types for lots of small vectors and matrices (for vectors, there are separate types for every sizes 4 and below, and matrices usually comes in a variety of sizes as well, at the very least 3x3, 3x4 and 4x4).

Typechecking is very useful here: if you try to transform a point in space represented by a Vector3 by a general 4x4 matrix, it fails compilation because you have to convert the point to homogeneous coordinates first. Very useful information from the type system.

Post reply on HN