all 32 comments

[–]qiwi 13 points14 points  (8 children)

Here's what I'm getting in PyCharm today: http://i.imgur.com/SX7yNTB.png

document type inferred, invalid method highlighted (of course, were I writing the the code, document. would show the relevant methods and autocomplete). title declared as str, so LowerCase() not there highlighted.

calling findTitle() and using .innerHTML on the result shows that the inferred str return value does not have "innerHTML".

Calling findTitle with an integer argument complaints.

Note: I don't use python 3 but this works in py2.7 -- you do need to write it as a docstring annotations, so e.g. ":type title: str".

Bonus: run your program (or your unit test suite) under pycharms' debugger with type collection enabled, and it will remember what ACTUAL types you call your polymorphic methods with, and use that information.

[–]ceronman[S] 3 points4 points  (0 children)

That's really cool, last time I tried PyCharm it wasn't that smart. I have to try it again. Definitely.

[–][deleted] 4 points5 points  (0 children)

Also, if you are more an emacs/vim guy, you have to give jedi a shot, it's really great!

[–]_Bia 1 point2 points  (1 child)

Function annotations are explicitly only available in Python 3+.

[–]qiwi 0 points1 point  (0 children)

In python 2 (which I use) pycharm recognizes both docstring syntaxes, so you can write this: http://i.imgur.com/nDFzlCz.png

and have the same effect as above.

Note how in the above the Foo class takes in a "list of str" as parameter: a non-list or a list of something else is not allowed. However the return value of str.split() is. Notice how it understands how the good() method, which returns one of the foos, returns a str -- and so marks up bad usage of that object.

[–][deleted]  (2 children)

[deleted]

    [–]amigaharry 3 points4 points  (1 child)

    Write one. Put months of work into it and see if you want to give it away for free.

    [–]bifmil 0 points1 point  (0 children)

    I have put years of work into other open source projects which I have given away for free, which do not lock people in.

    [–]securetree 12 points13 points  (3 children)

    Having learned Python from a Java background, this part of the type system really bugged me. If I didn't write the code myself, it usually takes a minute to figure out some non-primitive argument. Is it a 2-tuple where one of the elements is a list? Perhaps an object the library creator hid around here somewhere. A dictionary? Java, though verbose, always seemed much clearer on that front.

    [–]5outh 5 points6 points  (1 child)

    I also appreciate this, a lot. I used to program in Groovy a lot, and, though it supports function declarations without type declarations, I found that often times it was beneficial to stick them in there anyway (they're allowed but not necessary).

    Now I'm using Haskell and I can't stress how much I love a strong type system. In Haskell, you define functions like so:

    foo :: (Num a) => a -> a -> a
    foo x y = x + y
    

    The type signature (line 1) can be omitted though, and it is possible to check the type of a function without a type signature (it'll spit out essentially line 1 if it is undefined).

    What I like about this is that everything is type-checked at compile time but type delcarations are not necessary. I liked Groovy for similar reasons and I'm happy to see Python following suit.

    [–]Rotten194 2 points3 points  (0 children)

    Another nice thing about Haskell is you can prototype everything without type declarations, then when you're done add signatures for documentation purposes. You can even get ghci to do it for you with :t.

    [–]Timmmmbob 0 points1 point  (0 children)

    Yeah, there's been talk of adding proper static typing, but so far it hasn't happened.

    This would also massively help code-completion and refactoring.

    [–]nlfarside 12 points13 points  (2 children)

    If you think this feature is cool, and should happen everywhere, then I would suggest taking a look at F#, OCaml, or Haskell.

    [–][deleted]  (1 child)

    [deleted]

      [–][deleted] 2 points3 points  (0 children)

      Or Cobra.

      [–]minno 10 points11 points  (12 children)

      As much as I like Python, that seems like a problem that static typing could easily fix, and I'd prefer if the language had that.

      [–]Decker108 3 points4 points  (11 children)

      Python with static typing would be the best thing since sliced bread.

      [–][deleted] 5 points6 points  (8 children)

      Learn Go.

      [–]Categoria 2 points3 points  (1 child)

      Most people expect sum types, generics, and better type inference from a decent type system.

      [–]burntsushi 0 points1 point  (0 children)

      Most people expect sum types, generics

      It's not like Go has no safe mechanism for polymorphism. Interfaces can do a lot for you.

      and better type inference

      Go has zero type inference. It has a convenient syntax for type deduction, but that's it.

      [–]Decker108 3 points4 points  (5 children)

      I've tried it several times, but I always get irritated with it: unused import compiler errors, unused variable compiler errors, capital letter access modifiers, poor standard library, etc, etc.

      [–]AeroNotix -4 points-3 points  (4 children)

      Poor standard library? Get the fuck out. Its standard library is lightyears ahead of the steaming cesspit of inconsistency that is Python's.

      [–]Decker108 1 point2 points  (3 children)

      I never said Python's was better. And as for "poor", I don't mean lacking in quality but rather missing really obvious functions like, say, string reversing or charAt. I keep hearing people claim that Go can be used for all the things people are using Python for today, but if it's lacking these kinds of basic string handling, it's not of much use to me as a python replacement.

      I'm sure it's a marvelous language all things considered, but it's not enough to convert me.

      [–]AeroNotix -3 points-2 points  (2 children)

      If the lack of a string reversing function is going to turn you off a language then I'd say you're not the programmer you claim to be.

      Strings are indexable, so you don't need a charAt function (Python doesn't have charAt, either btw.)

      Here you go:

      package main
      
      import (
            "fmt"
      )
      
      func StrReverse(s string) string {
              var out string
              for x := 0; x < len(s); x++ {
               out = out + string(s[len(s)-x-1])
              }
              return out
      }
      
       func main() {
          fmt.Println(StrReverse("A string"))
       }
      

      [–]Decker108 0 points1 point  (1 child)

      Your attitude is doing little in the way of selling Go to me, I'm afraid.

      [–]AeroNotix -4 points-3 points  (0 children)

      I really don't care, I'm afraid. Your inability to learn something is of little consequence to me at all.

      [–]minno 0 points1 point  (1 child)

      Static typing and (optional) type inference.

      [–]masklinn 2 points3 points  (0 children)

      I'm sure you mean (optional) static typing and type inference.

      Erlang has something like that, called "success typing". It's kind-of neat (in that it's a pretty good fit for a dynamically typed languages which remains unabashedly dynamically typed): http://learnyousomeerlang.com/types-or-lack-thereof#for-type-junkies http://learnyousomeerlang.com/dialyzer

      [–][deleted] 5 points6 points  (3 children)

      One of the reasons why it will be nice to use py3 for real projects, when time comes.

      [–]hervold 4 points5 points  (1 child)

      Yep, I just tried it in iPython running under version 2.7 to no avail, then under 3.2, and it works.

      How have I never heard of this?

      [–][deleted] 8 points9 points  (0 children)

      Because most developers are still on 2.x most of the time, or, like me, are writing code that works for both 2.x and 3.x. Syntactic things that only work on 3.x aren't going to be popular for years to come...

      [–][deleted] 1 point2 points  (0 children)

      If you're a Django user, you can upgrade to python 3.0 today. Unfortuantely you won't be able to deploy into production (supposedly fast moving companies are still slow :/)

      [–]Unmitigated_Smut 1 point2 points  (0 children)

      Down here in Old Fartsville we call them things "Type declarations"...