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.
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:
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:)
and so on.