[Python-Dev] Function Prototypes

2021年12月23日 07:23:10 -0800

Hello
In thread about PEP 677, I (with the help of another proposal) came up with an 
alternative to typing.Callable called function prototypes. The basic concept is 
to create a function prototype object when the body of a function is omitted.
The thread can be found here: 
https://mail.python.org/archives/list/[email protected]/thread/OGACYN2X7RX2GHAUP2AKRPT6DP432VCN/
Mark Shannon initially proposed that functions be used as types and provided 
this example:
@Callable
def IntToIntFunc(a:int)->int:
 pass
def flat_map(
 l: list[int],
 func: IntToIntFunc
) -> list[int]:
 ....
I further proposed that we make the body of a function non-mandatory and create 
a function prototype if it is omitted. I provided these examples:
import typing
@typing.Callable
def IntToIntFunc(a: int) -> int
def flat_map(
 l: list[int],
 func: IntToIntFunc
) -> list[int]:
 ...
import ctypes
@ctypes.CFUNCTYPE
def f(x: int) -> bool
I have since taken it upon myself to implement this in a fork of cpython: 
https://github.com/asleep-cult/cpython
To remain consistent with function definitions, I have also added an 
alternative lambda syntax that allows you to annotate arguments and the return 
type. The syntax requires you to parenthesize the argument list:
lambda (a: int, b: int) -> int: ...
This new lambda syntax also allows you to create a function prototype by 
omitting the body. The original example can be rewritten as follows:
def flat_map(
 l: list[int],
 func: lambda (a: int) -> int
) -> list[int]:
 ...
Problems:
- It is not possible to use ParamSpec with this
- The lambda prototype syntax might be jarring
- Some people might accidentally forget the function body
Here is some feedback that I have already collected:
"Yeah, making the body optional (without looking at decorators) is not
acceptable either. Too easy to do by mistake (I still do this All. The.
Time. :-)" - Guido van Rossum
"i would strongly prefer this over the existing pep" - Ronny Pfannschmidt
"I find this unnecessary and unreadable.
Python isn't C or Java." - BundleOfJoysticks (Reddit)
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/B6VKYV5TKD2VSK6D2CUN77Q6MI5VIBU5/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to