Is it possible to type hint a lambda function?

Nathan Merrill :

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?

Martijn Pieters :

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the type hint for a function

Function hint - possible values for parameters

Is it possible to type hint more than one type?

Type hint that a function never returns

Type hint function accepting a Union

How to reference the type hint of a function

Is it possible to use type hint validation directly?

Is it possible to type hint a string that has numbers?

it is possible to type-hint a compiled regex in python?

Is Unpacking a Type Hint Possible? or Its Workarounds?

How to type hint that a function returns another function?

How to type hint a function that returns a function?

Is it possible to hint that a function parameter should not be modified?

What are the possible values for Spark DataFrame hint function?

Explicit type hint in method reference lambda results in raw type

How do I type hint a filename in a function?

The right way to type hint a Coroutine function?

What is the return type hint of a generator function?

How to hint the type of a function I do not control?

How to type hint a function that transforms an RDD?

Type hint for a reference to a generic function in TypeScript

type hint clone function in python 3.5+

How to type hint specific objects in function argument

How to type-hint "SupportsOrdering" in a function parameter?

Custom aggregate function in flink type hint

What is the type hint of a function that returns an image?

Is it possible to type hint exclusively a class object but exclude subclass objects?

How to type hint pandas.NA as a possible output

How to type hint function accepting both union and any type of that union?