How about instead of omitting the body, it contains a single expression, say:
def int(x: float): -> float typing.Prototype It doesn’t totally break protocol to have a function complie differently depending on its content— that’s done with generator functions. -CHB On Thu, Dec 23, 2021 at 7:20 AM <asleep.c...@gmail.com> wrote: > 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/python-dev@python.org/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 -- python-dev@python.org > To unsubscribe send an email to python-dev-le...@python.org > https://mail.python.org/mailman3/lists/python-dev.python.org/ > Message archived at > https://mail.python.org/archives/list/python-dev@python.org/message/B6VKYV5TKD2VSK6D2CUN77Q6MI5VIBU5/ > Code of Conduct: http://python.org/psf/codeofconduct/ > -- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython
_______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/PFN5BYFLWA75R3ZDYVKEP37VRCPLAVM3/ Code of Conduct: http://python.org/psf/codeofconduct/