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.

However, you may as well just use a function statement instead; the only advantage that a lambda offers is that you can put a function definition for a simple expression inside a larger expression. But the above lambda is not part of a larger expression, it is only ever part of an assignment statement, binding it to a name. That's exactly what a def func(var1: str, var2: str): return var1.index(var2) statement would achieve.

Note that you can't annotate *args or **kwargs arguments separately either, 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.

That limitation does not apply to a PEP 544 protocol with a __call__ method; use this if you need a expressive definition of what arguments should be accepted. You need Python 3.8 or install the typing-extensions project for a backport:

from typing_extensions import Protocol

class SomeCallableConvention(Protocol):
    def __call__(self, var1: str, var2: str, spam: str = "ham") -> int:
        ...

func: SomeCallableConvention = lambda var1, var2, spam="ham": var1.index(var2) * spam

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.

Answer from Martijn Pieters on Stack Overflow
Top answer
1 of 7
342

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.

However, you may as well just use a function statement instead; the only advantage that a lambda offers is that you can put a function definition for a simple expression inside a larger expression. But the above lambda is not part of a larger expression, it is only ever part of an assignment statement, binding it to a name. That's exactly what a def func(var1: str, var2: str): return var1.index(var2) statement would achieve.

Note that you can't annotate *args or **kwargs arguments separately either, 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.

That limitation does not apply to a PEP 544 protocol with a __call__ method; use this if you need a expressive definition of what arguments should be accepted. You need Python 3.8 or install the typing-extensions project for a backport:

from typing_extensions import Protocol

class SomeCallableConvention(Protocol):
    def __call__(self, var1: str, var2: str, spam: str = "ham") -> int:
        ...

func: SomeCallableConvention = lambda var1, var2, spam="ham": var1.index(var2) * spam

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.

2 of 7
69

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

from typing import Callable
is_even: Callable[[int], bool] = lambda x: (x % 2 == 0)
🌐
Adam Johnson
adamj.eu › tech › 2022 › 10 › 10 › python-type-hints-lambda-incompatible
Python type hints: lambdas don’t support type hints, but that’s okay - Adam Johnson
(Lambdas are “neutered” in the sense that they only support a subset of function features, e.g. no keyword-only arguments.) So, there’s no way to add type hints to a lambda.
🌐
LabEx
labex.io › tutorials › python-how-to-type-hint-lambda-functions-418016
How to type hint lambda functions | LabEx
Introduced in Python 3.5, they provide static type checking and improve code readability. ## Variable type hint name: str = "John" ## Function parameter and return type hints def greet(name: str) -> str: return f"Hello, {name}!"
🌐
Like Geeks
likegeeks.com › home › python › type hints for lambda functions in python
Type Hints for Lambda Functions in Python
You can add type hints to lambda functions by wrapping them in a function or variable definition.
🌐
Kevinhakanson
kevinhakanson.com › 2022-04-10-python-typings-for-aws-lambda-function-events
Python Typings for AWS Lambda Function Events | kevinhakanson.com
import boto3 def lambda_handler(event, context): bucket = event['detail']['bucket']['name'] file_key = event['detail']['object']['key'] s3 = boto3.client('s3') file_to_process = s3.get_object(Bucket=bucket, Key=file_key) Python has a module called typing that provides support for type hints, which make for a nice autocomplete experience in Visual Studio Code.
🌐
DEV Community
dev.to › hyperkai › type-hints-in-python-3-4gg
Type hint in Python (3) - DEV Community
November 25, 2025 - The typed variable with no value is possible as an annotated assignment statement(annasgstmt) even though it's not defined yet while the untyped variable with no value is impossible as an assignment statement(asgstmt) as shown below:
🌐
AWS
docs.aws.amazon.com › powertools › python › latest › utilities › typing
Typing - Powertools for AWS Lambda (Python)
This typing utility provides static ... by providing the IDE type hints. ... All examples shared in this documentation are available within the project repository. We provide static typing for any context methods or properties implemented by Lambda context object....
🌐
GitHub
github.com › python › mypy › issues › 4880
[Feature Request] Type Hinting via Lambdas · Issue #4880 · python/mypy
April 10, 2018 - My simple solution just comes from multimethods in functional languages: why not use a single variable lambda statement. If it, when evaluated on the input, returns True, it is of that type, otherwise it is not. Example: ... Somehow preserving the x name into the type. Perhaps using delayed annotation execution in Python 3.7
Author   ryanpeach
Find elsewhere
🌐
PyPI
pypi.org › project › aws-lambda-typing
aws-lambda-typing · PyPI
A package that provides type hints for AWS Lambda event, context and response objects. It's a convenient way to get autocomplete and type hints built into IDEs.
      » pip install aws-lambda-typing
    
Published   Apr 02, 2024
Version   2.20.0
🌐
Codefinity
codefinity.com › blog › A-Comprehensive-Guide-to-Python-Type-Hints
A Comprehensive Guide to Python Type Hints
A: Yes, you can create custom type hints using classes or by using TypeVar for creating generic types in Python. Q: How do I handle type hints for variadic functions (functions that accept a variable number of arguments)? A: You can use *args and **kwargs with type hints. For *args, use a type like *args: int for all integer arguments. For **kwargs, use **kwargs: Any, as the types can vary. Q: Can type hints be used in lambda functions?
🌐
Python.org
discuss.python.org › ideas
Optional Syntax for Typed Lambda Expressions - Ideas - Discussions on Python.org
January 5, 2026 - Title: [Pre-PEP] Optional Syntax for Typed Lambda Expressions Hi everyone, I would like to propose an optional syntax extension for Python’s lambda expressions to support parameter type annotations and generic type par…
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-lambda-anonymous-functions-filter-map-reduce
Python Lambda Functions - GeeksforGeeks
Argument (x): The input value passed to the lambda function. Expression (x**2): The operation performed on the argument and returned as the result. Let's see some of the practical uses of the Python lambda function.
Published   2 weeks ago
🌐
Real Python
realpython.com › ref › glossary › type-hint
type hint | Python Glossary – Real Python
A syntactic construct that allows you to indicate the expected data types of variables, function arguments, and return values.
🌐
GeeksforGeeks
geeksforgeeks.org › python › type-hints-in-python
Type Hints in Python - GeeksforGeeks
May 3, 2025 - Type hints are not enforced by the Python interpreter; they are purely for static analysis by external tools like mypy. So, even with type hints, the code will still run with errors unless a static type checker is used.
🌐
DigitalOcean
digitalocean.com › community › tutorials › lambda-expression-python
Python Lambda Expressions Explained with Examples | DigitalOcean
July 8, 2025 - Generators are a type of iterable, and lambda functions are not designed to produce a sequence of values. If you need a generator, you should use a generator function or a generator expression. ... # This is not allowed lambda x: (x**2 for x in range(10)) # Instead, use a generator function def square_generator(): for x in range(10): yield x**2 · Lambda expressions in Python are a powerful tool for writing concise, one-line functions.
🌐
Pyapp-kit
pyapp-kit.github.io › magicgui › type_map
Type Hints to Widgets - magicgui
This is useful when you want to use a type hint that refers to a type that has not yet been defined, or when you want to avoid importing a type that is only used in a type hint. from typing import TYPE_CHECKING if TYPE_CHECKING: from mymodule import MyType def my_function(x: 'MyType') -> None: ... In Python 3.7, the __future__.annotations feature was introduced (PEP 563), which postpones the evaluation of type annotations.
🌐
Javatpoint
javatpoint.com › type-hint-concepts-to-improve-python-code
Type Hint Concepts to Improve Python Code - Javatpoint
Type Hint Concepts to Improve Python Code with Codes in Python with tutorial, tkinter, button, overview, canvas, frame, environment set-up, first python program, etc.
🌐
AWS Cloud Community
docs.powertools.aws.dev › lambda › python › 1.27.0 › utilities › typing
Typing - AWS Lambda Powertools for Python
This typing utility provides static typing classes that can be used to ease the development by providing the IDE type hints. The LambdaContext typing is typically used in the handler method for the Lambda function.
🌐
Adam Johnson
adamj.eu › tech › tag › mypy
Adam Johnson
Python has no syntax to add type hints to lambdas, but that doesn’t mean you can’t use them in type-checked code.