Python typing Module
Example
Use type hints in function definitions:
from typing import List
def greet(names: List[str]) -> str:
return f"Hello {', '.join(names)}"
result = greet(['Emil', 'Linus'])
print(result)
Try it Yourself »
Definition and Usage
The typing module provides support for type hints in Python code.
Use it to add type annotations for better code documentation, IDE support, and static type checking with tools like mypy.
Members
Member | Description |
---|---|
Annotated | Add metadata to type hints. |
Any | Type that allows any value. |
Callable | Type for callable objects (functions, methods). |
cast() | Cast a value to a specific type (for type checkers). |
ClassVar | Mark class variables that should not be set on instances. |
Dict | Generic type for dictionaries. |
Final | Mark a variable as final (should not be reassigned). |
Generic | Base class for user-defined generic types. |
get_type_hints() | Return type hints for a function or method. |
List | Generic type for lists. |
Literal | Type for literal values. |
Optional | Type that can be a specific type or None. |
Protocol | Base class for protocol classes (structural subtyping). |
Set | Generic type for sets. |
Tuple | Generic type for tuples. |
TypedDict | Define dictionary types with specific required keys. |
TypeVar | Create type variables for generic types. |
Union | Type that can be one of several types. |