Is it possible to type hint a lambda function?

2020-02-09 05:37发布

Currently, in Python, a function's parameters and return types can be type hinted as follows:

def func(var1: str, var2: str) -> int:
    return var1.index(var2)

Which indicates that the function takes two strings, and returns an integer.

However, this syntax is highly confusing with lambdas, which look like:

func = lambda var1, var2: var1.index(var2)

I've tried putting in type hints on both parameters and return types, and I can't figure out a way that doesn't cause a syntax error.

Is it possible to type hint a lambda function? If not, are there plans for type hinting lambdas, or any reason (besides the obvious syntax conflict) why not?

标签: python lambda
2条回答
叛逆
2楼-- · 2020-02-09 06:07

Since Python 3.6, you can (see PEP 526):

from typing import Callable
is_even: Callable[[int], bool] = lambda x: (x % 2 == 0)

As user c-z noted, this is not the same as annotating the signature of a non-anonymous function though. Mypy v0.620 doesn't complain if you pass a str variable to is_even in the above example.

查看更多
戒情不戒烟
3楼-- · 2020-02-09 06:09

You can, sort of, in Python 3.6 and up using PEP 526 variable annotations. You can annotate the variable you assign the lambda result to with the typing.Callable generic:

from typing import Callable

func: Callable[[str, str], int] = lambda var1, var2: var1.index(var2)

This doesn't attach the type hinting information to the function object itself, only to the namespace you stored the object in, but this is usually all you need for type hinting purposes. Note that you can't annotate *args or **kwargs arguments separately this way, as the documentation for Callable states:

There is no syntax to indicate optional or keyword arguments; such function types are rarely used as callback types.

For the lambda expression itself, you can't use any annotations (the syntax on which Python's type hinting is built). The syntax is only available for def function statements.

From PEP 3107 - Function Annotations:

lambda 's syntax does not support annotations. The syntax of lambda could be changed to support annotations, by requiring parentheses around the parameter list. However it was decided not to make this change because:

  • It would be an incompatible change.
  • Lambda's are neutered anyway.
  • The lambda can always be changed to a function.

You can still attach the annotations directly to the object, the function.__annotations__ attribute is a writable dictionary:

>>> def func(var1: str, var2: str) -> int:
...     return var1.index(var2)
...
>>> func.__annotations__
{'var1': <class 'str'>, 'return': <class 'int'>, 'var2': <class 'str'>}
>>> lfunc = lambda var1, var2: var1.index(var2)
>>> lfunc.__annotations__
{}
>>> lfunc.__annotations__['var1'] = str
>>> lfunc.__annotations__['var2'] = str
>>> lfunc.__annotations__['return'] = int
>>> lfunc.__annotations__
{'var1': <class 'str'>, 'return': <class 'int'>, 'var2': <class 'str'>}

Not that dynamic annotations like these are going to help you when you wanted to run a static analyser over your type hints, of course.

查看更多
登录 后发表回答