2 ms·
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
by sophacles 12y ago
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.
- kevin_thibedeau 12y agoYou 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.