<div dir="ltr"><div><div><div><div>Ben Darnell (Tornado lead) brought up a good use case for allowing @overload in regular Python files.<br><br></div>There's some discussion (some old, some new) here: <a href="https://github.com/ambv/typehinting/issues/72">https://github.com/ambv/typehinting/issues/72</a><br><br></div>I now propose to allow @overload in non-stub (i.e. .py) files, but with the following rule: a series of @overload-decorated functions must be followed by an implementation function that's not @overload-decorated. Calling an @overload-decorated function is still an error (I propose NotImplemented). Due to the way repeated function definitions with the same name replace each other, leaving only the last one active, this should work. E.g. for Tornado's utf8() the full definition would look like this:<br><br><div class=""><pre><span class="">@overload</span>
<span class="">def</span> <span class="">utf8</span>(<span class="">value</span>: <span class="">None</span>) -> <span class="">None</span>: <span class="">...</span>
<span class="">@overload</span>
<span class="">def</span> <span class="">utf8</span>(<span class="">value</span>: <span class="">bytes</span>) -> <span class="">bytes</span>: <span class="">...</span>
<span class="">@overload</span>
<span class="">def</span> <span class="">utf8</span>(<span class="">value</span>: <span class="">str</span>) -> <span class="">bytes</span>: <span class="">...</span>  <span class=""># or (unicode)->bytes, in PY2</span>
<span class="">def</span> <span class="">utf8</span>(<span class="">value</span>):
    <span class=""># Real implementation goes here.</span></pre></div><br>NOTE: If you are trying to understand why we can't use a stub file here or why we can't solve this with type variables or unions, please read the issue and comment there if things are not clear. Here on python-ideas I'd like to focus on seeing whether this amendment is non-controversial (apart from tea party members who just want to repeal PEP 484 entirely :-).<br><br></div>I know that previously we wanted to come up with a complete solution for multi-dispatch based on type annotations first, and there are philosophical problems with using @overload (though it can be made to work using sys._getframe()). The proposal here is *not* that solution. If you call the @overload-decorated function, it will raise NotImplemented. (But if you follow the rule, the @overload-decorated function objects are inaccessible so this would only happen if you forgot or misspelled the final, undecorated implementation function).<br clear="all"></div><div><div><div><div><div><div><div><br>-- <br><div class="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div></div></div></div></div></div></div></div>