[Python-ideas] PEP 484 change proposal: Allowing @overload outside stub files

Guido van Rossum guido at python.org
Fri Jan 22 15:00:57 EST 2016


Ben Darnell (Tornado lead) brought up a good use case for allowing
@overload in regular Python files.

There's some discussion (some old, some new) here:
https://github.com/ambv/typehinting/issues/72

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:

@overloaddef utf8(value: None) -> None: ... at overloaddef utf8(value:
bytes) -> bytes: ... at overloaddef utf8(value: str) -> bytes: ...  # or
(unicode)->bytes, in PY2def utf8(value):
    # Real implementation goes here.


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 :-).

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).

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160122/015a7780/attachment.html>


More information about the Python-ideas mailing list