On Fri, Jul 8, 2016 at 7:02 PM, Chris Angelico email@example.com wrote:
Now your decorator does nothing, and there is literally ZERO run-time cost (the only cost is a single cheap check when the function is defined, which usually means on module import), closures aside. That wouldn't be possible with a core interpreter change...
I really shouldn't say impossible. Literally the moment I clicked Send, I thought of a way it could be done with minimal run-time cost: just create a completely separate opcode for "type-checked call". I don't think that would materially slow down the regular call operation. As a bonus, any functions that don't have annotations could use the normal call opcode, so they wouldn't be slowed down.
But the main point is, this would all entail additional complexity inside CPython, where the decorator method keeps the complexity in your application - and doesn't need nearly as much of it. The one advantage that CPython has is, as mentioned, the matching of arguments to parameters; maybe that could be provided in functools somewhere?