News for hackers. Stuff that matters.
Re:Type hinting for Python by sophacles on Tuesday January 06, 2015 @11:51AM (#8845329)
On the other hand, this could be used for some nice "duck type checking".

Rather than the classic interface pattern, where the passed object must be a subclass of a certain type, these annotations could be used inversely to that. Something like:

   duckclass foo:
       def x(): pass
       def y(a: int) -> str: pass
       n: int

   def bar(it: foo) -> boolean: 
       ...
Where other classes don't need to explicitly inherit from foo, but rather, a checker can raise a warning where you are passing something that doesn't have an integer attribute (or getter function) called n, and the x and y functions. This allows some nice analysis, while still allowing good duck typing.

There are ways to do this without special keyworks too... that was just a quick first example. (maybe something like:)

   class foo:
      def x(): pass
      def y(a: int) -> str: pass
      n: int

   def bar(it: duck_as(foo)) ->str: pass
and so on.
Re:Type hinting for Python by kevin_thibedeau on Tuesday January 06, 2015 @12:34PM (#8845651)
You can have this today by inserting an explicit decorator that validates annotated arguments. You can even skip the decorators by dynamically wrapping callables with the validator.